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& ...
 
随机推荐
- arm-linux-gcc
			
搭建交叉编译环境,即安装.配置交叉编译工具链.在Ubuntu环境下编译出嵌入式Linux系统所需的操作系统.应用程序等,然后再上传到目标机上. 交叉编译工具链是为了编译.链接.处理和调试跨平台体系结构 ...
 - ntpq -p命令详解
			
ntpq用来监视ntpd操作,ntpq -p查询网络中的NTP服务器,同时显示客户端和每个服务器的关系 [root@localhost ~]# ntpq -p remote ...
 - 《TCP/IP入门经典》摘录--Part 3
			
TCP/IP协议系统 3.子网划分和CIDR 子网 划分网络 为什么需要划分子网? 子网划分就是在网络 ID 之下提供了第 2 层逻辑组织.路由器能够把数据报发送给网络里的某个子网地址(一般对应于一个 ...
 - 诡异的BUG
			
1.今天遇到一个诡异的BUG(一个很古老的项目),为什么说他诡异呢,我们本地都是OK的,但是现场部署就会报错? 2.描述下现象其实这个问题不难定位(关键是有个jar包没有源码不能进行验证性的编译) 我 ...
 - WampServer 的安装\配置和使用
			
WampServer下载地址:http://www.wampserver.com/ WampServer安装(请按序号点击) 双击WampServer安装程序 步骤①更改路径 直接点击安装 等待安装不 ...
 - python面试的100题(3)
			
3.输入日期, 判断这一天是这一年的第几天? import datetime def dayofyear(): year = input("请输入年份: ") month = in ...
 - 密码学笔记——zip明文攻击
			
明文攻击(Known plaintext attack):是一种攻击模式,指攻击者已知明文.密文及算法,求密钥的过程. 例题: 这就是一个坑 密码是十位大小写字母.数字.特殊符号组成的,你爆破的开么? ...
 - 题解【2.23考试T1】div
			
1. div[题目描述] 这是一道传统题,源代码的文件名为 div.cpp/c/pas. 给定一棵树,你要判断是否存在一条边,使得割掉这条边后,这棵树被分成了点数相等的两部分,并且如果存在,请你找到这 ...
 - easyui的combogrid
			
easyui的combogri下拉框用在项目中很多,有时会出现很多问题,当然也好解决. 1.当向后台传id值时,用户输入的与查询出来的显示值一样,但combogrid为空? 情景:输入‘李四’,和显示 ...
 - testng实现代码和数据分层
			
todo: 参考: https://www.cnblogs.com/znicy/p/6534893.html