CF 568A(Primes or Palindromes?-暴力推断)
3 seconds
256 megabytes
standard input
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).
The input consists of two positive integers p, q,
the numerator and denominator of the fraction that is the value of A (, ).
If such maximum number exists, then print it. Otherwise, print "Palindromic tree is better than splay tree" (without the quotes).
1 1
40
1 42
1
6 4
172
能够发现不可能无解,极限情况n不大
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
bool is_prime(int x)
{
if (x==1) return 0;
Fork(i,2,sqrt(x))
{
if (x%i==0) return 0;
}
return 1;
}
const int MAXN =10000000;
int P[MAXN],siz=0,b[MAXN]={0};
void make_prime(int n)
{
Fork(i,2,n)
{
if (!b[i])
{
P[++siz]=i;
}
For(j,siz)
{
if (P[j]*i>n) break;
b[P[j]*i]=1;
if (i%P[j]==0) break;
}
}
}
bool is_pal(int x)
{
char s[10];
sprintf(s,"%d",x);
int p=0,q=strlen(s)-1;
while(p<q) if (s[p]!=s[q]) return 0;else ++p,--q;
return 1;
} bool B[MAXN]={0};
bool make_pal(int n)
{
char s[20];
For(i,10000)
{ sprintf(s,"%d",i);
int m=strlen(s);
int p=m-1;
for(int j=m;p>-1;j++,p--) s[j]=s[p]; int x;
sscanf(s,"%d",&x);
if (x<=n) B[x]=1; for(int j=m;j<=2*m-1;j++) s[j]=s[j+1];
sscanf(s,"%d",&x);
if (x<=n) B[x]=1; }
} int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);
int p,q;
cin>>p>>q;
make_prime(MAXN-1);
make_pal(MAXN-1);
int x1=0,x2=0,n=MAXN-1,ans=1,t=1;
For(i,n)
{
if (i==P[t]) x1++,t++;
if (B[i]) x2++;
if ((ll)(x1)*q<=(ll)(x2)*p) ans=i;
}
cout<<ans<<endl;
return 0;
}
CF 568A(Primes or Palindromes?-暴力推断)的更多相关文章
- 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 ...
- 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 ...
- 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是给定范围的,所以 ...
- codeforces 568a//Primes or Palindromes?// Codeforces Round #315 (Div. 1)
题意:求使pi(n)*q<=rub(n)*p成立的最大的n. 先收集所有的质数和回文数.质数好搜集.回文数奇回文就0-9的数字,然后在头尾添加一个数.在x前后加a,就是x*10+a+a*pow( ...
- codeforces 569C C. Primes or Palindromes?(素数筛+dp)
题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...
- Uva-oj Palindromes 暴力
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- 【34.88%】【codeforces 569C】Primes or Palindromes?
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #315 (Div. 2)——C. Primes or Palindromes?
这道题居然是一个大暴力... 题意: π(n):小于等于n的数中素数的个数 rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,当中A=p/q. 然后要你找到对于给定的A.找到使得 ...
- C. Primes or Palindromes?
prime numbers non greater than n is about . We can also found the amount of palindrome numbers with ...
随机推荐
- google F12
谷歌浏览器(Google Chrome)开发调试详细介绍 博客分类: 前端 浏览器chromegoogle调试开发 很多Web前台开发者都喜欢这种浏览器自带的开发者工具,这对前台设计.代码调试很大帮 ...
- redux学习总结
redux学习总结 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !imp ...
- 七、docker基本命令
Docker 基本命令 docker的基本命令 docker version :查看docker的版本号,包括客户端.服务端.依赖的Go等 [root@centos7 ~]# docker versi ...
- [uiautomator篇] 找父亲节点和其他兄弟节点
https://testerhome.com/topics/1250 Appium [已解决] UiSelector 如何根据节点定位到父节点 / 兄弟节点? liqing380 · 发布于 2014 ...
- BZOJ 1016 [JSOI2008]最小生成树计数 ——Matrix-Tree定理
考虑从小往大加边,然后把所有联通块的生成树个数计算出来. 然后把他们缩成一个点,继续添加下一组. 最后乘法原理即可. 写起来很恶心 #include <queue> #include &l ...
- SPOJ QTREE Query on a tree V ——动态点分治
[题目分析] QTREE4的弱化版本 建立出分治树,每个节点的堆表示到改点的最近白点距离. 然后分治树上一直向上,取min即可. 正确性显然,不用担心出现在同一子树的情况(不会是最优解),请自行脑补. ...
- [luoguP1053] 篝火晚会(贪心 + 乱搞)
传送门 假设第一个位置是1,那么枚举它的左右两边是谁,有两种情况,然后可以递推求出序列. 然后可以贪心,两个序列有多少个不同的数,答案就是多少,具体为啥,yy一下即可 然后就是判断递推求出的序列和目标 ...
- VB6 post图片
在VBA中怎样用XMLhttp 模拟http上传二进制文件? https://www.zhihu.com/question/40974557 作者:付杨 链接:https://www.zhihu.co ...
- 【noip】noip201503求和(题解可能不完美,但绝对详细)
3. 求和 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 题目描述 一条狭长的纸带被均匀划分出了n个格子,格子编号从1到n.每个格子 ...
- 【bzoj1299】[LLH邀请赛]巧克力棒 博弈+模拟
Description TBL和X用巧克力棒玩游戏.每次一人可以从盒子里取出若干条巧克力棒,或是将一根取出的巧克力棒吃掉正整数长度.TBL先手两人轮流,无法操作的人输. 他们以最佳策略一共进行了10轮 ...