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 ...
随机推荐
- ZooKeeper是什么(转)
ZooKeeper是什么? ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提 ...
- N天学习一个linux命令之kill
用途 用于终止进程 用法 kill [-s signal|-p] [--] pid... kill -l [signal] 说明 1.默认发送信号15(请求终止进程,程序可以捕获,操作系统会杀死没有对 ...
- Android:创建ZeroMQ的客户端
这里我们将给出一个subscribe模式的客户端示例程序,如下: private class ZeroMQMessageTask extends AsyncTask<String, Void, ...
- HDU 4524
简单题,先从右边消起,注意结束时a[1]==0才能是yes #include <iostream> #include <cstdio> #include <cstring ...
- hdu1595find the longest of the shortest 最短路
//给一个无向图,问删除一条边,使得从1到n的最短路最长 //问这个最长路 //这个删除的边必定在最短路上,假设不在.那么走这条最短路肯定比其它短 //枚举删除这条最短路的边,找其最长的即为答案 #i ...
- 【原创】TCP超时重传机制探索
TCP超时重传机制探索 作者:tll (360电商技术) 1)通信模型 TCP(Transmission Control Protocol)是一种可靠传输协议.在传输过程中当发送方(sender)向接 ...
- 读取到配置文件的C语言的接口实现
/********************************************************************* * Author : Samson * Date ...
- Lucene5学习之使用MMSeg4j分词器
分类:程序语言|标签:C|日期: 2015-05-01 02:00:24 MMSeg4j是一款中文分词器,详细介绍如下: 1.mmseg4j 用 Chih-Hao Tsai 的 MMSeg 算法( ...
- iOS7系统iLEX RAT冬青鼠安装教程:无需刷机还原纯净越狱系统
全网科技 温馨提醒:iLEX RAT和Semi-Restore的作用都是让你的已越狱的设备恢复至越狱的初始状态. 可是要注意无论你是用iLexRAT冬青鼠还是Semi-restore.对于还原来说都存 ...
- IE訪问Oracle EBS打不开Form的问题
IE訪问Oracle EBS打不开Form的问题 例如以下图. 最后我才知道真正的原因.原来是兼容性视图的问题. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5 ...