1015 Reversible Primes(20 分)

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 <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define wzf ((1 + sqrt(5.0)) / 2.0)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std; const int MAXN = 1e4 + ; int n, r; bool is_prime(int n)
{
if (n == || n == ) return false;
for (int i = ; i * i <= n; ++ i)
if (n % i == ) return false;
return true;
} int main()
{
while (scanf("%d", &n), n >= )
{
scanf("%d", &r);
if (!is_prime(n))
{
printf("No\n");
continue;
}
int cnt = , ans = ;
stack <int> my_stack;
while (n)
{
my_stack.push(n % r);
n /= r;
}
while (my_stack.size())
{
ans += my_stack.top() * (pow(r, cnt));
++ cnt;
my_stack.pop();
}
if (!is_prime(ans))
printf("No\n");
else
printf("Yes\n");
}
return ;
}

pat 1015 Reversible Primes(20 分)的更多相关文章

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

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

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

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

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

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

  4. 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 ...

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

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

  6. 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[求d进制下的逆][简单]

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

  8. PAT 1015 Reversible Primes

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

  9. PAT Advanced 1015 Reversible Primes (20) [素数]

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

随机推荐

  1. Hash Table Implementation in C++

    对于字符串,所用的hash函数为: size_t _Hash_bytes(const void* ptr, size_t len, size_t seed) { static const size_t ...

  2. Python开发【第九篇】字典

    字典 字典是一种可变的容器,可以存储任意类型的数据 字典中的每个数据都是用键进行索引,而不像序列容器(str,list,tuole)可以用整数进行索引 字典中的数据没有先后顺序,字典的存储是无序的 字 ...

  3. linux::jsoncpp库

    下载库:http://sourceforge.net/projects/jsoncpp/files/ tar -zxvf jsoncpp-src- -C jsoncpp () 安装 scons $ s ...

  4. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  5. vue——父子组件间传值

    (1)父组件给子组件传值(商品详情页): 根据订单类型,判断显示立即购买/立即拼单: 通过props来传递参数 父组件(商品详情页) 父组件调用子组件,在子组件的标签中,通过:数据名称=”数据”的形式 ...

  6. Nginx 了解一下?

    这篇文章主要简单的介绍下 Nginx 的相关知识,主要包括以下几部分内容: Nginx 适用于哪些场景? 为什么会出现 Nginx? Nginx 优点 Nginx 的编译与配置 Nginx 适用于哪些 ...

  7. Ubuntu 14.04风扇不停转,风扇狂转 的解决办法,亲测有效。

    Ubuntu 14.04风扇不停转,风扇狂转 的解决办法,亲测有效. 原文  http://ubuntuforums.org/showthread.php?t=2218367 楼主说:ubuntu 1 ...

  8. SpringBoot2.X整合Actuator

    一 说明 Actuator 的定义 actuator 是一个制造术语,指的是用于移动或控制某物的机械装置.执行器可以通过一个小的变化产生大量的运动. 要将 actuator 添加到基于 Maven 的 ...

  9. Nginx专题(1):Nginx之反向代理及配置

    摘要:本文从Nginx的概念出发,分别从反向代理的概念.优势.配置代码3个方面介绍了Nginx的特性之一反向代理. 文章来源:宜信技术学院 & 宜信支付结算团队技术分享第一期-宜信支付结算八方 ...

  10. kettle计划任务

    在kettle中固定抽取数据,需要用到kichen命令,编好批处理脚本:bat C: cd C:\soft\kettle\data-integration kitchen /file C:\soft\ ...