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. Java修饰符类型

    转自原文:http://www.yiibai.com/java/java_modifier_types.html 修饰符是添加到这些定义来改变它们的含义的关键词. Java语言有各种各样修饰词,其中包 ...

  2. @Html.DropDownList()的四种用法及自定义DropDownList扩展

    https://blog.csdn.net/xiaouncle/article/details/82856982

  3. SequoiaDB巨杉数据库入门:快速搭建流媒体服务器

    使用SequoiaDB的分布式文件系统搭建流媒体服务器 介绍 如今使用移动互联网的年轻人开始越来越多使用短视频展示自我,而流媒体则是支撑在线视频播放的核心技术.当我们开始构建流媒体站点时,往往面临最大 ...

  4. ORA-01476: 除数为 0

    假设是a/bdecode(b,0,null,a/b) 这样如果b为0,输出null,不为0输出a/b decode():将查询结果翻译成其他值,类似三目运算符 比较1个参数时      decode( ...

  5. SQL Server 2008创建数据库

    1.数据.数据库.数据管理系统基本概念: 数据:人类有用信息的符号化表示. 数据库:按照数据结构来组织.存储和管理数据的一个仓库. 数据库管理系统(DBMS):可维护.存储并为应用系统提供数据的软件系 ...

  6. too old

    The working copy at “” is too old (format 10) to work with client version ‘1.9.7(r18000392)’ 原因:svn版 ...

  7. Java基础之使用Scanner类获取用户输入

    创建 Scanner 对象的基本语法: Scanner s = new Scanner(System.in); Scanner 类的 next() 方法 import java.util.Scanne ...

  8. 【NOIP2012普及组】质因数分解

    P1075 质因数分解 假期第一天就给一道入门难度的题写题解…… 这道题一开始就被我想复杂了:埃式筛,欧拉筛……然而开一个1e9的数组?不现实. 直到看到题解区的dalao用唯一分解定理: 算术基本定 ...

  9. 题解 Fractal Streets

    题目链接 参考博客 #include<cstdio> #include<math.h> #include<utility>//pair using namespac ...

  10. oracle分组并在组内排序

    根据c1,c2分组,并且根据c3排序,取第一行select tt.*  from (select row_number() over(partition by c1, c2 order by c3 d ...