PAT 1015 Reversible Primes (判断素数)
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 (判断素数)的更多相关文章
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- 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分) 谜一般的题目,不就是个进制转换+素数判断
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- PAT甲题题解-1015. Reversible Primes (20)-素数
先判断n是否为素数然后把n转化成d进制下再反转,转化为十进制的num判断num是否为素数 注意n为0和1时,不是素数!!!注意反转后的num也有可能为1,不是素数!!! #include <io ...
- PAT Advanced 1015 Reversible Primes (20) [素数]
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- 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(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
随机推荐
- Paper: A novel visibility graph transformation of time series into weighted networks
1. Convert time series into weighted networks. 2. link prediction is used to evaluate the performanc ...
- ORA-00904: "I_LEVEL": invalid identifier
问题描述 ORA-00904: "I_LEVEL": invalid identifier 标示符无效
- XPath注入
XPath基础 XPath 即为 XML 路径语言,是一门在XML文档中查找信息的语言.XPath 基于 XML 的树状结构,有不同类型的节点,包括元素节点,属性节点和文本节点,提供在数据结构树中找寻 ...
- 如何着手学习WebRTC开发(转)
文章链接:http://www.sohu.com/a/146536246_458408 WebRTC中文社区-国内镜像:https://webrtc.org.cn/mirror/#windows%E2 ...
- Resharper快捷键使用
1: Alt+F7将你光标所在位置的变量的所有使用以列表的方式显示出来,显示结果的窗体可以像其他窗体那样停靠. 它的优点包括: 可以从所有使用中挑选只显示read usage或者write usa ...
- JAVASCRIPT实现的WEB页面跳转以及页面间传值方法
在WEB页面中,我们实现页面跳转的方法通常是用LINK,BUTTON LINK ,IMG LINK等等,由用户点击某处,然后直接由浏览器帮我们跳转. 但有时候,需要当某事件触发时,我们先做一些操作,然 ...
- ansible笔记(7):常用模块之包管理模块
1.yum_repository模块 用于远程管理远程主机上的yum仓库. 参数解析: name:必须参数,用于指定要操作的唯一的仓库ID,也就是.repo配置文件中每个仓库对应的“中括号”内的仓库I ...
- Oracle常用函数记录
Oracle函数 --schema:hcf --不带任何参数 http://www.cnblogs.com/wuyisky/archive/2010/05/11/oracle_function.htm ...
- (转载)Docker的boot2docker.iso镜像使用
原文路径:https://blog.csdn.net/jiangjingxuan/article/details/54908272#commentsedit 在Docker首次启动时需要下载的一个bo ...
- pyodbc-的一些说明
cursor的description 可以获得一些关于表的信息 info=cursor.description 此时表中有多少列就有多少个元素,一个元素就是一列的信息(格式是tuple),所以这里的i ...