Codeforces 568 A Primes or Palindromes?(求素数个数+判断是否是回文数)

A. 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


没什么好说的 当时做的时候以为关系有单调性 还以为要用二分。。其实数到13000000就达到那个42倍关系的极限了  所以直接从大的开始暴力枚举就能求出答案  另外不存在没有答案的情况


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1300000
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

int isPalindrome(int x)
{
  //if (x < 0) return false;
  int div = 1;
  while (x / div >= 10) {
    div *= 10;
  }
  while (x != 0) {
    int l = x / div;
    int r = x % 10;
    if (l != r) return 0;
    x = (x % div) / 10;
    div /= 100;
  }
  return 1;
}  //回文数的判断

int pri[MAXN];
void init()
{
    for(int i=0;i<MAXN;i++)
        pri[i]=1;
    pri[0]=pri[1]=0;
    for(int i=2;i<MAXN;i++)
    {
        if(pri[i])
        {
            if(i>MAXN/i) continue;
            for(int j=i*i;j<MAXN;j+=i)
                pri[j]=0;
        }
    }
    for(int i=1;i<MAXN;i++)
        pri[i]+=pri[i-1];
//    for(int i=1;i<=100;i++)
//        cout<<"i  "<<i<<"  "<<pri[i]<<endl;
}
int a[MAXN];

int main()
{
    //fread;
    init();
    MEM(a,0);
    for(int i=1;i<MAXN;i++)
        a[i]=isPalindrome(i);
    for(int i=1;i<MAXN;i++)
        a[i]+=a[i-1];
    int p,q;
    while(scanf("%d%d",&p,&q)!=EOF)
    {
        int ans;
        for(int i=MAXN-1;i>=1;i--)
        {
            if(q*pri[i]<=p*a[i])
            {
                ans=i;
                break;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值