Codeforces 569C Primes or Palindromes?

原创 2016年08月29日 20:45:56

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

题目大意: n以内的质数为π(n),n以内的回文数为rub(n),求满足π(n) ≤ A·rub(n)这个式子最大的n,如果不存在就输出Palindromic tree is better than splay tree。

题目思路:预处理

#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...
  • a456052
  • a456052
  • 2015年08月11日 11:05
  • 195

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

这道题竟然是一个大暴力。。。 题意:  π(n):小于等于n的数中素数的个数  rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,其中A=p/q; 然后要你找到对...

Codeforces Round #315 -Primes or Palindromes?(回文&&素数)

题目地址:Primes or Palindromes? 题意:给出p,q,A=p/q,求找出最大的n使得n以内的素数的个数...

【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 ...
  • zugofn
  • zugofn
  • 2016年10月18日 23:18
  • 164

CodeForces-237C- Primes on Interval

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

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 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 input s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces 569C Primes or Palindromes?
举报原因:
原因补充:

(最多只允许输入30个字)