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 ...
随机推荐
- ddl in PL/SQL
If you write DDL directly in PL/SQL. You will hit error. 1 DECLARE 2 str_sql varchar2(500); 3 begin ...
- spring 计时器
spring 计时器 可以这样: http://blog.csdn.net/u010648555/article/details/52162840 也可以使用annotation <!-- 设置 ...
- Notepad++支持jQuery、html5、css3
Notepad++里的代码提示文件是以XML文件存放于目录 ....\Notepad++\plugins\APIs\下的. 将这三个文件:html.xml, css.xml, javascript.x ...
- 冒泡排序Vs直接选择排序
什么是排序?为什么要使用排序?事实上我们生活中处处都用到了排序.拿字典来说,如今,我们要在字典中查找某个字(已经知道这个字的读音),首先.我们须要依据这个字的读音,找到它所所在文件夹中的位置,然后依据 ...
- 运用smali自己主动注入技术分析android应用程序行为
如今android开发人员社区里,除了app开发外,还有非常多周边的工具类产品,比方安全.性能等,app产品 已经出现了巨无霸,可是工具类的产品.眼下还没有出现规模比較大的公司,大部分还处于创业阶段, ...
- local_response_normalization 和 batch_normalization
Normalization Normalization local_response_normalization local_response_normalization出现在论文”ImageNe ...
- 【BZOJ 1230】 开关灯
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1230 [算法] 线段树 [代码] #include<bits/stdc++.h ...
- Java中继承,类的高级概念的知识点
1. 继承含义 在面向对象编程中,可以通过扩展一个已有的类,并继承该类的属性和行为,来创建一个新的类,这种方式称为继承(inheritance). 2. 继承的优点 A.代码的可重用性 B.子类可以扩 ...
- Firefox浏览器中,input输入框输入的内容在刷新网页后为何还在?
转自:http://www.webym.net/jiaocheng/473.html 这个问题比较容易解决,如果不希望浏览器保留以前输入的内容,只要给对应的 input 输入框加上以下参数: auto ...
- c++调用DOS命令,不显示黑屏
WinExec("Cmd.exe /C md c://12", SW_HIDE); 注释:/c是什么意思,不用/C会报错 CMD [/A | /U] [/Q] [/D] [/E:O ...