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求一遍前缀和. 代码: ...
随机推荐
- linux 挂载相关
mount命令的用法,以及技巧光盘镜像文件.移动硬盘.共享文件夹及U盘的方法. 一,挂接命令(mount) 挂接(mount)命令的使用方法. 命令格式: mount [-t vfstype] [- ...
- 利用DataSet部分功能实现网站登录
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- HBase——完全分布
实际上,在真实环境中你需要使用完全分布配置完整测试HBase.在一个分布式配置中,集群有多个节点,每个节点运行一个或多个HBase守护进程.其中包括主Master和备份Master实例,多个Zooke ...
- OpenCV实现图像颜色特征提取
https://github.com/ictlyh/ImageFeature 链接:http://pan.baidu.com/s/1mhUoPxI 密码:3cnn
- iBatis2 SqlMap中经常使用sql语句
本来我也不喜欢iBatis,那是由于我当时还不怎么会用它,如今我想说,iBatis是个好东西,不信你试试看.以下是我在项目实践中对iBatis的一个小总结.希望帮助众多在疲于iBatis编码而无暇思考 ...
- thymeleaf模版的使用
thymeleaf,我个人认为是个比较好的模板,性能也比一般的,比如freemaker的要高,而且把将美工和程序员能够结合起来,美工能够在浏览器中查看静态效果,程序员可以在应用服务器查看带数据的效果. ...
- 可执行Jar包调用动态链接库(DLL/SO)
踩过了很多的坑,查了很多资料,在此记录一下,以SpringBoot项目为基础. Maven加入JNA依赖 <!-- JNA start --> <dependency> < ...
- java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Object;
spring3_hibernate 集成报错信息 java.lang.NoSuchMethodError: org.jboss.logging.Logger.getMessageLogger(Ljav ...
- 用nvm管理windows nodejs时用npm全局安装的插件无法调用的解决方案
在环境变量中啊新建变量NODE_PATH赋值为prefix设置的地址即 prefix=D:\Users\xxx\AppData\Roaming\nodejs\npm-global 然后把%NODE_P ...
- Angular入门(二) 服务
目的:为了不再把相同的代码复制一遍又一遍,我们要创建一个单一的可复用的数据服务,并且把它注入到需要它的那些组件中. ※ 文件命名约定:服务名称的小写形式(基本名),加上.service后缀,如果服务 ...