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 (<105) 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<vector>
using namespace std;
vector<int> v;
long long int to_radix(int n, int d){
long long int rev_dec_num = 0;
long long int exp = 1;
while (n)
{
v.push_back(n % d);
n = n / d;
}
for(int i = v.size() - 1 ; i >= 0; i--){
rev_dec_num = rev_dec_num + v[i] * exp;
exp *= d;
}
return rev_dec_num;
}
void judge_prime(long long int num1, long long int num2){
bool flag = true;
long long int max_num;
long long int min_num;
max_num = max(num1, num2);
min_num = min(num1, num2);
for(long long int i = 2; i < min_num; i ++)
if(num1 % i == 0 || num2 % i == 0)
flag = false;
for(long long int i = min_num; i < max_num; i ++)
if(max_num % i == 0)
flag = false;
if(flag)
cout << "Yes" << endl;
else
{
cout << "No" << endl;
}
}
int main(){
long long int n,d,r_n;
while (true)
{
cin >> n;
if(n < 0)
break;
cin >> d;
r_n = to_radix(n, d);
judge_prime(n, r_n);
v.clear();
}
return 0;
}