链接:戳这里
C. Primes or Palindromes?
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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).
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
题意:
π(n) ≤ A·rub(n).
π(n) 表示1~n内的素数个数
rub(n). 表示1~n内的回文数个数
使得 π(n) ≤ A·rub(n). 等式成立 这个A是(1/42~42)
暴力找发现在 1178707 超过了42
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int p,q;
int vis[3000100],prime[3000100];
int sum[3000100],num[3000100];
bool pd(int x){
int anw[10];
int cnt=0;
while(x){
anw[++cnt]=x%10;
x/=10;
}
for(int i=1;i<=cnt;i++){
if(anw[i]!=anw[cnt-i+1]) return false;
}
return true;
}
int main(){
int cnt=0;
vis[1]=1;
for(int i=2;i<=3000000;i++){
if(!vis[i]) prime[cnt++]=i;
for(int j=0;j<cnt;j++){
if(i*prime[j]>3000000) break;
vis[i*prime[j]]=1;
if(i%prime[j]==0) {
break;
}
}
}
for(int i=1;i<=3000000;i++){
if(vis[i]) sum[i]=sum[i-1];
else sum[i]=sum[i-1]+1;
}
for(int i=1;i<=3000000;i++){
if(pd(i)) num[i]=num[i-1]+1;
else num[i]=num[i-1];
}
scanf("%d%d",&p,&q); /// p/q
for(int i=3000000;i>=1;i--){
ll x=(ll)q*sum[i],y=(ll)p*num[i];
if(x<=y){
cout<<i<<endl;
return 0;
}
}
printf("Palindromic tree is better than splay tree\n");
return 0;
}