codeforces 569C C. Primes or Palindromes?(素数筛+dp)
题目链接:
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 题意: 问满足pi[n]/rub[n]<=p/q的最大的n是多少; 思路: pi[i]和rub[i]都随着i的增大而增大,且pi[i]/rub[i]的值也随着增大,(小于10的数特殊);p/q给有范围,可以算一下大约1200000时pi[i]/rub[i]已经大约42了;所以暴力找到那个最大的n; AC代码:
/*2014300227 569C - 28 GNU C++11 Accepted 61 ms 14092 KB*/
#include <bits/stdc++.h>
using namespace std;
const int N=12e5+;
typedef long long ll;
const double PI=acos(-1.0);
int p,q,pi[N],vis[N],rub[N];
void get_pi()//素数筛+dp得到pi[i]
{
memset(pi,,sizeof(pi));
pi[]=;
for(int i=;i<N;i++)
{
if(!pi[i])
{
for(int j=;j*i<N;j++)
{
pi[i*j]=;
}
pi[i]=pi[i-]+;
}
else pi[i]=pi[i-];
}
}
int is_pal(int x)//判断一个数是不是回文数;
{
int s=,y=x;
while(y)
{
s*=;
s+=y%;
y/=;
}
if(s==x)return ;
return ;
}
void get_rub()
{
rub[]=;
for(int i=;i<N;i++)
{
if(is_pal(i))rub[i]=rub[i-]+;
else rub[i]=rub[i-];
}
}
int check(int x)
{
if(pi[x]*q<=p*rub[x])return ;
return ; }
int get_ans()
{
int ans=;
for(int i=;i<N;i++)
{
if(check(i))ans=i;
}
if(ans==)printf("Palindromic tree is better than splay tree\n");
else printf("%d\n",ans);
}
int main()
{
get_pi();
get_rub();
//cout<<pi[1200000]*1.0/(rub[1200000]*1.0);
scanf("%d%d",&p,&q);
get_ans(); return ;
}
codeforces 569C C. Primes or Palindromes?(素数筛+dp)的更多相关文章
- 【34.88%】【codeforces 569C】Primes or Palindromes?
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- codeforces 414A A. Mashmokh and Numbers(素数筛)
题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...
- 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 Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)
题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...
- 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 #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- codeforces 822 D. My pretty girl Noora(dp+素数筛)
题目链接:http://codeforces.com/contest/822/problem/D 题解:做这题首先要推倒一下f(x)假设第各个阶段分成d1,d2,d3...di组取任意一组来说,如果第 ...
- Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)
385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...
随机推荐
- java:注解(一)
什么是注解 注解(Annotation),也叫(metadata)元数据.一种代码级别的说明.它是JDK1.5及以后版本引入的一个特性,与类.接口.枚举是在同一个层次.它可以声明在包.类.字段.方法. ...
- ssh登录慢的解决办法
ubuntu的ssh登录有点慢,其实是很慢 google了一把,发现可以这样解决: (1)可能是DNS反向解析的问题 对于这样的问题,可以在/etc/ssh/sshd_config 中添加/修改: U ...
- android位置布局
fill_parent 设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间.这跟Windows控件的dockstyle属性大体一致.设置一个顶部布局或控件为 ...
- typedef 与 define 的区别
1.区别 (1)定义.执行时间.作用域 定义.执行时间: #define pchar char * typedef char *pchar; 定义的格式差别,显而易见的,要注意,define 是不能存 ...
- 【转载】ASP.Net请求处理机制初步探索之旅 - Part 3 管道
开篇:上一篇我们了解了一个ASP.Net页面请求的核心处理入口,它经历了三个重要的入口,分别是:ISAPIRuntime.ProcessRequest().HttpRuntime.ProcessReq ...
- CentOS 5.5安装SVN(Subversion)
检查已安装版本 #检查是否安装了低版本的SVN[root@localhost /]# rpm -qa subversion #卸载旧版本SVN[root@localhost modules]# yum ...
- html用jquery获取屏幕宽度与滚动条的关系
当内容高度超过屏幕高度时,获取的屏幕宽度不包括滚动条.即使是浮动,也要显式设置高度,才会全屏. 未超过时,获取的宽度包括滚动条.
- python升级安装后的yum的修复
升级python版本号后,执行yum # yum -y install openssl 提演示样例如以下: There was a problem importing one of the Pytho ...
- 【Selenium + Python】路径报错之OSError: [Errno 22] Invalid argument: './t/report/2018-03-23_11:03:12_report.html'
现象: 此问题真的是太痛苦了,查了好多资料是说路径的问题,结果还是报错,后来一点点的排查才发现原来是!!!!!! 废话不多说上原来代码: if __name__ == '__main__': star ...
- windowsphone8.1学习笔记之应用数据(三)
之前说了如何操作文本文件,如果是图片文件或者其他的二进制文件则需要操作文件的Stream或者Buffer数据.就需要用到DataReader和DataWriter这两个类了,这个的好好的练一下,以后的 ...