题目链接:

C. 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).

Examples
input
1 1
output
40
input
1 42
output
1
input
6 4
output
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)的更多相关文章

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

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

  2. codeforces 414A A. Mashmokh and Numbers(素数筛)

    题目链接: A. Mashmokh and Numbers time limit per test 1 second memory limit per test 256 megabytes input ...

  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 Round #257 (Div. 1) C. Jzzhu and Apples (素数筛)

    题目链接:http://codeforces.com/problemset/problem/449/C 给你n个数,从1到n.然后从这些数中挑选出不互质的数对最多有多少对. 先是素数筛,显然2的倍数的 ...

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

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

  7. Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

    传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...

  8. codeforces 822 D. My pretty girl Noora(dp+素数筛)

    题目链接:http://codeforces.com/contest/822/problem/D 题解:做这题首先要推倒一下f(x)假设第各个阶段分成d1,d2,d3...di组取任意一组来说,如果第 ...

  9. Codeforces 385C - Bear and Prime Numbers(素数筛+前缀和+hashing)

    385C - Bear and Prime Numbers 思路:记录数组中1-1e7中每个数出现的次数,然后用素数筛看哪些能被素数整除,并加到记录该素数的数组中,然后1-1e7求一遍前缀和. 代码: ...

随机推荐

  1. Ruby之Rspec的报错解决

    #enconding:utf-8 require 'selenium-webdriver' require 'rspec' describe "baidu main page" d ...

  2. Windows 2008 R2 Server FTP使用问题

     Windows 2008 R2 Server FTP必须在安装IIS的时候.安装IIS6.0兼职性. 否则建的FTP网站仅仅有关了防火墙才干使用. 原因未知.

  3. zabbix agent shell一键安装脚本

    #!/bin/bash basepath=$(cd ``; pwd) SHELL_DIR="${basepath}/shell" PACKAGE_DIR="${basep ...

  4. Java 加载器

    类的加载是由类加载器完成的,类加载器包括: 根加载器( BootStrap ).扩展加载器( Extension ).系统加载器( System )和用户自定义类加载器( java.lang.Clas ...

  5. WikiCFP--A Wiki for Calls For Papers

    WikiCFP--A Wiki for Calls For Papers ---->www.wikicfp.com/cfp/

  6. Android 进阶自定义 ViewGroup 自定义布局

    前言 在我们的实际应用中, 经常需要用到自定义控件,比如自定义圆形头像,自定义计步器等等.但有时我们不仅需要自定义控件,举个例子,FloatingActionButton 大家都很常用,所以大家也很经 ...

  7. rst2pdf 中文

    上篇说到用pandoc转换为reST为pdf是使用LaTeX作为中间格式的,而今天要说的rst2pdf貌似是直接转换为pdf的. 安装和调用 rst2pdf目前只支持Python2.7,因此在创建vi ...

  8. Tsung 初步介绍安装

    tsung是erlang的一个开源的一个压力测试工具,可以测试包括HTTP, WebDAV, Mysql, PostgreSQL, LDAP, and XMPP/Jabber等服务器.针对 HTTP ...

  9. Python 深入剖析SocketServer模块(一)(V2.7.11)

    一.简介(翻译)  通用socket server 类  该模块尽力从各种不同的方面定义server:  对于socket-based servers:  -- address family:     ...

  10. EasyNVR RTSP摄像机HLS直播服务器中使用Onvif协议获取设备快照

    我们知道EasyNVR中可以获取快照信息,之前的文章也说明了EasyNVR是如何进行快照抓取的 这里我们使用另一种方法进行快照的抓取 流程 获取设备能力Capabilities 获取设备的能力,并且可 ...