暴力枚举(刷题)
来源:互联网 发布:一个字的网络歌曲 编辑:程序博客网 时间:2024/06/05 19:33
【例题1 Primes or Palindromes? CodeForces - 568A 】
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).
Example
Input
1 1
Output
40
Input
1 42
Output
1
Input
6 4
Output
172
AC代码:
#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int maxn=1e7+10;int prime[maxn];bool vis[maxn];int cntpri[maxn];int cntpal[maxn];void sieve(){ int n=maxn; memset(vis,0,sizeof(vis)); memset(cntpri,0,sizeof(cntpri)); int cnt=0; for(int i=2;i<n;i++) { cntpri[i]=cntpri[i-1];//此处记录数目方法很好 if(!vis[i]){prime[cnt++]=i;cntpri[i]++;} for(int j=0;j<cnt && i*prime[j]<n;j++) { vis[i*prime[j]]=1; if(i%prime[j]==0) break; } }}bool ispal(int n){ char s[10]; sprintf(s,"%d",n);//学习sprintf的用法,此处是把整数写到字符串中去 int len=strlen(s); for(int i=0;i<len/2;i++) if(s[i]!=s[len-1-i]) return false; return true;}int pal_num(){ int n=maxn; memset(cntpal,0,sizeof(cntpal)); for(int i=1;i<=n;i++) { cntpal[i]=cntpal[i-1]; if(ispal(i)) cntpal[i]++; }}int main(){ ios::sync_with_stdio(false); sieve(); pal_num(); int p,q; while(cin>>p>>q) { bool flag=0; for(int i=maxn-2;i>=1;i--) if((long long)q*cntpri[i]<=(long long)p*cntpal[i])//注意此处要用long long不然错误 { cout<<i<<endl; flag=1; break; } if(!flag) cout<<"Palindromic tree is better than splay tree"<<endl; } return 0;}
- 暴力枚举(刷题)
- HLJUOJ1122(暴力枚举)
- HDU2566(暴力枚举)
- hdu1015(暴力枚举。。。。)
- Friends (暴力枚举)
- Safecracker(枚举暴力)
- “暴力”枚举(穷举)
- hdu 4968(暴力枚举)
- 炸弹人(暴力枚举)
- 1262 - Password (暴力枚举)
- poj 3671(暴力 枚举)
- Friend-Graph(暴力枚举)
- 完美数列(暴力枚举)
- 暴力枚举
- 暴力枚举?
- 暴力枚举
- 暴力枚举
- 暴力枚举
- 习题 6.6 输出以下的杨辉三角形(要求输出10行)
- 城市列表集成sdk
- 补充上拉刷新下拉加载的url问题
- Storm架构和编程模型总结
- 颜色代码
- 暴力枚举(刷题)
- <a>标签中的href="javascript:;"
- 519_ImageView的src有间距
- C++11 Defaulted 和 Deleted 函数
- Java--正则表达式(六)
- Bootstrap小练习
- MyBatis学习总结(一)——MyBatis快速入门
- Single Number II leetcode java
- js模块化开发