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.

比如在小数系统中73是一个反素数,因为37也是素数。

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.

现在给出任意两个正整数N和D,要求你写出N的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是不是素数,然后将N转换成D进制的数,然后翻转,然后转成10进制,然后判断结果和N是不是都是素数。
 
如23 2
转换成2进制是10111
反转是11101
转换成十进制是29
因为29和23都是素数,所以输出yes
 
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm> using namespace std; //判读是不是素数,是返回1,不是返回0
int isSu(int number)
{
if(number < )
return ;
for (int i = ; i*i <= number; i++)
{
if(number%i == )
return ;
}
return ;
} //将一个整数转换成任意进制,,逆序之后转换成10进制返回
int change(int a,int d)
{
int result=;
int q=;
int jinzhi[];
while (a>)
{
jinzhi[q] = a%d;
a /= d;
q++;
}
int dishu = ;
for (int i = q-; i >= ; i--)
{
result += jinzhi[i]*dishu;
dishu*=d;
}
return result;
} int main()
{
int n,d;
while (true)
{
cin>>n;
if(n<)
break; cin>>d; if(isSu(n) && isSu(change(n,d)))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
return ;
}

两点值得说明的,素数的判断没有用sqrt()而是用了i*i,还有就是我第一次错在,1不是素数上面了,其他都特别简单

 
 

PAT1015的更多相关文章

  1. PAT1015. Reversible Primes

    //题的理解是n在基数b中的的表示中,正序和逆序值都是素数,但是其实可直接判断n,因为n在b中的正常序列值就是再换成十进制就是n,但是不A:不知道为什么 用笨方法,先把n展开成b进制,正常计算其实是翻 ...

  2. PAT1015—— 德才论

    宋代史学家司马光在<资治通鉴>中有一段著名的“德才论”:“是故才德全尽谓之圣人,才德兼亡谓之愚人,德胜才谓之君子,才胜德谓之小人.凡取人之术,苟不得圣人,君子而与之,与其得小人,不若得愚人 ...

  3. 从零单排PAT1015,1016,1017,1018

    1015德才论 题目要求: 输入格式: 输入第1行给出3个正整数,分别为:N(<=105),即考生总数.L(>=60).为录取最低分数线,即德分和才分均不低于L的考生才有资格被考虑录取:H ...

  4. pat1015. Reversible Primes (20)

    1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...

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

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

随机推荐

  1. nginx: 响应体太大

    如果做proxy,可以将proxy配置修改buffer长度,或者直接关闭buffer.http { proxy_buffer_size 128k; proxy_buffers 4 256k; prox ...

  2. 移动端touch事件影响click事件的相关解决方法

    preventDefault()的方法,阻止事件的默认行为. 在移动端,手指点击一个元素,会经过:touchstart --> touchmove -> touchend -->cl ...

  3. JavaScript Function arguments.callee caller length return

    一.Function 函数是对象,函数名是指针. 函数名实际上是一个指向函数对象的指针. 使用不带圆括号的函数名是访问函数指针,并非调用函数. 函数的名字仅仅是一个包含指针的变量而已.即使在不同的环境 ...

  4. cocoaPods的安装方法

    1.打开终端 2.先升级Gem sudo gem update --system 3.切换cocoapods的数据源 [先删除,再添加,查看] gem sources --remove https:/ ...

  5. Android任务栈TaskStack

    Task:有多个Activity按顺序组成的一个完整的业务逻辑. 任务栈(TaskStack):新增的Activity放入栈中,点击back栈顶Activity从栈中退出. android:nohis ...

  6. 自定义VBS脚本(统计在指定文件中搜索字符串出现的次数)

    '=========================================================================='' VBScript Source File - ...

  7. nodejs全局安装与本地安装区别

    本地安装 1. 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modul ...

  8. HDU1236:排名

    Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑  每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后 ...

  9. StartSSL证书申请

    StartSSL官方地址: http://www.startssl.com/ 申请过程: 1)填写资料 2) 获取得验证码 3)提交验证码,等待6小时审核. 4)再次获得验证码,提交等待审核 5)审核 ...

  10. updating the chroot

    Ubuntu may stop working after a Chrome OS update. If that's the case, update all the installed targe ...