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 (<) and D (1), 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

其实就是将输入的数字转化为该进制的反序列,然后再转化为10进制
然后判断是否是素数即可

 #include <iostream>
#include <math.h>
#include <string> using namespace std; bool isPrim(int a)
{
if (a < )
return false;
int b = (int)sqrt(1.0 * a);
for (int i = ; i <= b; ++i)
{
if (a%i == )
return false;
}
return true;
} int getReverNum(int a, int b)
{
string str = "";//得到反转的序列
while (a > )
{
str += a % b + '';
a /= b;
}
int n = ;
for (int i = str.length() - , j = ; i >= ; --i, ++j)
n += (str[i] - '')*pow(b, j);
return n;
} int main()
{
int a, b;
while (cin >> a)
{
if (a < )break;
cin >> b;
if (isPrim(a) && isPrim(getReverNum(a,b)))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return ;
}

PAT甲级——A1015 Reversible Primes的更多相关文章

  1. PAT 甲级 1015 Reversible Primes(20)

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

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

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

  3. PAT 甲级 1015 Reversible Primes

    https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000 A reversible prime in ...

  4. PAT A1015 Reversible Primes (20 分)——进制转换,质数

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

  5. A1015. Reversible Primes

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

  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_A1015#Reversible Primes

    Source: PAT A1015 Reversible Primes (20 分) Description: A reversible prime in any number system is a ...

  8. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

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

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

随机推荐

  1. CSS 继承和优先级

    CSS继承性 CSS属性继承:外层元素的样式,会被内层元素进行继承. 多个外层元素的样式,最终都会“叠加”到内层元素上. 什么样的CSS属性能被继承呢? CSS文本属性都会被继承的: color. f ...

  2. PagedListCore的使用

    关于在Core2.0中PagedListCore实现分页 一.引言 开发中在对大量数据展示的时候,出于对性能的考虑,我们往往是使用分页功能(用户需要那一页我们就返回给他那一页,而不是一次性加载所有的数 ...

  3. USACO 2006 November Gold Corn Fields /// 状压 oj23941

    题目大意: 输入n m 接下来n行m列 0表示不能种玉米 1表示能 要求种玉米位置的上下左右四连通区域不能种玉米 输出方案数 Sample Input 2 31 1 10 1 0 Sample Out ...

  4. 主页面与iframe页面之间的javascript函数的调用

    1:在主页面里调用iframe页里面的javascript函数 <script type="text/javascript"> var childWindow = $( ...

  5. Neo4j-APOC使用总结(一)

    一.安装APOC 1.下载jar包:https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases 2.把jar包放在安装目录的plug ...

  6. linux流量监控iftop命令安装详解

    iftop跟nload差不多,也是捕获网卡流量的命令,nload的安装见之前发布的教程:http://www.cnblogs.com/catlee/p/5703541.html 开始安装.本文以cen ...

  7. Nand Flash 控制器中的硬件 ECC 介绍

    ECC 产生方法 ECC 是用于对存储器之间传送数据正确进行校验的一种算法,分硬件 ECC 和软件 ECC 算法两种,在 S3C2410 的 Nand Flash 控制器中实现了由硬件电路(ECC 生 ...

  8. CSS选择器及优先级

    转自CSS优先级的计算公式:http://wyz.67ge.com/css-selector-priority/ 通常我们可以将CSS的优先级由高到低分为六组: 无条件优先的属性只需要在属性后面使用 ...

  9. xcart小数点位数

    xcart小数点的位数默认是2位,有时候需要根据需要更改位数:一开始以为把数据库中的数据类型的位数更改过后,就能生效,结果发现xcart在程序中作了限制,只能是2位.那么只能通过更改程序的方式来更改了 ...

  10. CF875E Delivery Club

    题意:两个邮递员,一个初始在s1,s2.需要依次给x1,x2,...,xn送快递.求所有时刻中两个邮递员的距离最大值的最小值.n<=100000,xi<=1e9. 标程: #include ...