PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
题目描述:
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.
译: 一个可逆素数是:在某个数制中是一个素数,它在该数制中的“逆”也是一个素数。例如,在十进制中,73是可逆素数,因为它的逆37也是素数。现在给你任意两个正整数 N ( <10 5 ) 和 D (1< D ≤ 10) ,你应该说明 N是否是 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.
译:每个输入文件包含几个测试用例,每个用例包含两个正整数 N 和D占一行。以输入一个负数作为结束标志。
Output Specification (输出说明):
For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.
译:对于每个测试用例,在一行中输出,如果 N 是 D 数制下的可逆素数输出 Yes 否则输出 No。
Sample Input (样例输入):
73 10
23 2
23 10
-2
Sample Output (样例输出):
Yes
Yes
No
The Idea:
设计到素数,首先想到了先将题目范围内的所有素数标记出来,利用 筛法将所有素数对应的下标位置的数据值为 false。对于每个测试用例,在输入 N 后,判断 N 是否是一个素数,若不是素数可以直接输出 No ,如果 N 是一个素数,再将 N 转为 D 数制下的数字并取逆,再将其表示的数算 x出来,再判断 x 是否是一个素数,如果x是一个素数,则输出Yes,否则输出 No 。
The Codes:
#include<bits/stdc++.h>
using namespace std ;
#define MAX 100010
bool prime[MAX] = { false } ; // 初始化
void isPrime(){ // 筛选法求素数
for(int i = 2 ; i < MAX ; i ++ )
if(!prime[i])
for(int j = i + i ; j < MAX ; j += i)
prime[j] = true ; // i是素数,则 i 的所有倍数都不可能是素数
prime[1] = true ; // 注意 1 不是素数
}
int reverseNofD(int n , int d){
int m = 1 , eve[105] , cnt = 0 ;
for( ; n != 0 ; n /= d) eve[cnt ++] = n % d ;
for(int i = cnt - 1 ; i >= 0 ; m *= d , i --) // 逆置求加权值
n += m * eve[i];
return n ;
}
int main(){
isPrime() ; // 先标记素数
int n , d ;
while(scanf("%d" , &n) , n >= 0){
scanf("%d" , &d) ;
if(!prime[n]) // n 是素数
if(!prime[reverseNofD(n , d)]) printf("Yes\n") ; // 且 D 数制也是一个素数
else printf("No\n") ;
else printf("No\n") ;
}
return 0;
}
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642的更多相关文章
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- 【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...
- PAT (Advanced Level) Practice 1152 Google Recruitment (20 分)
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the p ...
- PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)
Two integers are called "friend numbers" if they share the same sum of their digits, and t ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...
随机推荐
- Set-Cookie & Secure & HttpOnly & SameSite
Set-Cookie & Secure & HttpOnly & SameSite HTTP/Headers/Set-Cookie Set-Cookie https://dev ...
- 微信小程序-生命周期图解
微信小程序-生命周期图解 小程序生命周期 App 生命周期 https://developers.weixin.qq.com/miniprogram/dev/reference/api/App.htm ...
- PEP 8 & Style Guide
PEP 8 & Style Guide Style Guide for Python Code https://www.python.org/dev/peps/pep-0008/ PEP Py ...
- Nodejs 使用 TypeScript
安装依赖 λ yarn add typescript types/node concurrently nodemon wait-on -D 初始化一个 tsconfig.json λ ./node_m ...
- Captain technology INC:全球新能源汽车格局突变
美国能源信息署EIA的统计数据显示,2020年上半年全美含纯电动和插电混动在内的新能源乘用车总销量仅为11.1万辆,同比缩水25%.虽然特斯拉在第三季度靠着13.93万辆的销量迎来了环比387%.同比 ...
- C++算法代码——你要乘坐的飞碟在这里[usaco]
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1056 题目描述 一个众所周知的事实,在每一慧星后面是一个不明飞行物UFO. 这些不明 ...
- python中的enumerate 函数(编号的实现方式)
enumerate 函数用于遍历序列中的元素以及它们的下标: 默认从0开始,如果想从1开始,可以仿照最后案例 加上逗号,和数字编号 >>> for i,j in enumerate( ...
- Python切换版本工具pyenv
目录 安装pyenv 安装与查看py版本 切换py版本 结合ide使用示例 和virtualenv的一些区别 参考文献 使用了一段时间,我发现这玩意根本不是什么神器,简直就是垃圾,安装多版本总是失败, ...
- Python3.x 基础练习题100例(61-70)
练习61: 题目: 打印出杨辉三角形. 程序: if __name__ == '__main__': a = [] for i in range(10): a.append([]) for j in ...
- 《C++ Primer》笔记 第13章 拷贝控制
拷贝和移动构造函数定义了当用同类型的另一个对象初始化本对象时做什么.拷贝和移动赋值运算符定义了将一个对象赋予同类型的另一个对象时做什么.析构函数定义了当此类型对象销毁时做什么.我们称这些操作为拷贝控制 ...