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 (<105) 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

注意读题,首先他得是一个质数才行,这个先决条件我一开始没有判断,另外判断质数时要注意1,2,3和负数的情况

#include<bits/stdc++.h>
#define de(x) cout<<#x<<" "<<(x)<<endl
#define each(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
const int maxn=50+5;
int buf[maxn];
bool isprime(int x)
{
if(x<=1)return false;
bool flag=true;
if(x==2)return true;
if(x==3)return true;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
flag=false;
break;
}
}
return flag;
}
int main()
{
//de(isprime(73));
int n,r;
while(true)
{
cin>>n;
if(n<0)break;
cin>>r;
if(n==1||n==0)
{
puts("No");
continue;
}
if(!isprime(n))///读题的问题,首先得判断他是不是一个质数才行,
{
puts("No");
continue;
}
int len=0;
while(n>0)
{
buf[len++]=n%r;
n/=r;
}
//de(len);
int sum=0;
len--;
for(int i=0;i<=len;i++)
{
sum+=buf[i]*pow(r,len-i);
}
//de(sum);
if(isprime(sum))puts("Yes");
else puts("No");
}
}

PAT-1015 Reversible Primes (20 分) 进制转换+质数的更多相关文章

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

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

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

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

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

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

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

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

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

  6. 【PAT甲级】1015 Reversible Primes (20 分)

    题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...

  7. 1015 Reversible Primes (20 分)

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

  8. pat 1015 Reversible Primes(20 分)

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

  9. PAT Advanced 1015 Reversible Primes (20) [素数]

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

随机推荐

  1. base64和hex

    base64和hex 我们知道,字符分为二种:一种是可见字符:另一种是不可见字符. 1)三种编码方式 hex也称为base16,意思是使用16个可见字符来表示一个二进制数组,编码后数据大小将翻倍,因为 ...

  2. TynSerial序列(还原)TFDMemTable

    TynSerial序列(还原)TFDMemTable 1)TFDMemTable查询数据 procedure TForm1.Qrys(accountno, sql, sql2: string; Dat ...

  3. Send me - PLANETSHAKERS

      Send me i will go 送我,我会去 send me i will go 送我,我会 to this city, to this nations 为这城市 为这国家 and to th ...

  4. latex运算符

    一些小的运算符,可以在数学模式下直接输入,但是有一些运算符需要用控制序列生成:

  5. Ionic4.x 中的 UI 组件(UI Components) 侧边栏ion-menu组件以及底部tabs结合 侧边栏 ion-menu

    1.侧边栏 ion-menu 组件的基本使用 1.创建项目 ionic start myApp sidemenu 2.配置项目 属性 作用 可选值 side 配置侧边栏的位置 start end me ...

  6. 阶段5 3.微服务项目【学成在线】_day16 Spring Security Oauth2_02-用户认证技术方案-单点登录

    2 用户认证技术方案 2.1 单点登录技术方案 分布式系统要实现单点登录,通常将认证系统独立抽取出来,并且将用户身份信息存储在单独的存储介质,比如: MySQL.Redis,考虑性能要求,通常存储在R ...

  7. 未清SO关闭处理

    使用:  SD_SALESDOCUMENT_CHANGE   关闭订单 DATA: ls_header_in LIKE bapisdhd1 . DATA: ls_header_inx LIKE bap ...

  8. cq三期备注说明

    1.关于导航栏添加登陆拦截操作

  9. liunx -bash:ls:command not found,执行命令总是报找不到

    解决办法: 使用绝对命令vi打开profile /bin/vi  /etc/profile 添加: export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/ ...

  10. nginx静态资源设置缓存的方法

    nginx静态资源设置缓存的方法 直接加expires 30d; 就是就可以了 缓存时间30天完整如下 <pre> location / { root /home/www/wordpres ...