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 ...
随机推荐
- oracle rac cache fusion
转载自 http://blog.csdn.net/tianlesoftware/article/details/6534239 Introduction This post is about orac ...
- AutoCAD 2014:安装时发生allied product not found错误
有个朋友在安装AutoCAD 2014时不慎误删了一个文件夹,结果导致安装AutoCAD时总是跳出”allied product not found”的错误. Google搜了下,解决方案如下: 1. ...
- MDA模型定义及扩展
Tiny框架中.对模型本向没有不论什么强制性约束,也就是说你能够把不论什么类型的对象作为模型.也不必实现不论什么接口. 因此简单的说,你定义一个类.里面有一些描写叙述业务属性或处理的内容,就能够说它是 ...
- 移植DirectFB于SOC3210(龙芯)【转】
本文转载自:http://blog.chinaunix.net/uid-25298908-id-120188.html 编译平台:龙芯.中标普华Linux桌面5 目标平台:SOC3210 一.获取源码 ...
- 洛谷 P3112 后卫马克 —— 状压DP
题目:https://www.luogu.org/problemnew/show/P3112 状压DP...转移不错. 代码如下: #include<iostream> #include& ...
- Section %post does not end with %end
Section %post does not end with %end Exception AttributeError: "NoneType" object no attrib ...
- 1tb等于多少g 1TB和500G有什么区别
转自:http://www.a207.com/article/view_39392 移动硬盘.U盘是生活中常见的用品,他们的内存大小是什么标准.很多人对于1tb等于多少g和1tb和500g有什么区别不 ...
- XHprof 使用 (转)
原文地址:http://blog.csdn.net/maitiandaozi/article/details/8896293 XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟 ...
- A. Jeff and Digits(cf)
A. Jeff and Digits time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 手机网站下拉加载数据js(简单版)
加载内容的地方html <div class="bgcolor_f0 clearfix"> <div class="recharge"> ...