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 ...
随机推荐
- vue实现简单学生信息管理案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 玩转ArduinoJson库 V5版本
1.前言 一直以来,博主的事例代码中都一直使用到JSON数据格式.而很多初学者一直对JSON格式有很大疑惑,所以博主特意分出一篇博文来重点讲解Arduino平台下的JSON库--Arduino ...
- ESP8266开发之旅 网络篇⑤ Scan WiFi——ESP8266WiFiScan库的使用
1. 前言 现在,通常,为了让手机连上一个WiFi热点,基本上都是打开手机设置里面的WiFi设置功能,然后会看到里面有个WiFi热点列表,然后选择你要的连接上. 基本上你只要打开手机连接WiF ...
- ESP8266开发之旅 网络篇⑫ 域名服务——ESP8266mDNS库
1. 前言 前面的博文中,无论是作为client端还是server端,它们之间的通信都是通过具体的IP地址来寻址.通过IP地址来寻址,本身就是一个弊端,用户怎么会去记住这些魔法数字呢?那么有没 ...
- 百万年薪python之路 -- 小数据池和代码块练习
1.请用代码验证 "alex" 是否在字典的值中? info = {'name':'王刚蛋','hobby':'铁锤','age':'18',...100个键值对} info = ...
- Redis备忘(二)
内存回收: 有时候发现10g的Redis删掉1g的key,内存占用没啥变化,因为内存页分配,有的页面可能还存在key,整个页面不能回收. 主从同步: CAP原理:一致性 可用性 分区容忍性 redis ...
- anaconda重装jupyter notebook后启动jupyter报错的问题
问题描述: 由于jupyter出现难以解决的问题,采用重新安装来解决问题,但是重装之后启动jupyter报错ImportError: libsodium.so.23: cannot open shar ...
- Java基础系列1:Java面向对象
该系列博文会告诉你如何从入门到进阶,一步步地学习Java基础知识,并上手进行实战,接着了解每个Java知识点背后的实现原理,更完整地了解整个Java技术体系,形成自己的知识框架. 概述: Java是面 ...
- python中的列表list练习
列表: 1.增 1.1 append,在列表的末尾追加元素,使用方法:list.append('元素') li = ['alex', 'wusir', 'eric', 'rain', 'alex'] ...
- 转载:全面理解面向对象的 JavaScript
来源:DeveloperWorks – 曾滢著 简介: JavaScript 函数式脚本语言特性以及其看似随意的编写风格,导致长期以来人们对这一门语言的误解,即认为 JavaScript 不是一门面向 ...