Codeforces Round #315 (Div. 2) 568A Primes or Palindromes?

题目:Click here

题意:π(n)表示不大于n的素数个数,rub(n)表示不大于n的回文数个数,求最大n,满足π(n) ≤ A·rub(n)。A=p/q;

分析:由于这个题A是给定范围的,所以可以先暴力求下最大的n满足上式,可以想象下随着n的增大A也在增大(总体正相关,并不是严格递增的),所以二分查找时不行的,所以对给定的A,n是一定存在的。这个题的关键就是快速得到素数表最好在O(n)的时间以内。(杭电15多校的一个题也用到了这个算法点这里查看)

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int M = 1179858+3;
 4
 5 double p, q;
 6 bool prime[M];
 7 int ans, pri, pal;
 8 void getprime( )    {   // 类似筛选法求素数 打表
 9     prime[1] = false;
10     for( int i=2; i<M; i++ )    {
11         if( !prime[i] )  continue;
12         prime[i] = true;
13         for( int j=2; j*i<M; j++ )
14             prime[i*j] = false;
15     }
16 }
17
18 bool palindromic( int n )   {   // 判断回文数
19     int fn = 0;
20     int aun = n;
21     while( aun )    {
22         fn *= 10;
23         fn += aun%10;
24         aun /= 10;
25     }
26     return fn == n ;
27 }
28
29 int main()  {
30     memset( prime, true, sizeof(prime) );
31     getprime( );
32     while( ~scanf("%lf%lf", &p, &q ) )  {
33         double A = p/q;
34         pri = pal = 0;
35         for( int i=1; i<M; i++ )    {
36             pri += prime[i];
37             pal += palindromic(i);
38             if( pal*p/q >= pri )
39                 ans = i;
40         }
41         printf("%d\n", ans );
42     }
43     return 0;
44 }
时间: 2024-10-06 08:00:10

Codeforces Round #315 (Div. 2) 568A Primes or Palindromes?的相关文章

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 Description Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindro

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

这道题竟然是一个大暴力... 题意: π(n):小于等于n的数中素数的个数 rub(n) :小于等于n的数中属于回文数的个数 然后给你两个数p,q,其中A=p/q: 然后要你找到对于给定的A,找到使得π(n)?≤?A·rub(n) 最大的n. (A<=42) 思路: 首先我们可以暴力算出当n为大概150万左右的时候,π(n)大概是 rub(n) 的42倍. 所以我们只需要for到150万左右就好,因为对于后面的式子,肯定能在150万的范围内找到一个n使得这个式子成立的. 而且,我们可以得出因为素

Codeforces Round #315 (Div. 2) C - Primes or Palindromes?(暴力打表)

题意:给一个p和q然后求π(n)?≤?p/q*rub(n),的最大的n值,其中π(n)?表示从1到n之间的素数的个数, rub(n)表示从1到n之间的回文数的个数(回文数不能有前导0,且从左到右和从右到左一样) 分析:其实这题没有题目没有确定n的范围让人不敢直接暴搜打表,但是你只要手动写个函数y=π(n)?/rub(n) 手动模拟暴力一下就可以发现其实这个函数大概是先下降后上升的,由于1/42<=p/q<=42,也就是说当y=42的时候就 是它的边界了,那么n的范围大概是1200000左右,取

Codeforces Round #315 (Div. 1)

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 un

Codeforces Round #315 (Div. 2) (ABCD题)

A. Music 题意: 一首歌长度为S秒,已经下载了T秒,下载速度为每q秒的现实时间能下载下来(q-1)秒 的歌曲.现在开始听歌,如果听到还没下载的地方就从0秒的地方开始replay,求一首歌听完需要从0秒听几次(包括一开始那次) 思路: 我们可以用路程-时间的思路来考虑这道题. 假设两位选手"播放"与"下载","播放"的起点是0m处,"下载"的起点是Tm处,终点在Sm处,"播放"的速度是1m/s,&qu

2017-4-29-Train:Codeforces Round #315 (Div. 2)

A. Music(数学题) Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city of

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(10,2).偶回文同理.然后不能二分,因为比值不是单调的. 乱码: #pragma comment(linker,"/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include

Codeforces Round #315 (Div. 2)

Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city of E

Codeforces Round #315 (Div. 2) A. Music 解题心得

原题: Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city