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的更多相关文章

  1. PAT (Advanced Level) 1015. Reversible Primes (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  2. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

  3. PAT 1015 Reversible Primes

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  4. PAT 甲级 1015 Reversible Primes(20)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  5. PAT 1015 Reversible Primes[求d进制下的逆][简单]

    1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...

  6. pat 1015 Reversible Primes(20 分)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  7. PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

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

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

随机推荐

  1. ZooKeeper 一致性协议 ZAB 原理

    一致性协议有很多种,比如 Paxos,Raft,2PC,3PC等等,今天我们讲一种协议,ZAB 协议,该协议应该是所有一致性协议中生产环境中应用最多的了.为什么呢?因为他是为 Zookeeper 设计 ...

  2. 记一次很坑的python2与python3共存问题

    当添加PYTHONPATH环境变量时,无论输入pip2 -V还是pip3 -V都显示的是python2的环境变量,使用pip3 install 时也是安装在了python2的三方库(因为python2 ...

  3. winform在A窗体刷新B窗体,并改变窗体的属性

    //A窗体设置B窗体的属性并刷新B窗体 Application.OpenForm["窗体名称"].Controls["控件名称"].visible=true;

  4. django系列7.2--django中的cookie和session基本操作,浏览器登陆验证的不同实现

    django中的cookie和session(02)–操作 一.Django中的cookie操作 ctrl + shift + del 是谷歌浏览器中清除页面缓存和cookie的快捷键 1.设置coo ...

  5. B - 영어(字符串)

    原题链接 B - 영어 Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit S ...

  6. JDBC技术(汇聚页)

    JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API, 换句话说:JDBC是用于在Java ...

  7. fd - 更好的 find 命令

    欢迎关注我的公众号 spider-learn fd(https://github.com/sharkdp/fd) 是 find 命令的一个更现代的替换. 对比一下 查找名字含有某个字符的文件 OLD ...

  8. Python(简单计算器)

    参考:https://www.cnblogs.com/alex3714/articles/5169958.html import re ret = re.search('\([^()]+\)','(1 ...

  9. mxonline实战6 , 忘记用户密码时进行重置

        对应github地址:密码重置   原理:  1. 一个需要输入用户邮箱和注册码的密码忘记页面 2. 点击提交后,用户邮箱收到一个邮件,包含重置密码的链接 3. 点击链接进入密码重置页面   ...

  10. 2016级算法期末模拟练习赛-F.AlvinZH的青春记忆IV

    1086 AlvinZH的青春记忆IV 思路 难题,动态规划. 这是一道很有意思的题,因为它不仅卡了时间,也卡了空间,而且卡的很妙很迷. 光是理解题意已经有点难度,简化题意:两串数字序列,相等的数字定 ...