https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000

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), 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

时间复杂度:$ O(\sqrt{N}) $

代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10; int prime(int x) {
if(x == 1) return 0;
if(x == 2) return true;
for(int i = 2; i * i <= x; i ++)
if(x % i == 0)
return 0;
return 1;
} int main() {
int N, D;
int num[maxn];
while(~scanf("%d", &N)) {
if(!N) break;
scanf("%d", &D);
if(prime(N) == 0) {
printf("No\n");
continue;
}
else {
int cnt = 0;
do{
num[cnt ++] = N % D;
N /= D;
}while(N); int sum = 0, k = 0;
for(int i = cnt - 1; i >= 0; i --) {
sum += num[i] * pow(D, k);
k ++;
} if(prime(sum))
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}

  

PAT 甲级 1015 Reversible Primes的更多相关文章

  1. PAT 甲级 1015 Reversible Primes(20)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  2. PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  3. PAT甲级——A1015 Reversible Primes

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  4. PAT Advanced 1015 Reversible Primes (20) [素数]

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

  5. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

  6. PAT 1015 Reversible Primes

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  7. PAT 1015 Reversible Primes[求d进制下的逆][简单]

    1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...

  8. pat 1015 Reversible Primes(20 分)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  9. PTA (Advanced Level) 1015 Reversible Primes

    Reversible Primes A reversible prime in any number system is a prime whose "reverse" in th ...

随机推荐

  1. udt的java版本judt项目持续升级1.2版本

    修改了一些问题,努力兼容udt4版本.具体内容查看项目更新说明: 当前项目版本1.2 地址:https://github.com/jinyuttt/judt

  2. 解析 Nginx 负载均衡策略

    转载:https://www.cnblogs.com/wpjamer/articles/6443332.html 1 前言 随着网站负载的不断增加,负载均衡(load balance)已不是陌生话题. ...

  3. C++继承和派生练习(一)--关于从people(人员)类派生出student(学生)类等

    . 从people(人员)类派生出student(学生)类 添加属性:班号char classNO[]:从people类派生出teacher(教师)类, 添加属性:职务char principalsh ...

  4. 【转载】Git忽略规则和.gitignore规则不生效的解决办法

    原文:https://www.cnblogs.com/zhangxiaoliu/p/6008038.html Git忽略规则: 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 ...

  5. React学习(1)—— 基础项目搭建以及环境配置

    首先,我们需要安装node.js,直接搜索并在官网下载安装包. node.js官网:https://nodejs.org/en/ 现在我们成功安装了node和npm,然后我们来用npm创建新的项目,首 ...

  6. sql语句中#{}和${}的区别

    #---将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by “111”, 如果传入的 ...

  7. Lavavel5.5源代码 - 限流工具

    app('redis')->connection('default')->throttle('key000') // 每60秒,只能有10个资源被获取,在3秒内获取不到锁抛出异常 -> ...

  8. hoj第三场G-manhattanp ositioning system

    ---恢复内容开始--- 一.题意 在二维坐标系内,给定若干个点和目标点距离该点的曼哈顿距离,求是否存在唯一解使得目标点坐标为整数. 二.题解 重新思考题目模型,考虑曼哈顿距离一定时,几何含义为,以给 ...

  9. Hibernate-ORM:10.Hibernate中的分页

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的分页 hibernate中的分页其实很好写,它通过操作对象的方式,来进行分页 ...

  10. hibernate 各历史版本下载 spring各历史版本下载

    hibernate 各历史版本下载http://sourceforge.net/projects/hibernate/files/ spring各历史版本下载http://www.springsour ...