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. 【23】备忘录模式(Memento Pattern)

    一.引言 在上一篇博文分享了访问者模式,访问者模式的实现是把作用于某种数据结构上的操作封装到访问者中,使得操作和数据结构隔离.而今天要介绍的备忘者模式与命令模式有点相似,不同的是,命令模式保存的是发起 ...

  2. Harbor 搜索镜像及查看 tag

    在我们搭建完 Harbor 后: https://www.cnblogs.com/klvchen/p/9482153.html 如果想要通过 API 获取 Harbor 上面的镜像及 tag 可以使用 ...

  3. CSS总结div中的内容垂直居中的五种方法

    一.行高(line-height)法 如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如: p { height:30px; line-height:3 ...

  4. 2018-01-19 Xtext试用: 快速实现简单领域专用语言(DSL)

    环境搭建 使用的Eclipse版本: Oxygen.1a Release (4.7.1a) Build id: 20171005-1200, 通过添加Xtext - Download上列出的Relea ...

  5. html学习笔记——ife task0001

    花了两三天大概看完html和css基本用法,但到自己布局的时候还是很懵不知道从哪里入手啊,就找了个简单的任务(ife2015 spring)试一下. 之前不涉及到布局的跳过,从涉及到position和 ...

  6. php使用PHPexcel类读取excel文件(循环读取每个单元格的数据)

    error_reporting(E_ALL); date_default_timezone_set('Asia/ShangHai'); include_once('Classes/PHPExcel/I ...

  7. linux设置自动更换壁纸

    #!/bin/bash let n=0 files=($HOME/wallpapers/*.jpg) count=${#files[@]} while [ 1 ] do let "n=n%$ ...

  8. 洗礼灵魂,修炼python(17)--跨平台操作三剑客—os,os.path.sys模块

    os 1.作用: 因为客户基本都是使用不同的操作系统,在不同的系统下,要完成一个项目,那必须跨平台操作,而python本来就是一个跨平台的语言,而有了os模块,则不需要在意什么系统.并且os模块是用于 ...

  9. python曲线拟合

    http://blog.sina.com.cn/s/blog_aed5bd1d0102vid7.html 1.多项式拟合范例: import matplotlib.pyplot as plt impo ...

  10. [MapReduce_1] 运行 Word Count 示例程序

    0. 说明 MapReduce 实现 Word Count 示意图 && Word Count 代码编写 1. MapReduce 实现 Word Count 示意图 1. Map:预 ...