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 (<105) 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

思路

如果一个数为素数,且其在D进制下的反转过后的数字在10进制i下为素数,就输出Yes,否则输出No。

注意1为合数,1不是素数。会有一组数据关于这个的。

代码

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <limits.h>
using namespace std;
bool flag[500000];
void init(){
flag[1] = 1;
for(int i = 2; i * i < 500000; i++){
if(!flag[i]){
for(int j = i * i; j < 500000; j += i){
flag[j] = 1;
}
}
}
} int getNum(int N, int D){
string t = "";
while(N != 0){
t = t + char(N % D);
N /= D;
}
long long sum = 0;
int d = 1;
for(int i = t.length() - 1; i >= 0; i--){
sum += t[i] * d;
d *= D;
}
return sum;
} int main() {
int N, D;
init();
while(cin >> N){
if(N < 0) return 0;
cin >> D;
getNum(N, D);
if(!flag[N] && !flag[getNum(N, D)]) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}

PAT 1015 Reversible Primes (判断素数)的更多相关文章

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

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

  2. PAT 1015 Reversible Primes

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

  3. pat 1015 Reversible Primes(20 分)

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

  4. PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

  5. PAT甲题题解-1015. Reversible Primes (20)-素数

    先判断n是否为素数然后把n转化成d进制下再反转,转化为十进制的num判断num是否为素数 注意n为0和1时,不是素数!!!注意反转后的num也有可能为1,不是素数!!! #include <io ...

  6. PAT Advanced 1015 Reversible Primes (20) [素数]

    题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...

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

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

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

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

  9. PAT 甲级 1015 Reversible Primes(20)

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

随机推荐

  1. 跨站点脚本攻击XSS

    来源:http://www.freebuf.com/articles/web/15188.html 跨站点脚本攻击是一种Web应用程序的攻击,攻击者尝试注入恶意脚本代码到受信任的网站上执行恶意操作.在 ...

  2. Spring学习(六)

    AOP和OOP 1.OOP:Object-Oriented Programming,面向对象程序设计,是静态的,一旦类写死了就不能改变了,要更改就得修改代码重新编译,父类类型引用指向对象来实现动态性. ...

  3. zabbix4.2配置监控MySQL

    1.在被监控主机安装好MySQL 相关步骤省略. 2.创建监控所需要的MySQL账户(MySQL服务器端) MariaDB [(none)]>grant usage on *.* to zabb ...

  4. EF工作流程

    1.EF基本的CRUD(增删改查)流程 2.EF的工作过程简析 EDM--Entity Data Model--实体数据模型 概念模型: EF通过领域类,上下文类,默认约定和配置构建概念模型. 存储模 ...

  5. css+div上下左右自适应居中

    主要记录自己日常积累的布局相关的东西,持续更新中. 1.登录框上下左右自适应居中 以前想要把登录表单始终放置在页面的中间,花了不少心思,一直以来用的解决方法都是用js,感觉有点麻烦不是很好,于是在网上 ...

  6. Oracle 11g安装 —— Oracle Database 11g Release2 for Windows(x64)

    文章来自:https://blog.csdn.net/IT_xiao_guang_guang/article/details/104422421 下面是我的Oracle 11g安装过程,希望可以帮到正 ...

  7. AcWing 792. 高精度减法

    https://www.acwing.com/problem/content/794/ #include<bits/stdc++.h> using namespace std; //判断是 ...

  8. 关于BaiduPSC-Go的一些bug的更正

    首先说下操作步骤 下载是在GutHub,这个不赘述,网上很多资料 下载之后配置环境变量,在path的后面加上一个分号,然后加上你下载的目录,目录名最好为英文 然后通过命令行CMD工具,输入BaiduP ...

  9. 2019牛客多校第一场E ABBA dp

    ABBA dp 题意 给出2(N+M)个AB字符,问能构造出N个AB子序列和M个BA子序列组成的2*(n+m)的序列种类有多少 思路 碰到计数构造类的题目,首先要去找到判断合法性的条件,即什么情况下合 ...

  10. Custom LED Keychain, Small And Surefire Gifts

    The    LED Keychain    makes it easy for people to carry their keys with them and carry them with th ...