题目链接:

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

题意:

问满足pi[n]/rub[n]<=p/q的最大的n是多少;

思路:

pi[i]和rub[i]都随着i的增大而增大,且pi[i]/rub[i]的值也随着增大,(小于10的数特殊);p/q给有范围,可以算一下大约1200000时pi[i]/rub[i]已经大约42了;所以暴力找到那个最大的n;

AC代码:
/*2014300227    569C - 28    GNU C++11    Accepted    61 ms    14092 KB*/
#include <bits/stdc++.h>
using namespace std;
const int N=12e5+;
typedef long long ll;
const double PI=acos(-1.0);
int p,q,pi[N],vis[N],rub[N];
void get_pi()//素数筛+dp得到pi[i]
{
memset(pi,,sizeof(pi));
pi[]=;
for(int i=;i<N;i++)
{
if(!pi[i])
{
for(int j=;j*i<N;j++)
{
pi[i*j]=;
}
pi[i]=pi[i-]+;
}
else pi[i]=pi[i-];
}
}
int is_pal(int x)//判断一个数是不是回文数;
{
int s=,y=x;
while(y)
{
s*=;
s+=y%;
y/=;
}
if(s==x)return ;
return ;
}
void get_rub()
{
rub[]=;
for(int i=;i<N;i++)
{
if(is_pal(i))rub[i]=rub[i-]+;
else rub[i]=rub[i-];
}
}
int check(int x)
{
if(pi[x]*q<=p*rub[x])return ;
return ; }
int get_ans()
{
int ans=;
for(int i=;i<N;i++)
{
if(check(i))ans=i;
}
if(ans==)printf("Palindromic tree is better than splay tree\n");
else printf("%d\n",ans);
}
int main()
{
get_pi();
get_rub();
//cout<<pi[1200000]*1.0/(rub[1200000]*1.0);
scanf("%d%d",&p,&q);
get_ans(); return ;
}

codeforces 569C C. Primes or Palindromes?(素数筛+dp)的更多相关文章

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

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. codeforces 414A A. Mashmokh and Numbers(素数筛)

    题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...

  3. Codeforces Round #315 (Div. 2C) 568A Primes or Palindromes? 素数打表+暴力

    题目:Click here 题意:π(n)表示不大于n的素数个数,rub(n)表示不大于n的回文数个数,求最大n,满足π(n) ≤ A·rub(n).A=p/q; 分析:由于这个题A是给定范围的,所以 ...

  4. Codeforces Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

  5. Codeforces Round #315 (Div. 1) A. Primes or Palindromes? 暴力

    A. Primes or Palindromes?Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3261 ...

  6. 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 st ...

  7. Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

    传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...

  8. codeforces 822 D. My pretty girl Noora(dp+素数筛)

    题目链接:http://codeforces.com/contest/822/problem/D 题解:做这题首先要推倒一下f(x)假设第各个阶段分成d1,d2,d3...di组取任意一组来说,如果第 ...

  9. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

随机推荐

  1. Oracle 字段类型

    Oracle 字段类型 http://www.cnblogs.com/lihan/archive/2010/01/06/1640547.html 字段类型 描述 字段长度及其缺省值 CHAR (siz ...

  2. 王立平--eclipse中改动android项目的版本

    改动版本 1.右键-->properties 2.android.改动须要的版本 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTU ...

  3. linux安装svn客户端subversion及使用方法

    1.下载 [maintain@HM16-213 software]$ wget http://subversion.tigris.org/downloads/subversion-deps-1.6.1 ...

  4. Linux学习日志--共享内存

    一:什么是共享内存             共享内存是属于IPC(Inter-Process Communication进程间通信)机制,其它两种是信号量和消息队列,该机制为进程开辟创建了特殊的地址范 ...

  5. 开始nodejs+express的学习+实践(1)

    开始nodejs+express的学习+实践(1) 开始nodejs+express的学习+实践(2) 开始nodejs+express的学习+实践(3) 开始nodejs+express的学习+实践 ...

  6. 修复open-ssl漏洞,升级open-ssl版本

    升级openssl环境至openssl-1.0.1g 1.查看源版本 [root@zj ~]# openssl version -a OpenSSL 0.9.8e-fips-rhel5 01 Jul ...

  7. &lt;二代測序&gt; 下载 NCBI sra 文件

    本文近期更新地址: http://blog.csdn.net/tanzuozhev/article/details/51077222 随着測序技术的不断提高.二代測序数据成指数增长. NCBI提供了S ...

  8. 【Android】带底部指示的自定义ViewPager控件

    在项目中经常需要使用轮转广告的效果,在android-v4版本中提供的ViewPager是一个很好的工具,而一般我们使用Viewpager的时候,都会选择在底部有一排指示物指示当前显示的是哪一个pag ...

  9. 【转】cmd chcp命令切换字符格式

    cmd chcp命令切换字符格式   命令介绍:   chcp 65001   #换成utf-8代码页   chcp 936       #换成默认的gbk   chcp 437       #美国英 ...

  10. WCF服务返回XML或JSON格式数据

    第一种方式public string GetData( string format) { string res = null; Student stu = new Student { StuID = ...