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

Examples
input
1 1
output
40
input
1 42
output
1
input
6 4
output
172
题意:不大于n的素数的个数<=A*不大于n的回文数的个数;求最大的n
思路:暴力;
#include<bits/stdc++.h>
using namespace std;
int a[100];
int check(int x)
{
    int ji=0;
    while(x)
    {
        a[ji++]=x%10;
        x/=10;
    }
    for(int i=0;i<ji/2;i++)
    {
        if(a[i]!=a[ji-1-i])
        return 0;
    }
    return 1;
}
int prime(int n)
{
    if(n<=1)
    return 0;
    if(n==2)
    return 1;
    if(n%2==0)
    return 0;
    int k, upperBound=n/2;
    for(k=3; k<=upperBound; k+=2)
    {
        upperBound=n/k;
        if(n%k==0)
            return 0;
    }
    return 1;
}
#define ll __int64
const int N=2e6+10;
ll p[N],h[N];
int main()
{
    for(ll i=1;i<=1200000;i++)
    {
        p[i]=p[i-1];
        h[i]=h[i-1];
        if(prime(i))
        p[i]++;
        if(check(i))
        h[i]++;
    }
    ll u,v;
    scanf("%I64d%I64d",&u,&v);
    for(int i=1200000;i>0;i--)
    if(v*p[i]<=u*h[i])
    {
        printf("%d\n",i);
        return 0;
    }
    printf("Palindromic tree is better than splay tree\n");
    return 0;
}

 

posted @ 2016-07-20 20:27  jhz033  阅读(156)  评论(0编辑  收藏  举报