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.

译:每个输入文件包含几个测试用例,每个用例包含两个正整数 ND占一行。以输入一个负数作为结束标志。


Output Specification (输出说明):

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

译:对于每个测试用例,在一行中输出,如果 ND 数制下的可逆素数输出 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的更多相关文章

  1. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  2. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  3. 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 ...

  4. 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 ...

  5. 【PAT Advanced Level】1015. Reversible Primes (20)

    转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...

  6. 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 ...

  7. 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 ...

  8. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  9. 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) ...

随机推荐

  1. window.ShadyCSS

    window.ShadyCSS Web Components # install $ yarn add @webcomponents/shadycss@1.7.1 # OR $ npm i @webc ...

  2. Docker & Node.js

    Docker & Node.js https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ https://docs.docker.com ...

  3. flutter 使用vs编辑windows插件

    创建插件 mkdir win_test && cd win_test flutter create -t plugin --platforms windows ./ 找到win_tes ...

  4. c++ 遍历当前程序的线程

    #include <iostream> #include <Windows.h> #include <Psapi.h> #include <TlHelp32. ...

  5. Dart: 解析html字符串

    安装html包 import 'package:http/http.dart' as http; import 'package:html/parser.dart' show parse; impor ...

  6. Techme INC:红光和近红外光疗法有效加速肌肉恢复,美国橄榄球队已采用

    Techme INC创始人兼董事长MADELEINE VAUGHAN表示:在运动结束后,肌肉纤维因为细微损伤造成酸痛情形,即是延迟性肌肉酸痛-DOMS.这类酸痛发生时,需要适度的恢复,避免造成肌肉拉伤 ...

  7. NGK项目与其他项目相比有哪些优势?

    一个项目运行这么久,难免不被其他项目比来比去.NGK项目之所以能被很多人关注,是因为NGK具有独特的优势,NGK具有很高的性能,在智能合约上有多种应用,而且NGK具有独特的跨链技术.转账没有手续费,在 ...

  8. python 相对路径和绝对路径的区别

    一,Python中获得当前目录和上级目录 获取当前文件的路径: from os import path d = path.dirname(__file__) #返回当前文件所在的目录 # __file ...

  9. ROS等下载时无法连接问题的解决方法

    资料参考: https://blog.csdn.net/weixin_44692299/article/details/105869229

  10. 整合shiro+jwt大体思路

    springboot整合shiro大体上的思路: 1.自定义一个类Realm extends AuthorizingRealm{} 主要是对token授权和认证 重写2个方法 doGetAuthori ...