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
,
He asked you to solve the following problem: for a given value of the coefficient A find the maximum
Input
The input consists of two positive integers
p
,
Output
If such maximum number exists, then print it. Otherwise, print “Palindromic tree is better than splay tree” (without the quotes).
Sample test(s)
input
1 1
output
40
input
1 42
output
1
input
6 4
output
172
题意
一种数字叫回文数,就是长的回文的数。。。
π(n)
就是从
1
到
题解
先暴力跑一发,发现
π(n)
的增长速度整体上是大于
rub(n)
的,能跑出这他们的比值为42时对应最大的
n
是小于
AC代码
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int p=0,r=0;
int x,y,g;
int ip[1180000];
int ir[1180000];
int ans;
bool is_r(int x)
{
int t=x;
int y=0;
while(t)
{
y*=10;
y+=t%10;
t/=10;
}
return x==y;
}
int gcd(int a,int b)
{
return (b>0)?gcd(b,a%b):a;
}
int main()
{
scanf("%d%d",&x,&y);
g=gcd(x,y);
x/=g;
y/=g;
memset(ip,0,sizeof ip);
memset(ir,0,sizeof ir);
ip[1]=1;
for(int i=2;i<=1179999;i++)
{
if(!ip[i])
{
for(int j=i+i;j<=1179999;j+=i)
ip[j]=1;
}
}
for(int i=1;i<=1179999;i++)
{
if(ip[i]==0)
p++;
if(is_r(i))
r++;
if(x*r>=y*p)
ans=i;
}
printf("%d\n",ans);
return 0;
}