1015 Reversible Primes (20)(20 分)提问

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.

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

radix是进制的意思,判断一个数是否是素数,如果不是则输出no,如果是则在D进制下进行Reverse,反转之后如果还是素数,那么就输出yes.

//如何求d进制的逆,参考:https://blog.csdn.net/sunbaigui/article/details/8657051

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
bool isPrime(int n){
int m=sqrt(n);
if(n==||n==)return false;
for(int i=;i<=m;i++){
if(n%i==)return false;
}
return true;
}
int rever(int n,int d){
// string s="";求d进制,逆的方法
// int m;
// while(n!=0){
// m=n%d;
// n/=d;
// s+=(m+'0');
// }
// cout<<s<<" ";
// n=0;
// m=s.size();
// for(int i=0;i<m;i++){
// n=n*d+(s[i]-'0');//这样求也是对的,但是明显比较复杂。
// }
int sum=;
do{
sum=sum*d+n%d;//求逆!
n/=d;
}while(n!=);
return sum;
} int main()
{
int n,d;
while(scanf("%d%d",&n,&d)!=){
//判断n是否是素数
if(isPrime(n)){
n=rever(n,d);
if(isPrime(n))
printf("Yes\n");
else
printf("No\n");
}else
printf("No\n");
}
return ;
}

1.求逆一个do-while循环太厉害了,注释掉的部分是我写的,不太对。

2.要注意特殊条件的判断,n==0||n==1直接返回false;不是素数,要不然,样例只能通过两个,另两个通不过!special case.

PAT 1015 Reversible Primes[求d进制下的逆][简单]的更多相关文章

  1. PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

  2. PAT 1015 Reversible Primes

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

  3. pat 1015 Reversible Primes(20 分)

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

  4. PAT 1015 Reversible Primes (判断素数)

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  5. PAT-1015 Reversible Primes (20 分) 进制转换+质数

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  6. 数位DP 求K进制下0~N的每个数每位上出现的数的总和

    好久没写博客了,因为感觉时间比较紧,另一方面没有心思,做的题目比较浅也是另一方面. 热身赛第二场被血虐了好不好,于是决定看看数位DP吧. 进入正题: 如题是一道经(简)典(单)的数位dp. 第一步,对 ...

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

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

  8. PAT A1015 Reversible Primes (20 分)——进制转换,质数

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

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

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

随机推荐

  1. spring mvc 跨域请求处理——spring 4.2 以上

    Controller method CORS configuration You can add to your @RequestMapping annotated handler method a  ...

  2. jquery-json 插件使用方法

    var thing = { plugin: 'jquery-json', version: 2.4 }; var encoded = $.toJSON( thing ); // '{"plu ...

  3. smarty 双引号中嵌入变量的方法

    1.如果变量中只包含字符.数字.下划线,可以将变量直接写在双引号中,如:"my name is $name" 2.如果带有其它字符,如“.”,则需要将变量用单引号括起来,如:“my ...

  4. iOS 8 AutoLayout与Size Class自悟

    前言 iOS8和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhone,再也不用纠结为大屏买三星舍苹果了…但是对于iOS开发人员来说,迎来了和Android开发开发一样的问题—> ...

  5. iOS - 自动化编译打包(Jenkins)

    从xcodebuild到shenzhen,再到Jenkins,完美演绎自动化操作. Features xcodebuild自动构建命令 简介 构建 生成ipa文件 利用 shenzhen 进行打包 J ...

  6. postgresql----网络地址类型和函数

    本人对网络这块实在是搞不清楚,要是能有人推荐一下资料就好了!不知道有没有跟我一样呢?!所以在这里先贴一点从其他地方搞来的一些IPv4的东东. IPv4主要包括一下5类地址 A类: 0 7位 网络号 2 ...

  7. PAT天梯赛L2-003 月饼【贪心】

    L2-003. 月饼 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不 ...

  8. 微软、谷歌、亚马逊、Facebook等硅谷大厂91个开源软件盘点(附下载地址)

    开源软件中有大量专家构建的代码,大大节省了开发人员的时间和成本,热衷于开源的大厂们总是能够带给我们新的惊喜.2016年9月GitHub报告显示,GitHub已经有超过 520 万的用户和超 30 万的 ...

  9. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  10. JNUOJ 1180 - mod5

    首先,可以自己先一个超时的标程出来: #include<cstdio> typedef long long ll; ll n,m,cnt; int main() { int t; scan ...