codeforces 569C C. Primes or Palindromes?(素数筛+dp)
2016-04-12 22:51
405 查看
题目链接:C. Primes or Palindromes?time limit per test
3 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputRikhail 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).Examplesinput
3 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputRikhail 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).Examplesinput
1 1output
40input
1 42output
1input
6 4output
172 题意: 问满足pi /rub <=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+4; typedef long long ll; const double PI=acos(-1.0); int p,q,pi ,vis ,rub ; void get_pi()//素数筛+dp得到pi[i] { memset(pi,0,sizeof(pi)); pi[1]=0; for(int i=2;i<N;i++) { if(!pi[i]) { for(int j=2;j*i<N;j++) { pi[i*j]=1; } pi[i]=pi[i-1]+1; } else pi[i]=pi[i-1]; } } int is_pal(int x)//判断一个数是不是回文数; { int s=0,y=x; while(y) { s*=10; s+=y%10; y/=10; } if(s==x)return 1; return 0; } void get_rub() { rub[0]=0; for(int i=1;i<N;i++) { if(is_pal(i))rub[i]=rub[i-1]+1; else rub[i]=rub[i-1]; } } int check(int x) { if(pi[x]*q<=p*rub[x])return 1; return 0; } int get_ans() { int ans=0; for(int i=1;i<N;i++) { if(check(i))ans=i; } if(ans==0)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 0; }
相关文章推荐
- Java获取 JVM 运行信息
- 信管师培训之第十一节课作业(法律法规+标准规范+职业道德)
- 关于Qt使用MySQL的体会
- View绘制流程(3)---scroller 和 scrollTo srollBy
- 禅道邮箱配置163邮箱/阿里云邮箱区别
- 练习五 数组转换成字符串
- 练习12,去除ArrayList中重复的字符串内容
- 奇妙的图像处理算法
- 类和对象 三角形(4)
- 欢迎使用CSDN-markdown编辑器
- c++第三次作业
- 如何把自己打造成技术圈的 papi 酱
- 20159311《网络攻击与防范》第六周学习总结
- 笨笨的小猪
- java:解决在nextInt()后使用nextLine()的方法
- Android学习笔记之AndroidManifest.xml文件解析
- java上机课代码。。。重写equals方法,toString方法。。。
- web获取URL相关信息
- JDK环境变量配置及Tomcat安装服务
- HttpContext及HttpContext.current