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.
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<bits/stdc++.h>
using namespace std;
int main(){
long long d,n;
long long a[111111];
while(cin>>d)
{
if(d<0)break;
cin>>n;
int f1=0,f2=0;
if(d==1)
{
cout<<"No"<<endl;
continue;
}
for(long long i=2;i<=sqrt(d);i++)
{
if(d%i==0)
f1=1;
}
int z=0;
while(d)
{
a[z]=d%n;
d/=n;
z++;
}
d=0;
long long k1=1;
int f3=0;
for(int i=z-1;i>=0;i--)
{
if(a[i]>0)f3=1;
if(f3==0)continue;
d+=k1*a[i];
k1*=n;
}
for(long long i=2;i<=sqrt(d);i++)
{
if(d%i==0)
f2=1;
}
if(d==1)
f2=1;
if(f1||f2)
{
cout<<"No"<<endl;
}
else cout<<"Yes"<<endl;
}
return 0;
}