题目描述
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.
输入描述
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.
输出描述
For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.
输入例子
73 10
23 2
23 10
-2
输出例子
Yes
Yes
No
我的代码
#include<stdio.h>
#include<math.h>
#include<iostream>
using namespace std;
struct A{
int num;
int r;
}a[100010];
int getRadix(int n,int r){
int ans=0;
int k[10010]={-1},i=0;
while(n!=0){
k[i]=n%r;
n=n/r;
i++;
}
for(int j=0;j<i;j++){
ans=ans+k[j]*pow(r,i-1-j);
}
return ans;
}
bool isPrime(int n){
int sqr=(int)sqrt(1.0*n);
for(int i=2;i<=sqr;i++){
if(n%i==0){
return false;
}
}
return true;
}
int main(){
int count=0;
while(1){
scanf("%d",&a[count].num);
if(a[count].num<0){
break;
}
scanf("%d",&a[count].r);
count++;
}
int rev;
for(int i=0;i<count;i++){
rev=getRadix(a[i].num,a[i].r);
if(a[i].num==1){
printf("No\n");
continue;
}
if(isPrime(a[i].num) && isPrime(rev)){
printf("Yes\n");
}
else{
printf("No\n");
}
}
return 0;
}
网友评论