【CodeForces - 569C】Primes or Palindromes? (思维,分析范围,暴力枚举判断)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/82762404

题干:

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 Afind 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

题目大意:

定义π(N)=∑(i为素数?1:0),i<=N, rub(N)=∑(i为回文数?1:0),i<=N。A=p/q,求最大的N,使得π(n) ≤ A·rub(n)。

解题报告:

     通过观察单调性(因为显然素数个数增长的快啦,可以打个表判断一下,n随便取个值,然后输出几组数据,就可以看出来,素数个数的增长远远大于回文数的增长,我这有个数据,100W个数,有1000个回文数左右),和p和q给的范围(给你范围的目的就是告诉你,可以打表?),我们知道可以算出一个极限来,可以算出 ,n是20W就足够了,数据量也不大,直接打表暴力。

AC代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 2e6+ 5;
int wei[1005],cnt;
ll su[1000005];
bool isprime[MAX];
ll sum1[MAX],sum2[MAX];//sum1素数,sum2回文数 
void prime() {
	cnt = 0;
	memset(isprime,1,sizeof isprime);
	isprime[0] = isprime[1] =0;
	for(ll i = 2 ; i<MAX; i++) {
		if(isprime[i]) {
			su[++cnt] = i;
		}
		for(ll j = 1; i * su[j]< MAX && j <= cnt; j++) {
			isprime[i * su[j]] = 0;
		}
	}
} 
bool fit(int x) {
	int p = 0;
	while(x) {
		wei[++p] = x%10;
		x/=10;
	}
	for(int i = 1; i<=(p+1)/2; i++) {
		if(wei[i] != wei[p-i+1]) return 0;
	}
	return 1;
} 
int main()
{
	int n;//1000W 中 1W个回文数左右
	int p,q;
	scanf("%d%d",&p,&q);
	prime();
	for(int i = 1; i<MAX; i++) {
		sum1[i] = sum1[i-1];
		if(isprime[i]) sum1[i]++;
	}
	for(int i = 1; i<MAX; i++) {
		sum2[i] = sum2[i-1];
		if(fit(i)) sum2[i]++;
	}
	int ans = -1; 
	for(int i = MAX-1; i>=0; i--) {
		if(sum1[i] * q <= sum2[i] * p) {
			ans = i;break;
		}
	}
	if(ans == -1) puts("Palindromic tree is better than splay tree");
	else printf("%d\n",ans);
	return 0 ;
 } 

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/82762404