Codeforces Round #315 -Primes or Palindromes?(回文&&素数)
来源:互联网 发布:意大利帅哥知乎 编辑:程序博客网 时间:2024/06/05 00:28
题目地址:Primes or Palindromes?
题意:给出p,q,A=p/q,求找出最大的n使得n以内的素数的个数<=A*n以内的回文串的个数。
思路:其实就是n以内的素数的个数*q<=n以内的回文串的个数*p。然而p/q的最大值为42,那么数到13000000就到达极限,所以我们直接可以从大的开始往小的一步一步的暴力枚举就可以(感觉窝还是太年轻。。QAQ
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma comment(linker, "/STACK:102400000,102400000")using namespace std;typedef __int64 LL;const int inf=0x3f3f3f3f;const double pi= acos(-1.0);const double esp=1e-7;const int maxn=2*1e6+10;int npal[maxn],a[30];int pri[maxn],npri[maxn];int is_prime(){ memset(pri,0,sizeof(pri)); for(int i=2;i<maxn;i++) { if(!pri[i]) { for(int j=i*2;j<maxn;j+=i) { pri[j]=1; } } }}int is_pal(int x){ int cnt=0; while(x){ a[cnt++]=x%10; x/=10; } for(int i=0;i<cnt/2;i++) { if(a[i]!=a[cnt-i-1]) return 0; } return 1;}void init(){ memset(npri,0,sizeof(npri)); memset(npal,0,sizeof(npal)); for(int i=2;i<maxn;i++) { npri[i]=npri[i-1]; if(!pri[i]) npri[i]++; } for(int i=1;i<maxn;i++) { npal[i]=npal[i-1]+is_pal(i); }}int main(){ is_prime(); init(); int p,q; while(~scanf("%d %d",&p,&q)) { int flag=0; for(int i=maxn-1;i>=0;i--){ if(q*npri[i]<=p*npal[i]) { flag=1; printf("%d\n",i); break; } } if(!flag) { puts("Palindromic tree is better than splay tree"); } } return 0;}
1 0
- Codeforces Round #315 -Primes or Palindromes?(回文&&素数)
- Codeforces Round #315 (Div. 2) C. Primes or Palindromes? 素数表 回文数
- Codeforces Round #315 (Div. 2) C. Primes or Palindromes? (素数打表 回文数)
- CodeForces 568A. Primes or Palindromes?( 素数 + 回文数 )
- C. Primes or Palindromes?-Codeforces Round #315 (Div. 2)
- Codeforces Round #315 (Div. 2) C. Primes or Palindromes?
- Codeforces 568 A Primes or Palindromes?(求素数个数+判断是否是回文数)
- Primes or Palindromes? CodeForces
- CodeForces #315 (div1) A.Primes or Palindromes?
- Codeforces Round #315 (Div. 2)569C Primes or Palindromes?(预处理)
- Codeforces Round #315 (Div. 2) C. Primes or Palindromes? (打表枚举)
- Codeforces Round #315 (Div. 2)——C. Primes or Palindromes?
- Codeforces Round #315 (Div. 2) C - Primes or Palindromes?(暴力打表)
- Codeforces 568A Primes or Palindromes?
- CodeForces 568A Primes or Palindromes?
- Codeforces 569C Primes or Palindromes?
- 【34.88%】【codeforces 569C】Primes or Palindromes?
- 回文素数Prime Palindromes
- 网络流
- abap chain endchain
- 数据库之Case When
- 基于消息机制的异步架构之回调函数注册
- LintCode-和大于S的最小子数组
- Codeforces Round #315 -Primes or Palindromes?(回文&&素数)
- tableView从后往前传值
- Android项目中如何用好构建神器Gradle?
- 数据结构实验之链表五:单链表的拆分
- 30-CSS-03-CSS(基本选择器&优先级)
- 【转】在shell中捕捉信号的trap命令
- SolrCloud中的文件与Collection管理
- 数据结构(Java语言)——LinkedList简单实现
- 新的开始2015.8.11