pat 1015 Reversible Primes(20 分)
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 (<105) 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 分)的更多相关文章
- PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- 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 ...
- 【PAT甲级】1015 Reversible Primes (20 分)
题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...
- 1015 Reversible Primes (20 分)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT Advanced 1015 Reversible Primes (20) [素数]
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
随机推荐
- PHP stream_wrapper_register
<?php /** * 引用:http://php.net/manual/en/function.stream-wrapper-register.php * 把变量当成文件读写的协议 * * C ...
- 概念理解-异步IO
#include <aio.h> /* 函数名 :int aio_write(struct aiocb *aiocbp) 参 数 :struct aiocb *aiocbp 返回值 :执行 ...
- 9.Linux用户管理(下)
1. 为用户添加密码 [root才能执行] 1为新用户添加密码{只能是root} {密码尽可能的复杂} [0-9][a-Z][a-Z] [!@#$%^&]* [root@yinwucheng ...
- ESP8266开发之旅 网络篇⑫ 域名服务——ESP8266mDNS库
1. 前言 前面的博文中,无论是作为client端还是server端,它们之间的通信都是通过具体的IP地址来寻址.通过IP地址来寻址,本身就是一个弊端,用户怎么会去记住这些魔法数字呢?那么有没 ...
- Mybaits 源码解析 (五)----- 面试源码系列:Mapper接口底层原理(为什么Mapper不用写实现类就能访问到数据库?)
刚开始使用Mybaits的同学有没有这样的疑惑,为什么我们没有编写Mapper的实现类,却能调用Mapper的方法呢?本篇文章我带大家一起来解决这个疑问 上一篇文章我们获取到了DefaultSqlSe ...
- (day33)数据库
目录 1. 数据库是什么 2. 为什么使用数据库 3. 数据库的分类 1. 关系型数据库 2. 非关系型数据库 4. mysql的架构 5. mysql的安装 1. windows的安装 2. win ...
- SpringBoot整合RabbitMq(二)
本文序列化和添加package参考:https://www.jianshu.com/p/13fd9ff0648d RabbitMq安装 [root@topcheer ~]# docker ...
- expect实现自动输入密码功能
系统: Ubuntu:16.04 安装expect: sudo apt-get update sudo apt-get install expect 脚本实例: //这一行告诉操作系统脚本里的代码使用 ...
- [AI开发]零数学公式告诉你什么是(卷积)神经网络
大部分介绍神经网络的文章中概念性的东西太多,而且夹杂着很多数学公式,读起来让人头疼,尤其没什么基础的人完全get不到作者想要表达的思想.本篇文章尝试零公式(但有少量数学知识)说清楚什么是神经网络,并且 ...
- xss代码集
</script>"><script>prompt(1)</script> </ScRiPt>"><ScRiPt& ...