PAT_A1015#Reversible Primes
Source:
Description:
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 Nand D. The input is finished by a negative N.
Output Specification:
For each test case, print in one line
Yesif N is a reversible prime with radix D, orNoif not.
Sample Input:
73 10
23 2
23 10
-2
Sample Output:
Yes
Yes
No
Keys:
- 进制转换
- 素数(Prime)
Attention:
- int < 1e9 ~ 2^8-1, long < 1e18 ~ 2^16-1;
- 1e5的十进制转换为二进制会超过int范围,进制转换和逆置一起进行的话就避免中间数超出范围的问题了;
Code:
/*
Data: 2019-05-12 21:32:05
Problem: PAT_A1015#Reversible Primes
AC: 18:26 题目大意:
给一整数N<1e5,进制D
求N的D进制逆置后是否仍是素数
*/
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
const int M=1e5+; bool isPrime(int num)
{
if(num== || num==)
return false;
for(int i=; i*i<=num; i++)
if(num%i==)
return false;
return true;
} int Reverse(int n, int d)
{
int ans=;
while(n!=)
{
ans *= d;
ans += (n%d);
n /= d;
}
return ans;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,d;
while(scanf("%d",&n) && n>=)
{
scanf("%d", &d);
if(isPrime(n) && isPrime(Reverse(n,d)))
printf("Yes\n");
else
printf("No\n");
} return ;
}
PAT_A1015#Reversible Primes的更多相关文章
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT 甲级 1015 Reversible Primes(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PTA (Advanced Level) 1015 Reversible Primes
Reversible Primes A reversible prime in any number system is a prime whose "reverse" in th ...
- pat1015. Reversible Primes (20)
1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...
- pat 1015 Reversible Primes(20 分)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT A1015 Reversible Primes (20 分)——进制转换,质数
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
随机推荐
- spring历史背景
1.2004年spring出现第一版本spring frameworl1.0 2.写代码永远是最简单的,后续的运维工作才是让人感到无助的 3.spring boot在运维方面做了很多工作,部署,监控, ...
- AngularJS:让submit重新生效
当我们在html中声明了ng-app后,form的submit就会失效,必须通过angularJS来处理.如果这时还是想用普通的方式提交的话,需要修改form标签,如下所示: <form met ...
- AngularJS:一行JS代码实现控件验证效果
如上图所示,我们需要实现如下这些验证功能: 控件都是必输控件 都需要控制最大长度 第一次打开页面,控件不能显示为错误状态 输入内容再清空后,必输控件需要显示为错误状态 只有所有输入合法后,发布按钮才能 ...
- [Javascript] AbortController to cancel the fetch request
We are able to cancel the fetch request by using AbortController with RxJS Observable. return Observ ...
- js获得子页面元素
在主页面中.添加iframe子页面,假设须要获得子页面的元素则能够使用javascript的contentWindow来获得,注意iframe和frameset必须加入id.详细样例例如以下: mai ...
- 垃圾回收GC:.Net自己主动内存管理 上(二)内存算法
垃圾回收GC:.Net自己主动内存管理 上(二)内存算法 垃圾回收GC:.Net自己主动内存管理 上(一)内存分配 垃圾回收GC:.Net自己主动内存管理 上(二)内存算法 垃圾回收GC:.Net自己 ...
- 对扩展openflow协议的一点思考
软件定义X变得越来越火,正所谓,Software is eating the world. 软件定义网络也是如此.不论是在工业界还是学术界都将是一次伟大的革命,都在紧随着这个行业的方向,找自 ...
- etcd数据备份与恢复验证
一.单机 说明:执行etcd备份数据的恢复的机器必须和原先etcd所在机器一致 1.单机备份 etcdctl --endpoints="https://10.25.72.62:2379&qu ...
- selenium3 + python - cookie定位
from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitimport time d ...
- Python 40 数据库-外键约束 、多对一与多对多的处理
mysql提供了 foreign key,专门用于为表和表之间 建立物理关联 思考 表里存储的是一条条的记录,两个表之间能产生的关系有哪些? 现有 A B两张表 1.多对一 2.一对一 ...