Codeforces Round #315 (Div. 2) C. Primes or Palindromes? (打表枚举)

C. Primes or Palindromes?
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 nrub(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 pq, 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).

Sample test(s)
input
1 1
output
40
input
1 42
output
1
input
6 4
output
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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值