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  () and  (), you are supposed to tell if  is a reversible prime with radix .

Input Specification:

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

Output Specification:

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

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
//N在10进制下是质数,N经过D进制转换后的数在10进制下也是质数
bool isprime(int n) {//判断素数
    if (n <= 1)return false;
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0)return false;
    }
    return true;
}
int main() {
    int N;
    int D;
    while (cin >> N >> D) {
        if (N < 0)break;
        if (!isprime(N)) {
            cout << "No" << endl;
            continue;
        }
        int temp = N;
        int reverse[100];//置换中间数
        int index = 1;
        while (temp >= D) {
            reverse[index] = temp % D;
            temp = temp / D;
            index++;
        }
        reverse[index] = temp;
        int re_prime=0;
        for (int i = 1; i <= index; i++) {
            re_prime = re_prime + reverse[i] * pow(D, index - i);
        }
        if (!isprime(re_prime)) {
            cout << "No" << endl;
            continue;
        }
        cout << "Yes" << endl;
    }
}