1015 Reversible Primes 分数 20
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.
任何数字系统中的可逆素数都是其在该数字系统中“逆”也是素数的素数。例如,在十进制中,73是可逆素数,因为它的逆37也是素数。
现在给定任意两个正整数N(<10^5)和D(1<D≤10),你应该判断N是否是以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.
输入文件由几个测试用例组成。每种情况都占据一行,其中包含两个整数N和D。输入以负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.
对于每个测试用例,如果N是以D为基数的可逆素数,则打印一行“是”,如果不是,则打印“否”
Sample Input:
73 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No
#include<iostream>
#include<cmath>
using namespace std;
//判断是否为素数
bool isprime(int a){
if(a<=1)return false;
for(int i=2;i<(int)sqrt(1.0*a);i++){
if(a%i==0) return false;
}
return true;
}
int a[100001];
int main()
{
while(1){
int n,d;
cin>>n>>d;
if(n<0)break;
if(isprime(n)==false)cout<<"No"<<endl;//不是素数
if(isprime(n)==true){
int len=0;
while(n!=0){//d的下标为0是第一位
a[len++]=n%d;
n/=d;
}
//当while跳出循环时,此时n值为0,len为转换进制后数字的长度
for(int i=0;i<len;i++){
n=n*d+a[i];
}
if(isprime(n)==true){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
}
return 0;
}