【CodeForces - 569C】Primes or Palindromes? (思维,分析范围,暴力枚举判断)
題干:
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?Afind the maximum?n, such that?π(n)?≤?A·rub(n).
Input
The input consists of two positive integers?p,?q, 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 1Output
40Input
1 42Output
1Input
6 4Output
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 ;}?
總結
以上是生活随笔為你收集整理的【CodeForces - 569C】Primes or Palindromes? (思维,分析范围,暴力枚举判断)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: qconsvc.exe - qconsv
- 下一篇: reminder.exe - remin