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;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

Primes or Palindromes? CodeForces - 569C

http://codeforces.com/problemset/problem/569/C 神奇的暴力,注意大数相乘可能爆ll #include #include #include #inclu...
  • qq_36424540
  • qq_36424540
  • 2017年11月01日 00:08
  • 32

【34.88%】【codeforces 569C】Primes or Palindromes?

time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandar...
  • harlow_cheng
  • harlow_cheng
  • 2016年12月08日 22:22
  • 142

Codeforces 464A No to Palindromes!(构造)

题目链接:Codeforces 464A No to Palindromes! 题目大意:给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,并 ...
  • u011328934
  • u011328934
  • 2014年09月08日 16:59
  • 1141

Codeforces Round #315 (Div. 2)569C Primes or Palindromes?(预处理)

C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in...
  • GKHack
  • GKHack
  • 2015年08月11日 16:15
  • 1228

Codeforces Round #316 (Div. 2)E. Pig and Palindromes(dp好题)

E. Pig and PalindromesPeppa the Pig was walking and walked into the forest. What a strange coinciden...
  • u010660276
  • u010660276
  • 2015年08月16日 10:30
  • 663

CodeForces 568A Primes or Palindromes?

C - Primes or Palindromes? Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...
  • yao1373446012
  • yao1373446012
  • 2016年06月25日 20:28
  • 230

codeforces 316 D

D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes ...
  • DoJintian
  • DoJintian
  • 2015年08月19日 11:14
  • 481

Codeforces 568A Primes or Palindromes?

Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too ...
  • XDU_PYL
  • XDU_PYL
  • 2015年09月28日 09:53
  • 276

CodeForces570E - Pig and Palindromes 【DP】

#include #include #include #include #include using namespace std; const int mod = 1e9+7; int n,m,ste...
  • qq_33583069
  • qq_33583069
  • 2016年10月01日 22:03
  • 150

Codeforces Round #316 (Div. 2) E. Pig and Palindromes DP

500*500的矩阵,从(1,1)走到(n,m),求走出一条回文道路的方案数。 要求回文从起点s和终点t若相同则可以走,并且走(n+m-2)/2步相遇,每一步的状态只与上一步有关,两个数组轮换记录状...
  • u013044116
  • u013044116
  • 2015年08月16日 01:16
  • 428
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces 569C Primes or Palindromes?
举报原因:
原因补充:

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