A. 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 nrub(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 pq,
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不大

#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?-暴力推断)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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是给定范围的,所以 ...

  4. 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( ...

  5. codeforces 569C C. Primes or Palindromes?(素数筛+dp)

    题目链接: C. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes in ...

  6. Uva-oj Palindromes 暴力

     Palindromes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  7. 【34.88%】【codeforces 569C】Primes or Palindromes?

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. Codeforces Round #315 (Div. 2)——C. Primes or Palindromes?

    这道题居然是一个大暴力... 题意: π(n):小于等于n的数中素数的个数 rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,当中A=p/q. 然后要你找到对于给定的A.找到使得 ...

  9. C. Primes or Palindromes?

    prime numbers non greater than n is about . We can also found the amount of palindrome numbers with ...

随机推荐

  1. cygin常用命令

    基本操作命令: ---------------------------------------------------------------------- --------------------- ...

  2. PAT Basic 1037

    1037 在霍格沃茨找零钱 如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一 ...

  3. python基础学习笔记——模块

    自定义模块 我们今天来学习一下自定义模块(也就是私人订制),我们要自定义模块,首先就要知道什么是模块啊 一个函数封装一个功能,比如现在有一个软件,不可能将所有程序都写入一个文件,所以咱们应该分文件,组 ...

  4. 开始 python programming第三版案例分析

    最近研究python,打算将python programming第三版案例分析下 但是全书1600多页 比较费时 而且 介绍太多 感觉没有必要! python programming 堪称经典之作 第 ...

  5. Laya for...in和for each...in

    当for...in和for each...in同时作用于一个对象时,for...in 获取的是key, for each...in获取的是value for each(var i in loadInf ...

  6. Python MySQLdb的execute和executemany的使用

    如果使用executemany对数据进行批量插入的话,要注意一下事项: conn = MySQLdb.connect(host = “localhost”, user = “root”, passwd ...

  7. POJ 1038 Bugs Integrated, Inc. ——状压DP

    状态压缩一下当前各格子以及上面总共放了几块,只有012三种情况,直接三进制保存即可. 然后转移的时候用搜索找出所有的状态进行转移. #include <map> #include < ...

  8. BZOJ 2720 [Violet 5]列队春游 ——期望DP

    很喵的一道题(我可不是因为看了YOUSIKI的题解才变成这样的) $ans=\sum_{x<=n}\sum_{i<=n} iP(L=i)$ 其中P(x)表示视线为x的概率. 所以只需要求出 ...

  9. BZOJ 4568 [Scoi2016]幸运数字 ——线性基 倍增

    [题目分析] 考虑异或的最大值,维护线性基就可以了. 但是有多次的询问,树剖或者倍增都可以. 想了想树剖动辄数百行的代码. 算了,我还是写倍增吧. 注:被位运算和大于号的优先级坑了一次,QaQ [代码 ...

  10. 刷题总结——火柴排队(NOIP2013)

    题目: 题目背景 NOIP2013 提高组 Day1 试题 题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间 ...