A1015 Reversible Primes (20 point(s))

D进制的回文判断

1. 原文

原题

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 (<$10^5$) 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:

1
2
3
4
73 10
23 2
23 10
-2

Sample output:

1
2
3
Yes
Yes
No

2. 解析

判断n是否为素数,将数字转换成d进制的数,反转后判断是否为素数

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<cstdio>
#include<cmath>
bool isPrime(int x){
if(x<2){
return false;
}
int sqr = sqrt(1.0*x);
for(int i = 2;i<=sqr;i++){
if(x%i==0){
return false;
}
}
return true;
}
int main(){
int n,d;
while(true){
scanf("%d%d",&n,&d);
if (n<0)
{
break;
}
int temp = 0;
bool flag = isPrime(n);
do{
temp = temp*d+n%d;
n/=d;
}while(n!=0);
if (flag&&isPrime(temp))
{
printf("Yes\n");
}else{
printf("No\n");
}
}
return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1015/
  • 发布时间: 2019-09-03 12:35
  • 更新时间: 2021-10-29 13:59
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!