# Codeforces 569C Primes or Palindromes?

http://codeforces.com/problemset/problem/569/C

C. Primes or Palindromes?
time limit per test3 seconds
memory limit per test 256 megabytes

Problem description
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

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
using namespace std;

const int maxm=2500000+5;
int PI[maxm],rub[maxm];
int isPrime[maxm];

int pal(int x)            //判断回文数
{
int a[15],t=0,len,i;
while(x)
{
a[t++]=x%10;
x=x/10;
}
len=t-1;

for(i=0;i<=len/2;i++)
{
if(a[i]!=a[len-i])
return 0;
}
return 1;
}

int main()
{

int i;
for(i=1;i<maxm;i++)
isPrime[i]=1;
for(i=2;i<maxm;i++)       //判断是否是质数
{
if(isPrime[i]==1)
{
for(int j=i+i;j<maxm;j+=i)
isPrime[j]=0;
}
}

PI[1]=0;
for(i=2;i<maxm;i++)        //预处理质数个数
{
PI[i]=PI[i-1]+isPrime[i];
}

rub[1]=1;
for(i=2;i<maxm;i++)         //预处理回文数个数
{
int c=pal(i);
rub[i]=rub[i-1]+c;
}

int p,q;
while(scanf("%d%d",&p,&q)!=EOF)
{
double A=(double)p/(double)q;
for(i=maxm-1;i>=1;i--)
{
if(PI[i]<=A*rub[i])
{
printf("%d\n",i);
break;
}
}
if(i<1)
printf("Palindromic tree is better than splay tree\n");
}
return 0;
}

• 本文已收录于以下专栏：

## 【34.88%】【codeforces 569C】Primes or Palindromes?

time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandar...

## Codeforces Round #315 (Div. 2) C. Primes or Palindromes?

C. Primes or Palindromes?time limit per test3 seconds memory limit per test256 megabytes inputstan...

## 【codeforces237C】Primes on Interval

C. Primes on Interval  time limit per test1 second  memory limit per test256 megabytes  inputstan...

## Codeforces 237C：Primes on Interval（素数打表+二分）

C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes ...

## CodeForces-237C- Primes on Interval

E - E Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u SubmitStatusPr...

## Codeforces 465C No to Palindromes!【暴力+思维】

A. No to Palindromes! time limit per test 1 second memory limit per test 256 megabytes input stan...

## Codeforces Round #265 (Div. 2) C.No to Palindromes!

C. No to Palindromes! time limit per test 1 second memory limit per test 256 megabytes ...

## Codeforces Round #265 (Div. 2) C. No to Palindromes!(字符串+构造??)

C. No to Palindromes! time limit per test 1 second memory limit per test 256 megabytes input s...

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)