569C】Primes or Palindromes? (思維,分析範圍,暴力判斷,)
題幹:
Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic number is another matter. It is aesthetically pleasing, and it has a number of remarkable properties. Help Rikhail to convince the scientific community in this!
Let us remind you that a number is called prime if it is integer larger than one, and is not divisible by any positive integer other than itself and one.
Rikhail calls a number a palindromic if it is integer, positive, and its decimal representation without leading zeros is a palindrome, i.e. reads the same from left to right and right to left.
One problem with prime numbers is that there are too many of them. Let's introduce the following notation: π(n) — the number of primes no larger than n, rub(n) — the number of palindromic numbers no larger than n. Rikhail wants to prove that there are a lot more primes than palindromic ones.
He asked you to solve the following problem: for a given value of the coefficient A
Input
The input consists of two positive integers p, q, the numerator and denominator of the fraction that is the value of A (, ).
Output
If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes).
Examples
Input
1 1
Output
40
Input
1 42
Output
1
Input
6 4
Output
172
解題報告:
通過觀察單調性,和p和q給的範圍,我們知道可以算出一個極限來,可以算出 ,n是20W就足夠了,資料量也不大,直接打表暴力。
AC程式碼:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 2e6+ 5;
int wei[1005],cnt;
ll su[1000005];
bool isprime[MAX];
ll sum1[MAX],sum2[MAX];//sum1素數,sum2迴文數
void prime() {
cnt = 0;
memset(isprime,1,sizeof isprime);
isprime[0] = isprime[1] =0;
for(ll i = 2 ; i<MAX; i++) {
if(isprime[i]) {
su[++cnt] = i;
}
for(ll j = 1; i * su[j]< MAX && j <= cnt; j++) {
isprime[i * su[j]] = 0;
}
}
}
bool fit(int x) {
int p = 0;
while(x) {
wei[++p] = x%10;
x/=10;
}
for(int i = 1; i<=(p+1)/2; i++) {
if(wei[i] != wei[p-i+1]) return 0;
}
return 1;
}
int main()
{
int n;//1000W 中 1W個迴文數左右
int p,q;
scanf("%d%d",&p,&q);
prime();
for(int i = 1; i<MAX; i++) {
sum1[i] = sum1[i-1];
if(isprime[i]) sum1[i]++;
}
for(int i = 1; i<MAX; i++) {
sum2[i] = sum2[i-1];
if(fit(i)) sum2[i]++;
}
int ans = -1;
for(int i = MAX-1; i>=0; i--) {
if(sum1[i] * q <= sum2[i] * p) {
ans = i;break;
}
}
if(ans == -1) puts("Palindromic tree is better than splay tree");
else printf("%d\n",ans);
return 0 ;
}