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. osx上使用'cd'命令跳转到别名(alias)目录

    在mac上使用windows的共享目录时,在terminal中时法使用cd的,会提示"xxx 不是目录",经过一番的查找,发现了Mac Terminal 'cd' to folde ...

  2. ffmpeg学习(二) 通过rtsp获取H264裸流并保存到mp4文件

    本篇将使用上节http://www.cnblogs.com/wenjingu/p/3977015.html中编译好的库文件通过rtsp获取网络上的h264裸流并保存到mp4文件中. 1.VS2010建 ...

  3. driver.get()和driver.navigate().to()到底有什么不同?-----Selenium快速入门(四)

    大家都知道,这两个方法都是跳转到指定的url地址,那么这两个方法有什么不同呢?遇到这种情况,第一反应就是查查官方的文档. 官方文档的说法是:Load a new web page in the cur ...

  4. .net使用QQ邮箱发送邮件

    /// <summary> /// 发送邮件 /// </summary> /// <param name="mailTo">要发送的邮箱< ...

  5. .NET控件名称缩写一览表

    转载自如下链接: https://www.cnblogs.com/xpvincent/p/9334851.html 字体实在是太小了,我看着好闹心,就复制过来自己放大下,谢谢. 标准控件1 btn B ...

  6. 接口和抽象类的使用场景以及多类继承存在的问题(c#)

    我们首先来看下抽象class能发挥优势的使用场景. 假设有一个Cars基类,具体型号的Car继承该基类,并实现自己独有的属性或方法. public class Cars { public string ...

  7. AJAX get/post;

    $.ajax({ dataType: "json", type: "POST", url: "地址(/api/products)", dat ...

  8. MVC中用jQuery加BootStrap实现动态增加删除文本输入框!

    http://www.freejs.net/article_biaodan_278.html 这是在网上找到方法,我修改了一下实合我的项目,发博只为收藏记录并加深记忆. 修改后效果如下 @model ...

  9. loadrunner代理录制

    loadrunner在使用过程中会受到浏览器版本的限制,有些web服务在低版本的浏览器上不能兼容,可通过代理录制的方式解决此问题. 注: (1)本文中的ip仅作示例,要按实际测试情况填写ip. (2) ...

  10. java使用memcached1--安装与基本使用

    环境 CentOs6.4 libevent-2.0.22-stable memcached-1.4.24 一.memcached安装 # cd /usr/local 1.编译安装libevent # ...