坑点://注意不仅要判断翻转后的数是否是质数,还要判断原数是否是质数
#include<iostream>
#include<stack>
using namespace std;
bool is_Prime(int a){
if(a==0||a==1) return false;
for(int i=2;i*i<=a;i++)
if(a%i==0) return false;
return true;
}
int rev(int n,int d){
int ans=0,i=0,temp,D=1;
stack<int> st;
while(n>0){
st.push(n%d);
n/=d;
}
while(!st.empty()){
temp=st.top();
ans+=temp*D;
st.pop();
D*=d;
}
return ans;
}
int main()
{
int n,d;
while(1){
cin>>n>>d;
if(n<0) return 0;
int a=rev(n,d);
if(is_Prime(n)&&is_Prime(a)) cout<<"Yes"<<endl; //注意不仅要判断翻转后的数是否是质数,还要判断原数是否是质数
else cout<<"No"<<endl;
}
}