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求一遍前缀和. 代码: ...
随机推荐
- js:argument
引用:http://www.cnblogs.com/lwbqqyumidi/archive/2012/12/03/2799833.html http://www.cnblogs.com/Fskj ...
- leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
- PowerBuilder -- 数字金额大写
//==================================================================== // 事件: .pub_fc_change_number( ...
- PHP基础函数手记
PHP常用函数总结(180多个):http://www.jb51.net/article/101179.htm PHP常用函数归类总结[大全]:http://blog.csdn.net/ty_hf/a ...
- 【BZOJ】1003 Cards
[解析]Burnside引理+背包dp+乘法逆元 [Analysis] 这道题卡了好久,就是没想懂置换跟着色是不一样的. 依据burnside引理.在一个置换群作用下不等价类的个数为每一个置换作用下不 ...
- python 基础 7.1 datetime 获得时间
一 datatime 的使用 object timedeta tzinfo time data dat ...
- python 基础 2.8 python练习题
python 练习题: #/usr/bin/python #coding=utf-8 #@Time :2017/10/26 9:38 #@Auther :liuzhenchuan #@File ...
- Tomcat学习笔记【2】--- Tomcat安装、环境变量配置、启动和关闭
本文主要讲Tomcat的安装和配置. 一 Tomcat安装 1.1 下载 下载地址:http://tomcat.apache.org/ 1.2 安装 Tomcat是不需要安装的,解压压缩包即可. 在安 ...
- python自动化运维六:paramiko
paramiko是基于python实现的SSH2远程安全连接,支持认证以及密钥方式,可以实现远程命令执行,文件传输,中间SSH代理等功能.也就是采用SSH的方式进行远程访问.SSH登陆的方式可以参考之 ...
- while 循环中的break continue pass 的用法
while break:跳出最近的循环 continue:跳到最近所在循环的开头处 pass:什么也不做,只是空占位语句,它本身与循环没什么关系,但属于简单的单个单词语句的范畴: pass 语句是无运 ...