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))的更多相关文章

  1. PAT 甲级 1015 Reversible Primes(20)

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

  2. pat 1015 Reversible Primes(20 分)

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

  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 分) 凌宸1642

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

  5. PAT 1015 Reversible Primes

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

  6. PAT 1015 Reversible Primes[求d进制下的逆][简单]

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

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

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

  9. PAT 1049 数列的片段和(20)(代码+思路分析)

    1049 数列的片段和(20)(20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2 ...

  10. PAT 1033 旧键盘打字(20)(20 分)

    1033 旧键盘打字(20)(20 分) 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在2 ...

随机推荐

  1. 20155336 2016-2017-2《JAVA程序设计》第七周学习总结

    20155336 2016-2017-2<JAVA程序设计>第七周学习总结 教材学习内容总结 第十三章 认识时间与日期 格林威治标准时间:简称GMT时间,参考格林威治皇家天文台的标准太阳时 ...

  2. fifo 上使用 select -- 转

    http://www.outflux.net/blog/archives/2008/03/09/using-select-on-a-fifo/ The right way to handle on-g ...

  3. weblogic 反序列化补丁绕过漏洞的一个批量检测shell脚本(CVE-2017-3248 )

    ~ 以下内容,仅供学习参考 ~ weblogic 反序列化补丁绕过漏洞已经出了两个月了,balabala ~~~ 废话不说,拿到该漏洞的利用工具weblogic.jar,但只能一个个检测ip和端口,效 ...

  4. npm_一个有意思的npm包

    $ npm install yosay const yosay = require('yosay'); console.log(yosay('Hello, and welcome to my fant ...

  5. 解决windows文件夹不能自动刷新的问题

    我用的是win7系统,最近忽然发现我的文档文件夹里的文件不能自动刷新了,就是当剪切或删除某个文件后,文件夹里的文件没有变化,看起来文件还在原文件夹中,只有通过手动刷新后才能看到效果,该如何解决? 网上 ...

  6. 【矩阵】RQ/QR 分解

    Multiple View Geometry in Computer Vision A.4.1.1 (page 579) 将一个 3x3 矩阵 $ A $ 进行 RQ 分解是将其分解成为一个上三角阵 ...

  7. linux中set、unset、export、env、declare,readonly的区别以及用法

    set命令显示当前shell的变量,包括当前用户的变量;   env命令显示当前用户的变量;   export命令显示当前导出成用户变量的shell变量.           每个shell有自己特有 ...

  8. 配置子目录Web.config使其消除继承,iis7.0设置路由

    iis7.0设置路由 ,url转向,伪静态 <system.webServer>      <modules runAllManagedModulesForAllRequests=& ...

  9. [转]在C#程序设计中使用Win32类库

    http://blog.163.com/j_yd168/blog/static/496797282008611326218/     C# 用户经常提出两个问题:“我为什么要另外编写代码来使用内置于 ...

  10. 三、springcloud之服务调用Feign

    一.背景 项目中接口调用: Httpclient Okhttp Httpurlconnection RestTemplate 微服务提供了更简单,方便的Feign 二.Feign简介 Feign是一个 ...