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
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 6 bool cha(int n) 7 { 8 if(n==1) return false;//注意1不是素数 9 bool temp=true; 10 for(int i=2;i<n;i++) 11 if(n%i==0) 12 { 13 temp=false; 14 break; 15 } 16 17 return temp; 18 } 19 20 int shu[101]; 21 22 23 int zhuan(int n,int d) 24 { 25 int count=0; 26 int tem=n; 27 while(tem) 28 { 29 shu[count++]=tem%d; 30 tem=tem/d; 31 } 32 33 tem=0; 34 int j; 35 for( j=0;j<count-1;j++) 36 tem=(tem+shu[j])*d; 37 tem+=shu[j]; 38 return tem; 39 40 } 41 42 43 44 int main() 45 { 46 int n; 47 while(cin>>n) 48 { 49 if(n<0) break; 50 51 int d; 52 cin>>d; 53 bool ifis=cha(n); 54 if(!ifis) cout<<"No"<<endl; 55 else 56 { 57 int rev=zhuan(n,d); 58 ifis=cha(rev); 59 if(!ifis) cout<<"No"<<endl; 60 else cout<<"Yes"<<endl; 61 } 62 63 } 64 return 0; 65 } 66