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 ...
随机推荐
- Intel Ivy Bridge Microarchitecture Events
This is a list of all Intel Ivy Bridge Microarchitecture performance counter event types. Please see ...
- pytest6-Fixture finalization / executing teardown code(使用yield来实现)
Fixture finalization / executing teardown code By using a yield statement instead of return, all the ...
- chrome devtools tip(1)--调试伪类
开发中我们经常遇到,添加些focus,hover事件,样式,但当我们去打开 chrome devtools,浮动上去的时候,然后准备去改变样式的时候,结果由于光标移动了,样式不见了,非常不方便调试,其 ...
- ElasticSearch安装及使用
ElasticSearch安装及使用 ELK由Elasticsearch.Logstash和Kibana三部分组件组成. Elasticsearch 是个开源分布式搜索引擎,它的特点有:分布式,零配置 ...
- git jenkins 基本部署 gitlab私有仓库
从代码私有性方面来看,公司不希望员工获取到全部的代码,这个时候 GitLab 无疑是最佳的选择.但对于开源项目而言,GitHub 依然是代码托管的首选平台. 1.安装gitlab[root@gitla ...
- lable的渲染
<StackPanel Margin=" TextOptions.TextFormattingMode="Display"> <Label TextOp ...
- Apache2的安装
Apache2的安装 1.执行:sudo apt-get install apache2. 2.sudo vim /etc/apache2/apache2.conf在最后加上:ServerName l ...
- 数据结构(四十四)交换排序(1.冒泡排序(O(n²))2.快速排序(O(nlogn))))
一.交换排序的定义 利用交换数据元素的位置进行排序的方法称为交换排序.常用的交换排序方法有冒泡排序和快速排序算法.快速排序算法是一种分区交换排序算法. 二.冒泡排序 1.冒泡排序的定义 冒泡排序(Bu ...
- C#关于private protected sealed Virtual/Override
Public:公开权限 Private:修饰类时类为程序集或者包含此类的类内部权限:修饰变量时只能类内部使用: Protected:修饰变量,只能继承类可以使用,对外(包括继承类的实例)无权限: Ab ...
- SpringBoot整合MybatisPlus3.X之逻辑删除(三)
pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId& ...