1015 Reversible Primes (20)(20 point(s))
problem
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
answer
#include<bits/stdc++.h>
using namespace std;
int prime[100000] = {2, 3};
set<int> s;
void Prime(){
s.insert(2);
s.insert(3);
int i, j, flag, ta = 2;
for(i = 5; i < 100010; i+=2){
for(j = 0, flag = 1; prime[j]*prime[j] <= i; j ++){
if(i % prime[j] == 0) {
flag = 0; break;
}
}
if(flag){
prime[ta++] = i;
s.insert(i);
}
}
}
bool isPrime(int num)
{
set<int>::iterator it;
it = s.find(num);
if(it != s.end()) return true;
else return false;
}
int Reverse(int a, int d){
vector<int > s;
while(a > 0){
s.push_back(a%d);
a/=d;
}
int num = 0;
reverse(s.begin(), s.end());
for(int i =0; i< s.size(); i++){
num += s[i]*pow((float)d, i);
// cout<<s[i];
}
return num;
}
int main(){
ios::sync_with_stdio(false);
// freopen("test.txt", "r", stdin);
Prime();
int N, M;
while (cin>>N){
if(N < 0) break;
cin>>M;
if(!isPrime(N) || !isPrime(Reverse(N,M))) {
cout<<"No"<<endl;
}else{
cout<<"Yes"<<endl;
}
}
return 0;
}
experience
- 素数晒法,背下模板。
1015 Reversible Primes (20)(20 point(s))的更多相关文章
- PAT 甲级 1015 Reversible Primes(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- pat 1015 Reversible Primes(20 分)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- 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 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT 1015 Reversible Primes
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PAT 甲级 1011 World Cup Betting (20)(20 分)
1011 World Cup Betting (20)(20 分)提问 With the 2010 FIFA World Cup running, football fans the world ov ...
- PAT 甲级 1001 A+B Format (20)(20 分)
1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...
- PAT 1049 数列的片段和(20)(代码+思路分析)
1049 数列的片段和(20)(20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2 ...
- PAT 1033 旧键盘打字(20)(20 分)
1033 旧键盘打字(20)(20 分) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在2 ...
随机推荐
- .NET 下第一次接触Redis数据库
关于Redis 1.简介 Redis是著名的NOSQL(Not Only SQL)数据库,是键值对结构.(我只用过键值对结构的) 他为存储键值对做了优化,在大型网站中应用广泛.Redis提供了数据的自 ...
- Css3帧动画深入探寻,讲点项目中实际会碰到的问题
先加个副标题XD --如何解决background-size为100%下处理@keyframes 正是在项目中遇到副标题,才引起我更深入的探寻 先略带一下基本的css3动画 css3的动画实现是通过属 ...
- c++ ACM常用函数
1 保留小数点后两位 #include <iomanip> cout << setiosflags(ios::fixed) << setprecision(2)&l ...
- 翻译:CommonJS的wiki
CommonJS的wiki资料原文 Modules/AsynchronousDefinition 异步定义的模块(国内一般叫AMD模块定义) STATUS: PROPOSAL 现状:提案 Implem ...
- 采用dlopen、dlsym、dlclose加载动态链接库【总结】【转】
转自:https://www.cnblogs.com/Anker/p/3746802.html 1.前言 为了使程序方便扩展,具备通用性,可以采用插件形式.采用异步事件驱动模型,保证主程序逻辑不变,将 ...
- django Rest Framework----APIView 执行流程 APIView 源码分析
在django—CBV源码分析中,我们是分析的from django.views import View下的执行流程,这篇博客我们介绍django Rest Framework下的APIView的源码 ...
- 【前端vue开发架构】vue开发单页项目架构总结
为营销活动设计的前端架构 主要的技术栈为 Vuejs,Webpack,请自行阅读如下技术或者框架的文档: 一.基础说明: node (https://nodejs.org/en/) npm (http ...
- html- 头部元素
一:HTML <head> 元素 <head> 元素是所有头部元素的容器.<head> 内的元素可包含脚本,指示浏览器在何处可以找到样式表,提供元信息,等等. 以下 ...
- jQuery中绑定事件的几种方法
以click事件为例,jQuery中绑定事件有三种方法: (1)target.click(function(){}); (2)target.bind("click",functi ...
- mac系统安装redis
1.下载 打开官网:https://redis.io/ Download---Stable---Download3.2.8,下载最新稳定版,这里是3.2.8 2.安装 下载完成后,打开命令行工具,执行 ...