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. docker install and minikube install

    1.选择国内的云服务商,这里选择阿里云为例 curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/ ...

  2. python: 关于解决'\u'开头的字符串转中文的方法

    爬虫爬到的内容是这样的: 如果直接打印出来是这样的: python3的解决办法:字符串.encode('utf-8').decode('unicode_escape') python2:字符串.dec ...

  3. Publish site through visual studio

    https://www.c-sharpcorner.com/UploadFile/4b0136/getting-started-with-iis-host-and-publish-in-mvc-5/ ...

  4. leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...

  5. window server 2008 iis7+php安装配置

    安装环境支持 Microsoft Visual C++ 2012 net framework 4.5   php配置 precision = 20 serialize_precision = 100 ...

  6. React Native 中的 Flex Box 的用法(水平布局、垂直布局、水平居中、垂直居中、居中布局)

     版权声明:本文仅供个人学习. CSS 中 Flex-Box 语法链接 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html Flex 是 ...

  7. 重装GUI前备份GUI配置

    sap系统要重装, gui配置 想要保存,这个要怎么弄? SAP菜单  选项-> 本地数据 -> 历史记录 里的地址 C:\Users\Administrator\AppData\Roam ...

  8. OO ALV事件里使用E消息,下一步会退出到系统初始界面

    在OO ALV  data_change事件时(选中行),锁定KEY值, 继续,取消选择,退出到系统初始界面 改成 pv_status = 'E'. pv_msg = '采购订单' && ...

  9. 将任意音频格式文件转换成16K采样率16bit的wav文件

    此转换需要使用ffmpeg 假设有目录 d:\录音 目录有 张三.m4a, 李四.m4a xxx.m4a(其他任意格式音频触类旁通可以把 *.m4a改成*.*).批量转换成采样率16K,有符号,16b ...

  10. LeetCode_53. Maximum Subarray

    53. Maximum Subarray Easy Given an integer array nums, find the contiguous subarray (containing at l ...