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

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)Arub(n) .

Input

The input consists of two positive integers p , q, the numerator and denominator of the fraction that is the value of A(A=pq,p,q<104142pq42).

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) 就是回文数的个数,然后输入 p ,q,问令 π(n)pqrub(n) 成立的最大的n。

题解

先暴力跑一发,发现 π(n) 的增长速度整体上是大于 rub(n) 的,能跑出这他们的比值为42时对应最大的 n 是小于1180000的(具体多少忘了。。。),然后就暴力出一个素数筛,一个回文筛。。。从头暴力到尾就好。。。(怎么又是暴力。。。)

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值