Sorry next time I will read the question carefully...
# include <iostream>
# include <cmath>
# include <algorithm>
# include <vector>
using namespace std;
// 如果一个数字是素数并且将它转换为D进制后,将其在D进制下逆序,然后转换为10进制的数也是素数的话,那么就是Yes
vector<int> Num;
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, D;
while(scanf("%d", &N) != EOF){
if(N < 0)
break;
scanf("%d", &D);
if(isPrime(N) == false)
printf("No\n");
else{
//将10进制的N转换为数组D进制;
Num.clear(); // 记得清空
do{
Num.push_back(N % D);
N /= D;
}while(N != 0);
// 将数组D进制下的N逆序,并将逆序后转换回10进制数字
// (因为存的时候就是倒着的,所以不用反向迭代器)
for(auto it = Num.begin();it != Num.end();it++)
N = N*D + *it;
isPrime(N) ? cout<<"Yes\n" : cout<<"No\n";
}
}
return 0;
}