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. no need jQuery anymore & You don't need jQuery anymore!

    no need jQuery anymore & You don't need jQuery anymore! "use strict"; /** * * @author ...

  2. google advanced search operators

    google advanced search operators https://www.google.com/advanced_search js es6 site:xgqfrms.xyz http ...

  3. ruby & rvm

    ruby & rvm https://rvm.io/ Ruby Version Manager (RVM) RVM is a command-line tool which allows yo ...

  4. GeoJSON feature & Mapbox segments

    GeoJSON feature & Mapbox segments custom JSON format ??? { "rows": [], "props&quo ...

  5. js clear copy

    js clear copy window.getSelection().empty() & window.getSelection().removeAllRanges() & docu ...

  6. 为什么要抢挖Baccarat流动性挖矿的头矿?头矿的价值是什么?

    今年下半年,DeFi流动性挖矿非常受投资者的欢迎,究其原因,其超高的挖矿回报率着实足够吸引无数投资者的眼球.而即将上线的Baccarat流动性挖矿,也未上线先火了一把.Baccarat是由NGK公链推 ...

  7. (转)IP协议详解之子网寻址、子网掩码、构造超网

    原文网址:http://www.cnblogs.com/way_testlife/archive/2010/10/05/1844399.html 子网寻址 1. 从两级IP地址到三级IP地址 < ...

  8. 配置安装oh-my-bash

    背景 最近项目上有用到petalinux,所以只能在bash环境下使用命令行,所以不能使用oh-my-zsh 之前看一个dalao用oh-my-bash来规避这个问题.所以这里试一下安装oh-my-b ...

  9. SQL Server中DELETE和TRUNCATE的区别

    ​DELETE和TRUNCATE语句之间的区别是求职面试中最常见的问题之一.这两条语句都可以从表中删除数据.然而,也有不同之处. 本文将重点讨论这些差异,并通过实例加以说明. TRUNCATE DEL ...

  10. Error Code: 1366. Incorrect DECIMAL value: '0' for column '' at row -1 0.266 sec;

    Reference: https://stackoverflow.com/questions/35037288/incorrect-decimal-integer-value-mysql     Er ...