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. Mongodb在window上启动

    MongoDB 是目前在IT行业非常流行的一种非关系型数据库(NoSql),其灵活的数据存储方式备受当前IT从业人员的青睐.这里主要讲如何在windows平台下安装MongoDB. 安装最新版本mon ...

  2. [USACO1.5]数字三角形 Number Triangles

    题目描述 观察下面的数字金字塔. 写一个程序来查找从最高点到底部任意处结束的路径,使路径经过数字的和最大.每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 ...

  3. (Linux 日常命令)[20171225]

    目的:记录Linux日常所用命令 [20171222]Linux环境下查看硬件组件型号 cat /proc/cpuinfo及lspci 查看CPU [root@t-redhat- ~]# cat /p ...

  4. Linux入门-第五周

    1.磁盘lvm管理,完成下面要求,并写出详细过程: 1) 创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小 为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv; ...

  5. 【linux基于Postfix和Dovecot邮件系统的搭建】

    一:PostFixe和Dovecot的简单介绍 Postfix postfix是Wietse Venema在IBM的GPL协议之下开发的MTA(邮件传输代理)软件.postfix是Wietse Ven ...

  6. Windows下安装Mysql5.5.27(社区版)

    所有平台的 MySQL 下载地址为: MySQL 下载. 挑选你需要的 MySQL Community Server 版本及对应的平台. 运行mysql-5.5.27-win32.msi 进入欢迎界面 ...

  7. Node.js(一)----安装

    1.下载 地址 https://nodejs.org/en/download/ 注: 系统为ubuntu 下载的源码包 tar.gz 或者 wget https://nodejs.org/dist/v ...

  8. jquery easyui alert闪一下的问题

    最近做项目使用了 jQuery EasyUI,版本是 1.4.3.x,在使用alert方法的时候如果alert后面执行页面跳转的话alert的消息只会闪一下,就跳到其他页面了 $.messager.a ...

  9. 事物总线模式实例——EventBus实例详解

    事件总线模式是一种广泛运用于安卓开发之中的一种软件架构模式,而事件总线模式在安卓开发中最广泛的应用莫过于AndroidStudio提供的EventBus,所以我就EventBus来谈谈对事件总线模式的 ...

  10. Test类实验

    package PC_TEST; class CPU{ int speed; CPU(){ speed=0; } CPU(int k){ speed=k; } void setSpeed(int k) ...