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 (<10​5​​) 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

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
using namespace std;
const int maxn = ;
int n, d;
int to_10(int i,int radix) {
int res = ,exp=;
while (i != ) {
res += i%*pow(radix, exp);
exp++;
i /= ;
}
return res;
}
bool is_prime(int i) {
if (i == || i == )return false;
if (i == || i == )return true;
for (int j = ; j*j <= i; j++) {
if (i%j == )return false;
}
return true;
}
string to_s(int i,int radix) {
string s = "";
do {
s += '' + i % radix;
i /= radix;
} while (i != );
reverse(s.begin(), s.end());
return s;
}
int to_num(string s) {
int res = ;
for (int i = s.length()-; i >=; i--) {
res = res * + s[i] - '';
}
return res;
}
int main() {
while (true) {
scanf("%d", &n);
if (n < )break;
else {
scanf("%d", &d);
if (is_prime(n) && is_prime(to_10(to_num(to_s(n,d)), d))) printf("Yes\n");
else printf("No\n");
}
}
system("pause");
return ;
}

注意点:题目没看懂,导致结果一直错。题目的意思是一个10进制数如果他本身是素数,然后转换到给定进制下并将其反转,再转化到10进制,还是素数的话即为 Yes。

PAT A1015 Reversible Primes (20 分)——进制转换,质数的更多相关文章

  1. PAT-1015 Reversible Primes (20 分) 进制转换+质数

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

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

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

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

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

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

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

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

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

  6. PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  7. 【PAT甲级】1015 Reversible Primes (20 分)

    题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...

  8. 1015 Reversible Primes (20 分)

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  9. PAT 甲级 1019 General Palindromic Number (进制转换,vector运用,一开始2个测试点没过)

    1019 General Palindromic Number (20 分)   A number that will be the same when it is written forwards ...

随机推荐

  1. Java基础——Oracle(五)

    一.Oracle  中的分页 1) select * from emp; 2)select * ,rownum from emp; //这样写不行 3)select ename,job,sal,row ...

  2. idea 中使用 jetty 插件

    参考博客:http://blog.csdn.net/xiejx618/article/details/49936541

  3. docker swarm 搭建及跨主机网络互连案例分析

    准备工作 安装docker,不建议直接使用Docker官方的yum install docker wget http://yum.dockerproject.org/repo/main/centos/ ...

  4. 免费工资总额管控系统-JXHR2016

    •工资总额是指按照国家统计局规定的统计口径或企业规定,在一定时期内支付给各类用工的劳动报酬总额 •工资总额,即基本工资,包括岗位工资.各项津补贴 •JXHR2016以薪酬管控为核心,结合人力资源规划. ...

  5. CloudSim源代码学习——任务单元(Cloudlet)

    /* * Title: CloudSim Toolkit * Description: CloudSim (Cloud Simulation) Toolkit for Modeling and Sim ...

  6. WebLogic 8的安装与配置详谈

    本文主要是以windouw下32位的版本为例展开介绍,主要包括其安装与配置. 一.图形界面安装过程 1.双击安装程序server815_win32.exe,开始进行程序的安装. 2.点击Next按钮进 ...

  7. cve-list

    dlink CVE-2018-17786 CVE-2018-17787 CVE-2018-17880 CVE-2018-17881 mongoose CVE-2018-10945 openwrt CV ...

  8. Redis系列(一):Redis的简介与安装

    原文链接(转载请注明出处):Redis系列(一):Redis的简介与安装 什么是 Redis Redis 是一个使用ANSI C 编写的开源.支持网络协议.基于内存.可选持久性的键值对数据库,它是一个 ...

  9. WebSocket实现简单的在线聊天

    SuperWebSocket在WebService中的应用 最开始使用是寄托在IIS中,发布之后测试时半个小时就会断开,所以改为WindowsService 1. 新建Windows服务项目[Test ...

  10. 【PHP调试篇】PHP高性能日志组件SeasLog

    简述 什么是SeasLog SeasLog是一个C语言编写的PHP扩展,提供一组规范标准的功能函数,在PHP项目中方便.规范.高效地写日志,以及快速地读取和查询日志. 为什么使用SeasLog 无论在 ...