PAT(Advanced Level) 1015 Reversible Primes (20分)

PAT(Advanced Level) 1015 Reversible Primes (20分)

A 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 (< 1 0 5 10^5 105) and D (1

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 
using namespace std;

int isprime(int n){
    if (n == 2) return 1;
    if (n == 1) return 0;
    for (int i = 2; i * i <= n; i++){
        if (n % i == 0) return 0;
    }
    return 1;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    int n, radix;
    while (cin >> n && n > 0){
        cin >> radix;
        int t = 0, N = n;
        while (N){
            t = t * radix + N % radix;
            N /= radix;
        }
        if (isprime(t) && isprime(n)) puts("Yes");
        else puts("No");
    }

    return 0;
}

你可能感兴趣的:(PAT(Advanced,Level)