1015. Reversible Primes (20)

来源:互联网 发布:哪里有砍价软件 编辑:程序博客网 时间:2024/06/10 01:53

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 1023 223 10-2
Sample Output:
YesYesNo
英语真是硬伤,这个没读懂。我看到案例23 2的时候在想,23怎么能是2进制。。。

然后百度,发现理解错题意


假如23 2 首先判断23是否为素数,然后判断23在二进制下面的数逆序转换成10进制是否为素数

也就是23在2 redix为10111 逆序就为11101,转换成十进制为29,都为素数

#include<iostream>#include<cstring>#include<cstdio>#include<queue>#include<stack>#include<set>#include<algorithm>#include<cmath>using namespace std;int n,d;int pr(int num){if(num==1) return 0;if(num==2||num==3) return 1;for(int i=2;i<=sqrt(num);i++){if(num%i==0) return 0;}return 1;}int p(int num){int sum=0;while(num){sum=sum*d+num%d;num/=d;}if(pr(sum)) return 1;return 0;}int main(){while(cin>>n&&n>0){cin>>d;if(pr(n)&&p(n)){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}}return 0;}