PTA (Advanced Level) 1015 Reversible Primes
Reversible Primes
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
解题思路:
本题给出一个十进制数字n,之后给出其进制d,要求判断其是不是可逆素数,若是可逆素数输出Yes否则输出No。
本题的可逆素数要求给出数字本身是素数且,将其转化为给出的进制,反转后重新转化为十进制还是素数。
如:23的2进制为10111反转后为11101 = 29,29与23都是素数,所以23是可逆素数。
由于本题有多组输入,所以可以用素数筛(埃氏筛法)先将素数打表。
埃氏筛法:
int prime[maxn];
bool vis[maxn] = {false};
int cnt = ;
void findPrime(){ //埃氏筛法
//每找到一个一个素数将其倍数都标记为不是素数
//时间复杂度O(n loglogn)
for(int i = ; i < maxn; i++){
if(vis[i] == false){ //i是素数
prime[cnt++] = i;
for(int j = * i; j < maxn; j += i){
vis[j] = true; //标记所以i的倍数
}
}
}
}
之后根据我们得到的素数判断输入的数是否为素数,若不是素数直接输出No,若是素数则将其转化为对应进制的数字后反转,将反转后得到的数字重新转化为10进制,在判断其是不是素数,是的话输出Yes否则输出No。
这样我们就需要两个函数,一个用来将其他进制数转化为10进制。另一个用来将10进制转化为其他进制并反转。
转化为d进制:
string decimalToOther(int num, int radix){ //将十进制数转化为其他进制数
string ans;
//ans从最低位开始记录,结束后得到的直接就是转化后数字的反转
while(num){
ans += (num % radix) + '';
num /= radix;
}
return ans;
}
转化为10进制
LL toDecimal(string num, int radix){ //将某进制数转化为10进制
LL ans = ;
for(int i = ; i < num.size(); i++){
ans = ans * radix + (num[i] - '');
if(ans < )
return -;
}
return ans;
}
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+;
int prime[maxn];
bool vis[maxn] = {false};
int cnt = ;
void findPrime(){ //埃氏筛法
//每找到一个一个素数将其倍数都标记为不是素数
//时间复杂度O(n loglogn)
for(int i = ; i < maxn; i++){
if(vis[i] == false){ //i是素数
prime[cnt++] = i;
for(int j = * i; j < maxn; j += i){
vis[j] = true; //标记所以i的倍数
}
}
}
}
LL toDecimal(string num, int radix){ //将某进制数转化为10进制
LL ans = ;
for(int i = ; i < num.size(); i++){
ans = ans * radix + (num[i] - '');
if(ans < )
return -;
}
return ans;
}
string decimalToOther(int num, int radix){ //将十进制数转化为其他进制数
string ans;
//ans从最低位开始记录,结束后得到的直接就是转化后数字的反转
while(num){
ans += (num % radix) + '';
num /= radix;
}
return ans;
}
int n, d;
int main()
{
findPrime(); //素数打表
while(scanf("%d", &n) != EOF && n > ){
//输入n,n < 0时直接结束运算
scanf("%d", &d);
//输入进制d
if(vis[n] == true || n <= ){ //若n不是素数直接输出No进行下一次运算
printf("No\n");
continue;
}
string toRadix = decimalToOther(n, d);
//将n转化为d进制并反转
int ans = toDecimal(toRadix, d);
//将反转的数重新转化为10进制
if(vis[ans] == true || ans <= ){//若反转的数不是素数输出No
printf("No\n");
continue;
}
printf("Yes\n"); //否则输出Yes
}
return ;
}
PTA (Advanced Level) 1015 Reversible Primes的更多相关文章
- PAT (Advanced Level) 1015. Reversible Primes (20)
简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT 甲级 1015 Reversible Primes(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- pat 1015 Reversible Primes(20 分)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PTA(Advanced Level)1036.Boys vs Girls
This time you are asked to tell the difference between the lowest grade of all the male students and ...
- 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 ...
随机推荐
- kubernetes yaml
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Deployment #指定创建资源的角色/类型 metadata: #资源的元数据/属性 ...
- 记一次Angular2环境搭建及My First Angular App小demo呈现
参考连接?不如说是照搬链接.AngularJs官网地址快速起步地址. 对于一个一直只是用jq,偶尔学习点Knockout js,了解一点mvvm结构的前端来说,学习Angular2还是有点困难的.好了 ...
- brew安装指定版本boost
brew 如何安装指定版本的boost brew uninstall boost brew install boost@1.57 brew link boost@1.57 --force --over ...
- “java.lang.NullPointerException”异常分析
1.父类定义的某个属性,没有被子类使用,或者在子类中,又重新定义一次. 2.因为调用了一个object的方法,且此object的reference为null:比如说:String a=null; // ...
- MYSQL处理高并发,防止库存超卖(图解)
抢购场景完全靠数据库来扛,压力是非常大的,我们在最近的一次抢购活动改版中,采用了redis队列+mysql事务控制的方案,画了个简单的流程图: 先来就库存超卖的问题作描述:一般电子商务网站都会遇到如团 ...
- Performs the analysis process on a text and return the tokens breakdown of the text
Analyzeedit Performs the analysis process on a text and return the tokens breakdown of the text. Can ...
- rpm小结
包括安装.卸载.升级.查询.校验.数据库维护 一. 安装: 常用方法:rpm -ivh 包名 ,其中“i”表示安装, "v"表示显示安装过程,“h"表示以#显示程 ...
- win7 下如何安装 Microsoft Web Application Stress Tool
Microsoft Web Application Stress Tool是一个简单易用的性能压力测试工具,但因为其诞生的年代较早,在目前的win7系统下运行,会存在一些兼容性的问题,以下是我在实际使 ...
- 【1】JMicro微服务-RPC体验
如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl JMICRO运行简单服务提供者和消费者 1. 下载源代码 git checkout https://github.com/m ...
- STM32-RTC实时时钟-毫秒计时实现
OS:Windows 64 Development kit:MDK5.14 IDE:UV4 MCU:STM32F103C8T6 1.RTC时钟简介 STM32 的实时时钟(RTC)是一个独立的定时器, ...