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 (<) and D (1), 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<algorithm> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<string.h> using namespace std; const int maxn=110010; int isR(int n,int d){ int m=0; int a[maxn]; int i=0; while(n>0){ a[i]=n%d; n/=d; i++; } for(int j=0;j<=i-1;j++){ m=m*d+a[j]; } return m; } bool isPrime(int n){ if(n==0){ return false; } if(n==1){ //1既不是素数也不是合数 return false; } for(int i=2;i<=sqrt(n);i++){ //注意:i<=sqrt(n) 否则4判错 if(n%i==0){ return false; } } return true; } int main(){ int n,d; int a,b; while(scanf("%d",&n)){ a=0; b=0; if(n<0){ return 0; } scanf("%d",&d); if(isPrime(n)){ a=1; } n=isR(n,d); if(isPrime(n)){ b=1; } if(a==1&&b==1){ printf("Yes "); }else{ printf("No "); } } return 0; }