codeforces 569 C Primes or Palindromes? (暴力)
2015-08-15 05:41
351 查看
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 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).
Sample test(s)
input
output
input
output
input
output
先预处理出来比小于等于n的素数的个数和回文数的个数...
素数不筛竟然也可以过
然后暴力就好.
需要注意的是,比值并不单调,找最大的n,可能之前某个数开始不满足条件,之后又有满足条件的了.
我们可以倒着扫来找,第一个满足条件的就是最大的.
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 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).
Sample test(s)
input
1 1
output
40
input
1 42
output
1
input
6 4
output
172
先预处理出来比小于等于n的素数的个数和回文数的个数...
素数不筛竟然也可以过
然后暴力就好.
需要注意的是,比值并不单调,找最大的n,可能之前某个数开始不满足条件,之后又有满足条件的了.
我们可以倒着扫来找,第一个满足条件的就是最大的.
/************************************************************************* > File Name: code/cf/#315/C.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年08月11日 星期二 00时54分13秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #define y0 abc111qqz #define y1 hust111qqz #define yn hez111qqz #define j1 cute111qqz #define tm crazy111qqz #define lr dying111qqz using namespace std; #define REP(i, n) for (int i=0;i<int(n);++i) typedef long long LL; typedef unsigned long long ULL; const int inf = 0x7fffffff; const int N=2E6+5; int f ,g ; bool prime( int x) { if (x==1) return false; if (x<=3) return true; for ( int i = 2 ; i*i<=x ; i ++) { if (x%i==0) return false; } return true; } bool pal( int x) { int a[100]; int len=0; while(x) { len++; a[len]=x%10; x = x/ 10; } for ( int i = 1 ;i <= len/2 ; i++) { if (a[i]!=a[len+1-i]) return false; } return true; } int main() { int cnt = 0; memset(f,0,sizeof(f)); memset(g,0,sizeof(g)); for (int i = 1 ; i <= N-5 ; i ++) { if (prime(i)) { cnt++; // pri[cnt] = i; } f[i] = cnt; } int cnt2 = 0 ; for ( int i =1 ; i <=N-5 ; i++) { if (pal(i)) { cnt2++; } g[i] = cnt2; } // for ( int i =1; i <= N-5; i ++) // { // cout<<"i:"<<i<<"f[i]:"<<f[i]<<" g[i]:"<<g[i]<<"f[i]/g[i]:"<<f[i]*1.0/g[i]*1.0<<" g[i]/f[i]"<<g[i]*1.0/f[i]<<endl; // } int p,q; int i = 0; scanf("%d %d",&q,&p); double r = p*1.0/q; for ( int i = N-5 ; i >= 1 ; i--) { if (r*f[i]<=g[i]) { cout<<i<<endl; break; } } return 0; }
相关文章推荐
- linux下的nfc开发
- codeforces 570 E. Pig and Palindromes (dp)
- CF316D
- CC150-Array and String 1.3
- Win32内存结构
- codeforces 570 D. Tree Requests (dfs序)
- 开源Word读写组件DocX 的深入研究和问题总结
- Java读取Properties文件并不会出现中文乱码
- 黑马程序员——面向对象(Static关键字+main函数)-第12天
- java第一章学习总结
- 【原创】express3.4.8源码解析之路由中间件
- 【原创】express3.4.8源码解析之中间件
- 反射调用方法和反射时传递带参数的构造器
- 【Qt】]两种解决Qt5显示中文乱码的方法
- 第一个Android程序——计算器
- poj 1769 Minimizing maximizer(dp+线段树)
- 如何快速清除无用的css
- Kafka 学习笔记(十一)关于Partition
- C++ 继承与派生(二)
- 世界上最富有的前十个国家