题目
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 (<
) 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, orNo
if not.
样例输入
73 10
23 2
23 10
-2
样例输出
Yes
Yes
No
题意理解
题目大意是给你一个数,判断一下这个数是否是素数,如果不是素数那么可以直接输出No了,如果是素数,那么将这个数字转换成D进制的数的逆序,再判断一下是不是素数,如果是,则输出Yes,不是就输出No。那么我们直接根据题意模拟一下即可。水题。
代码
#include<bits/stdc++.h>
using namespace std;
bool prime(int nu){
if(nu<2)return false;
for(int i=2;i<=nu/i;i++){
if(nu%i==0)return false;
}
return true;
}
bool check(int nu,int d){
if(!prime(nu))return false;
int x=nu;int sum=0;
while(x){
int c=x%d;
sum=sum*d+c;
x/=d;
}
if(!prime(sum))return false;
else return true;
}
void solve(){
int n,m;
while(~scanf("%d",&n)&&n>0){
scanf("%d",&m);
if(check(n,m))puts("Yes");
else puts("No");
}
}
int main(){
solve();
return 0;
}
版权声明:本文为NHS6671原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。