Source:

PAT A1015 Reversible Primes (20 分)

Description:

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 Nand 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

Keys:

  • 进制转换
  • 素数(Prime)

Attention:

  • int < 1e9 ~ 2^8-1, long < 1e18 ~ 2^16-1;
  • 1e5的十进制转换为二进制会超过int范围,进制转换和逆置一起进行的话就避免中间数超出范围的问题了;

Code:

 /*
Data: 2019-05-12 21:32:05
Problem: PAT_A1015#Reversible Primes
AC: 18:26 题目大意:
给一整数N<1e5,进制D
求N的D进制逆置后是否仍是素数
*/
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
const int M=1e5+; bool isPrime(int num)
{
if(num== || num==)
return false;
for(int i=; i*i<=num; i++)
if(num%i==)
return false;
return true;
} int Reverse(int n, int d)
{
int ans=;
while(n!=)
{
ans *= d;
ans += (n%d);
n /= d;
}
return ans;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,d;
while(scanf("%d",&n) && n>=)
{
scanf("%d", &d);
if(isPrime(n) && isPrime(Reverse(n,d)))
printf("Yes\n");
else
printf("No\n");
} return ;
}

PAT_A1015#Reversible Primes的更多相关文章

  1. PAT 1015 Reversible Primes

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

  2. PAT 甲级 1015 Reversible Primes(20)

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

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

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

  4. PTA (Advanced Level) 1015 Reversible Primes

    Reversible Primes A reversible prime in any number system is a prime whose "reverse" in th ...

  5. pat1015. Reversible Primes (20)

    1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A r ...

  6. pat 1015 Reversible Primes(20 分)

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

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

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

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

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

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

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

随机推荐

  1. HDU 3167 KMP

    很久之前做的一题,忽然想起来,依然觉得思路巧妙. //这道题,确实是一道好题.但如何应用KMP,确实大大超出了意料中. //这道题匹配的是某元素在子串中的名次,也就是在子串中排第几小.我想了整整一天, ...

  2. Linux下route add 命令加入路由列表

    route add命令的主要作用是加入静态路由,通常的格式是: route ADD 157.0.0.0 MASK 255.0.0.0  157.55.80.1 METRIC 3 IF 2 參数含义:d ...

  3. Gevent的协程实现原理

    之前之所以看greenlet的代码实现,主要就是想要看看gevent库的实现代码. .. 然后知道了gevent的协程是基于greenlet来实现的...所以就又先去看了看greenlet的实现... ...

  4. Python3.4 远程操控电脑(开关机)

    import poplib import sys import smtplib from email.mime.text import MIMEText import os from email.he ...

  5. java 集合交并补

    通过使用泛型方法和Set来表达数学中的表达式:集合的交并补.在下面三个方法中都将第一个參数Set复制了一份,并未直接改动參数中Set. package Set; import java.util.Ha ...

  6. C++_homework_StackSort

    顾名思义(?)类似于单调栈?维护一个单调递减的栈.一旦准备入栈的元素大于栈顶元素,栈一直弹出直到准备入栈的元素小于等于栈顶元素,弹出的元素压入另一个tmp栈中. #include <iostre ...

  7. CentOs7 修改rpm安装背景图

    http://bbs.chinaunix.net/thread-4166176-1-1.html

  8. Android对话框与Activity共存时的异常

    异常提示信息 01-01 18:30:38.630: E/WindowManager(14537): Activity com.jack.outstock.activity.ManageCustomA ...

  9. XML案例(使用JAXP进行DOM解析)

    1.book.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> ...

  10. PCB MS SQL 通过表名查询各字段信息和vb.net C# module类代码

    正式表:各字段内容获取 ) SET @tabname = 'ppeflow' SELECT @tabname AS '表名' ,(CASE ))+ ')' )) ) )) + ')' )) ) )) ...