1015 Reversible Primes (20)(20 point(s))
problem
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 (< 10^5^) and D (1 < D <= 10), 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 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
answer
#include<bits/stdc++.h>
using namespace std;
int prime[100000] = {2, 3};
set<int> s;
void Prime(){
s.insert(2);
s.insert(3);
int i, j, flag, ta = 2;
for(i = 5; i < 100010; i+=2){
for(j = 0, flag = 1; prime[j]*prime[j] <= i; j ++){
if(i % prime[j] == 0) {
flag = 0; break;
}
}
if(flag){
prime[ta++] = i;
s.insert(i);
}
}
}
bool isPrime(int num)
{
set<int>::iterator it;
it = s.find(num);
if(it != s.end()) return true;
else return false;
}
int Reverse(int a, int d){
vector<int > s;
while(a > 0){
s.push_back(a%d);
a/=d;
}
int num = 0;
reverse(s.begin(), s.end());
for(int i =0; i< s.size(); i++){
num += s[i]*pow((float)d, i);
// cout<<s[i];
}
return num;
}
int main(){
ios::sync_with_stdio(false);
// freopen("test.txt", "r", stdin);
Prime();
int N, M;
while (cin>>N){
if(N < 0) break;
cin>>M;
if(!isPrime(N) || !isPrime(Reverse(N,M))) {
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
}
return 0;
}
experience
- 素数晒法,背下模板。
1015 Reversible Primes (20)(20 point(s))的更多相关文章
- 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 "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 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PAT 甲级 1011 World Cup Betting (20)(20 分)
1011 World Cup Betting (20)(20 分)提问 With the 2010 FIFA World Cup running, football fans the world ov ...
- PAT 甲级 1001 A+B Format (20)(20 分)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT 1049 数列的片段和(20)(代码+思路分析)
1049 数列的片段和(20)(20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2 ...
- PAT 1033 旧键盘打字(20)(20 分)
1033 旧键盘打字(20)(20 分) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在2 ...
随机推荐
- CentOS Linux 7 安装教程
建立新的虚拟机 将CentOS 7 ISO文件插入到CD-Rom 启动虚拟机,F12选择启动方式为CD/DVD 选择Install CentOS Linux 7 加载安装必要文件 选择安装过程所显示的 ...
- 在window 8 或windows2012 上用命令行安装framework3.5 方法
找到对应操作系统安装目录的sources文件夹下的sxs文件夹,拷贝到本地电脑,如F:盘 根目录下 CMD(管理员身份)命令: dism.exe /online /enable-feature /fe ...
- 状压dp(B - 炮兵阵地 POJ - 1185 )
题目链接:https://cn.vjudge.net/contest/276236#problem/B 题目大意:略 具体思路:和我的上一篇写状压dp的思路差不多,不过就是这个题相当于上一个题的升级 ...
- reshape中的-1
>>> a = np.array([[1,2,3], [4,5,6]]) >>> np.reshape(a, (3,-1)) # the unspecified v ...
- Exif xss
这种XSS出现的状况会特别少. Exif是啥??? 可交换图像文件格式(英语:Exchangeable image file format,官方简称Exif),是专门为数码相机的照片设定的,可以记录数 ...
- 理解mipi协议【转】
转自:http://blog.csdn.net/wanglining1987/article/details/50202615 完成mipi信号通道分配后,需要生成与物理层对接的时序.同步信号: MI ...
- go语言版本变化
The Go Project Go is an open source project developed by a team at Google and many contributors ...
- vue总结 03过滤器
过滤器 Vue.js 允许你自定义过滤器,可被用于一些常见的文本格式化.过滤器可以用在两个地方:双花括号插值和 v-bind 表达式 (后者从 2.1.0+ 开始支持).过滤器应该被添加在 JavaS ...
- hive数据倾斜问题
卧槽草草 来源于其它博客: 貌似我只知道group by key带来的倾斜 hive在跑数据时经常会出现数据倾斜的情况,使的作业经常reduce完成在99%后一直卡住,最后的1%花了几个小时都没跑完, ...
- JS动态创建元素(两种方法)
前言 创建元素有两种方法 1)将需要创建的元素,以字符串的形式拼接:找到父级元素,直接对父级元素的innnerHTML进行赋值. 2)使用Document.Element对象自带的一些函数,来实现动态 ...