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 find the maximum n, such that π(n) ≤ A·rub(n).
The input consists of two positive integers p, q, the numerator and denominator of the fraction that is the value of A (, ).
If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes).
1 1
40
1 42
1
6 4
172
π(i) 表示小于i的素数个数 rub(i)表示小于 i 的回文个数 A = p / q;
求 满足 π(i) ≤ A·rub(i) 的i 的最大值
A的最大值为42 素数的增长远远大于回文数增长个数,打了150w的素数表 和 回文数表 就行了
最后直接判断 q * π(i) ≤ p·rub(i) 即可
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <stdlib.h> #include <algorithm> #define N 1500000 using namespace std; int su[N]; bool u[N]; int n[N]; void prime() { memset(u,true,sizeof(u)); int s=1; u[1] = false; for(int i=2; i<N; i++) //素数表 欧拉筛法 { if(u[i]) su[s++]=i; for(int j=1; j<s; j++) { if(i*su[j]>N) break; u[i*su[j]]=false; if(i%su[j]==0) break; } } int sum=0; for(int i=1;i<N;i++) { if(u[i]) sum++; n[i]=sum; //小于i的宿舍个数 } } bool judge(int x) //判断是否为回文数 { int a[15]; int c=1; while(x) { a[c++] = x%10; x /=10; } int r = c/2; for(int i=1;i<=r;i++) { if(a[i] != a[c-i]) return false; } return true; } int rub[N]; void huiwen() { int sum=0; //记录回文数的个数 for(int i=1;i<N;i++) { if(i<10 || judge(i)) { sum++; } rub[i]=sum; //小于i的回文数个数 } } int main() { prime(); //素数 huiwen(); //回文数 int p ,q; while(~scanf("%d%d",&p,&q)) { int ans=1; for(int i=N-1; i>=1;i--) { if(n[i]*q <= rub[i]*p) { ans=i; break; } } printf("%d\n",ans); } return 0; }