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. intermediate-python-for-data-science

    目录 matplotlib plot Scatter Plot histogram Customization Dictionaries, Part 1 Create dictionary dicti ...

  2. 2020算法设计竞赛 C 汉诺塔

    作者:珩月链接:https://ac.nowcoder.com/discuss/367149来源:牛客网 将木板按照Xi从小到大排序,将这时的Yi数列记为Zi数列,则问题变成将Zi划分为尽可能少的若干 ...

  3. Execl导出

    1.ExeclUtil.java public class ExcelUtil { public static <T> HSSFWorkbook exprotExcel(String ti ...

  4. pycharm项目移植过程中遇到的问题

     调试中遇到三个问题: 问题1:Error running 'run_all_test': Cannot run program "C:\Users\Administrator\.virtu ...

  5. [CF1234F] Yet Another Substring Reverse - 字符串,状压DP

    CF1234F Yet Another Substring Reverse Description 给定一个字符串,可以任意翻转一个子串,求最终满足所有字符互不相同的子串的最大长度. 数据范围: \( ...

  6. 是未来还是“有毒”?紧抓球鞋风口的毒APP机遇与危机并存

    编辑 | 于斌 出品 | 于见(mpyujian) 新一代的"潮流"之风正在席卷新生代消费市场,从去年开始,国内二手球鞋交易领域突然开始火爆,大有成为新一轮"风口&quo ...

  7. 神舟战神笔记本使用U盘重装系统

    今天我的神舟战神笔记本电脑系统故障了,在这里记录一下使用U盘重装系统的方法: 1.插入装机U盘. 2.按开机键,然后马上按Esc键. 3.选择Boot Manager进入. 4.选择EFI USB D ...

  8. git 卡住推不上去

    luoxu@lenovo:~/testGit/.git$ env | grep -i proxy 查看有没有设置代理 ALL_PROXY=socks://127.0.0.1:1080/ no_prox ...

  9. JS高级---拷贝继承:把一个对象中的属性或者方法直接复制到另一个对象中

    拷贝继承:把一个对象中的属性或者方法直接复制到另一个对象中 浅拷贝 function Person() { } Person.prototype.age = 10; Person.prototype. ...

  10. 什么是nuget?nuget包是如何管理

    本文链接:https://blog.csdn.net/Microsoft_Mao/article/details/101159800做windows开发的,迟早会接触到nuget这个东西,那么今天我们 ...