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))的更多相关文章

  1. PAT 甲级 1015 Reversible Primes(20)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  2. pat 1015 Reversible Primes(20 分)

    1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...

  3. PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  4. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...

  5. PAT 1015 Reversible Primes

    1015 Reversible Primes (20 分)   A reversible prime in any number system is a prime whose "rever ...

  6. PAT 1015 Reversible Primes[求d进制下的逆][简单]

    1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...

  7. 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 ...

  8. 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 ...

  9. PAT 1049 数列的片段和(20)(代码+思路分析)

    1049 数列的片段和(20)(20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2 ...

  10. PAT 1033 旧键盘打字(20)(20 分)

    1033 旧键盘打字(20)(20 分) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在2 ...

随机推荐

  1. 转 -- OK6410 tftp下载内核、文件系统以及nand flash地址相关整理、总结

    转载地址:http://emouse.cnblogs.com/ 飞凌官方提供了一键下载烧写linux的方式,相对来说比较方便,但是对于开发来说不够灵活,因此这篇文章把tftp相关的点介绍一下,整理下其 ...

  2. 【leetcode 简单】 第八十三题 反转字符串中的元音字母

    编写一个函数,以字符串作为输入,反转该字符串中的元音字母. 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leet ...

  3. JavaScript数组的概念

    数组 1.数组是什么?   数组就是一组变量存放在里面就是数组.   例如:var list=['apple','goole','alibaba',520]   (1.这些数据有一些相关性的.   ( ...

  4. node.js 开发环境搭建

    node.js下载地址 https://nodejs.org/download/ windows系统建议下载 msi 安装完成配置环境变量(根据安装路径来) NODE_PATH=C:\Program ...

  5. jQuery入门——(二)

    0.基本知识 $与jQuery等价,$.fun代表jQuery的全局方法. jQuery必须首先导入JQuery库, jQuery的事件都不带on,例如 $("#btn").cli ...

  6. 【黑客免杀攻防】读书笔记15 - 源码免杀、C++壳的编写

    1.源码免杀 1.1 定位产生特征的源码 定位文件特征 1.根据MyCCL的特征码定位工具,定位出有特征的地址 2.根据VS的反汇编窗口,输入有特征的地址得到特征地址与源码的关系 3.插入Messag ...

  7. Testbench学习——$fopen/$display/$fclose

    昨天在用Vivado写Testbench顶层时,为了以后便于数据的存储导出分析,需要用的文件数据记录的功能,于是,下面谈谈$fopen/$display/$fclose这三者的用法. $fopen—— ...

  8. ubuntu git 简单入门【转】

    转自:http://blog.chinaunix.net/uid-20718384-id-3334859.html 1. 安装 sudo apt-get install git-core 2.  初始 ...

  9. 【2017-10-1】雅礼集训day1

    今天的题是ysy的,ysy好呆萌啊. A: 就是把一个点的两个坐标看成差分一样的东西,以此作为区间端点,然后如果点有边->区间没有交. B: cf原题啊.....均摊分析,简单的那种. 线段树随 ...

  10. Pytorch数据读取框架

    训练一个模型需要有一个数据库,一个网络,一个优化函数.数据读取是训练的第一步,以下是pytorch数据输入框架. 1)实例化一个数据库 假设我们已经定义了一个FaceLandmarksDataset数 ...