PAT(Advanced Level) 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 (<10510^5105) 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 isprime(int n){
if (n == 2) return 1;
if (n == 1) return 0;
for (int i = 2; i * i <= n; i++){
if (n % i == 0) return 0;
}
return 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, radix;
while (cin >> n && n > 0){
cin >> radix;
int t = 0, N = n;
while (N){
t = t * radix + N % radix;
N /= radix;
}
if (isprime(t) && isprime(n)) puts("Yes");
else puts("No");
}
return 0;
}
本文探讨了PAT(Advanced Level)1015题“可逆素数”的解决方法,介绍了如何判断一个数在特定进制下是否为可逆素数,即其反转数也是素数。通过示例输入输出,展示了使用C++实现的判断算法。
254

被折叠的 条评论
为什么被折叠?



