PTA Reversible Primes (20分)

释放无限光明的是人心,制造无边黑暗的也是人心,光明和黑暗交织着,厮杀着,这就是我们为之眷恋又万般无奈的人世间。

reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.

Now given any two positive integers N (<10​5​​) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#include<climits>//INT_MAX

//#include<bits/stdc++.h>
#define PP pair<ll,int>
#define inf 0x3f3f3f3f
#define llinf 0x3f3f3f3f3f3f3f3fll
#define dinf 1000000000000.0
#define PI 3.1415926
typedef long long ll;
using namespace std;
int const mod=1e9+7;
const int maxn=3e5+10;
queue<int>q;
int p[101100],n,d;
void init()
{
    memset(p,0,sizeof p);
    p[1]=1;
    for(int i=2; i<=101000; i++)
    {
        if(p[i]==0)
        {
            for(int j=2; j*i<=101000; j++)
                p[i*j]=1;
        }
    }
    return ;
}
int main()
{
    init();
    while(~scanf("%d",&n))
    {
        if(n<0)
            break;
        scanf("%d",&d);
        if(p[n]==1)
            printf("No\n");
        else
        {
            while(!q.empty())
                q.pop();
            int k=n;
            while(k)
            {
                q.push(k%d);
                k=k/d;
            }
            k=0;
            while(!q.empty())
            {
                k=k*d+q.front();
                q.pop();
            }
            if(p[k]==0)
                printf("Yes\n");
            else
                printf("No\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/108430208