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 (<10
5
) 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
题目本身不难,但是还是不会,再看别人的思路之前我是拒绝的,我想这样的题目要自己独立弄出来,不知道是今天有点累了还是什么,不大想动脑子了,估计是前面1014那个题目搞的,太恶心了= =,主要是进制的转换把,而且以前的素数模板竟然错了一个点,唉
#include<iostream>
using namespace std;
bool isprime(int n){
if(n < 2 ) return false;
for(int i=2;i*i<=n;i++){
if(n % i == 0) return false;
}
return true;
}
int main(){
int n;
while(scanf("%d",&n) ){
if(n < 0) break;
int radix;
cin>>radix;
if(isprime(n) == false) cout<<"No"<<endl;
else{
int a[100],index=0,ans=0;
do{
a[index++] = n % radix;
n/=radix;
}while(n != 0);
for(int i=0;i<index;i++){
ans = ans * radix + a[i];
}
if(isprime(ans)) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
}
return 0;
}
这题主要就是 10进制转某进制,某进制转10进制