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. dropload上拉加载更多

    <link href="~/Scripts/dropload/dropload.min.css" rel="stylesheet" /> <s ...

  2. unbuntu16初始化设置,并解决虚拟机操作系统窗口不能自适应问题

    版本说明: 虚拟机:VMware Workstation 12.5.2 操作系统:ubuntu 16.04 unbuntu不同版本的下载链接:http://old-releases.ubuntu.co ...

  3. 【PHP】array_column函数

    array_column() 返回输入数组中某个单一列的值. 例子,从记录集中取出 last_name 列: <?php // 表示由数据库返回的可能记录集的数组 $a = array( arr ...

  4. scala成长之路(6)函数入门

    众所周知,scala作为一门极客型的函数式编程语言,支持的特性包括: 函数拥有“一等公民”身份: 支持匿名函数(函数字面量) 支持高阶函数 支持闭包 部分应用函数 柯里化 首先需要指出,在scala中 ...

  5. Python3.X-文本编码问题

    1.请说明python2与python3的默认编码是什么? python的默认编码是ASCII码,python3的默认编码是utf-8 2.为什么会出现中文乱码?能列举出现乱码的情况有哪几种么? 编码 ...

  6. 数据结构的C语言基础

    数据结构的C语言基础 1. 数据输出 printf()函数为格式输出函数,它存在于标准函数库中,在C语言程序中可以直接调用,但程序源文件的开头必须包含以下命令: #include < stdi ...

  7. 6-C++远征之封装篇[上]-学习笔记

    C++远征之封装篇(上) 课程简介 类(抽象概念),对象(真实具体) 配角: 数据成员和成员函数(构成了精彩而完整的类) 构造函数 & 析构函数(描述了对象的生生死死) 对象复制和对象赋值 ( ...

  8. Mysql通过Adjacency List(邻接表)存储树形结构

    转载自:https://www.jb51.net/article/130222.htm 以下内容给大家介绍了MYSQL通过Adjacency List (邻接表)来存储树形结构的过程介绍和解决办法,并 ...

  9. Python2 Sequence类型簇

  10. (数据科学学习手札17)线性判别分析的原理简介&Python与R实现

    之前数篇博客我们比较了几种具有代表性的聚类算法,但现实工作中,最多的问题是分类与定性预测,即通过基于已标注类型的数据的各显著特征值,通过大量样本训练出的模型,来对新出现的样本进行分类,这也是机器学习中 ...