徐子烊

  • 主页
  • 语雀
所有文章 关于我

徐子烊

  • 主页
  • 语雀

1015 Reversible Primes (20 分)

2019-03-10

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:

1
2
3
4
73 10
23 2
23 10
-2

Sample Output:

1
2
3
Yes
Yes
No

作者: CHEN, Yue

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

题目大意

给出一个数n以及进制d,要求判断n以及n在d进制翻转后是否都是素数。

分析

读取n,判断是否为素数。如是则转换为相应进制再翻转,转换为10进制判断是否为素数。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
#include <map>
#include <cstring>
#include <cmath>

using namespace std;

string change(int n,int radix){
if(radix==1)
return NULL;
int m=0;
string ans="";
while(n>=pow(radix,m))m++;
for(int i=m-1;i>=0;i--){
int base=pow(radix,i);
int d=n/base;
n-=d*base;
ans+=to_string(d);
}
return ans;
}

int go_back(string s,int radix){
int n=0;
int l=s.length();
for(int i=0;i<l;i++){
int d=s[i]-'0';
n+=d*pow(radix,l-i-1);
}
return n;
}

bool isprime(int n){
if(n<=1)
return false;
int k=sqrt(n);
for(int i=2;i<=k;i++){
if(n%i==0)
return false;
}
return true;
}
int main() {
std::ios::sync_with_stdio(false);
int n,radix;
while(cin>>n&&n>0){
cin>>radix;
if(isprime(n)){
string s=change(n,radix);
reverse(s.begin(),s.end());
//cout<<s<<endl;
int ans=go_back(s,radix);
//cout<<ans<<endl;
if(isprime(ans))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
} else cout<<"No"<<endl;
}
}
  • PAT
  • 进制转换
  • PAT题解

扫一扫,分享到微信

微信分享二维码
1019 General Palindromic Number (20 分)
1014 Waiting in Line (30 分)
  1. 1. 1015 Reversible Primes (20 分)
    1. 1.1. Input Specification:
    2. 1.2. Output Specification:
    3. 1.3. Sample Input:
    4. 1.4. Sample Output:
  2. 2. 题目大意
  3. 3. 分析
  4. 4. 代码
© 2020 徐子烊
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • PAT
  • 多项式
  • dijkstra
  • 图
  • 树
  • 最大子序列
  • 进制转换
  • 二分
  • dfs
  • 连通分量
  • 模拟
  • STL
  • 大整数
  • 需复习
  • 链表
  • 贪心
  • 动态规划
  • 子序列
  • 二叉树
  • LCS
  • 栈
  • 排序
  • 质因数
  • 树状数组
  • 完全二叉树
  • 整数溢出
  • AVL树
  • 深搜
  • 哈希
  • bfs
  • 并查集
  • 红黑树
  • LCA
  • 拓扑排序
  • lca
  • 实习
  • bat
  • Leetcode
  • Java并发
  • 线程池
  • 6.824
  • 分布式
  • mapreduce
  • caffe
  • epoll
  • Raft
  • schnorr
  • 密码学
  • 设计模式
  • 单例模式
  • 原型模式
  • 工厂模式
  • 建造者模式
  • 抽象工厂
  • 桥接模式
  • 模版模式
  • 组合模式
  • 策略模式
  • 装饰器模式
  • 迭代器模式
  • 适配器模式

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Raft论文阅读笔记

    2020-04-27

    #分布式#Raft

  • MIT 6.824 分布式系统 Lab1 MapReduce实现 2020 Spring

    2020-03-30

    #6.824#分布式#mapreduce

  • MapReduce论文阅读笔记

    2020-03-28

    #分布式#mapreduce

  • Java并发之线程池使用及源码剖析

    2020-03-21

    #Java并发#线程池

  • epoll如此快的关键

    2020-03-19

    #epoll

  • go实现schnorr签名与验证

    2020-03-18

    #schnorr#密码学

  • Leetcode 68. Text Justification

    2019-08-22

    #Leetcode

  • 设计模式之装饰器模式

    2019-06-06

    #设计模式#装饰器模式

  • 设计模式之组合模式

    2019-06-04

    #设计模式#组合模式

  • 设计模式之策略模式

    2019-06-03

    #设计模式#策略模式

  • 设计模式之桥接模式

    2019-06-02

    #设计模式#桥接模式

  • 设计模式之抽象工厂模式

    2019-05-31

    #设计模式#抽象工厂

  • 设计模式之建造者模式

    2019-05-31

    #设计模式#建造者模式

  • 设计模式之原型模式

    2019-05-28

    #设计模式#原型模式

  • 设计模式之单例模式

    2019-05-28

    #设计模式#单例模式

  • 设计模式之工厂模式

    2019-05-26

    #设计模式#工厂模式

  • 设计模式之模版模式

    2019-05-23

    #设计模式#模版模式

  • 设计模式之适配器模式

    2019-05-23

    #设计模式#适配器模式

  • 设计模式之迭代器模式

    2019-05-23

    #设计模式#迭代器模式

  • PAT完结撒花&总结

    2019-05-21

    #PAT

  • 1151 LCA in a Binary Tree (30 分)

    2019-05-21

    #PAT#lca

  • 1148 Werewolf - Simple Version (20 分)

    2019-05-20

    #PAT

  • 1146 Topological Order (25 分)

    2019-05-19

    #PAT#拓扑排序

  • 1145 Hashing - Average Search Time (25 分)

    2019-05-19

    #PAT#哈希

  • 1143 Lowest Common Ancestor (30 分)

    2019-05-13

    #PAT#LCA

  • 1139 First Contact (30 分)

    2019-05-11

    #PAT

  • 1135 Is It A Red-Black Tree (30 分)

    2019-05-09

    #PAT#树#红黑树

  • 1134 Vertex Cover (25 分)

    2019-05-09

    #PAT

  • 1131 Subway Map (30 分)

    2019-05-08

    #PAT#图

  • 1129 Recommendation System (25 分)

    2019-05-07

    #PAT

  • 1127 ZigZagging on a Tree (30 分)

    2019-05-07

    #PAT#树

  • 1123 Is It a Complete AVL Tree (30 分)

    2019-05-03

    #PAT#AVL树

  • 1122 Hamiltonian Cycle (25 分)

    2019-05-03

    #PAT

  • 1121 Damn Single (25 分)

    2019-05-02

    #PAT

  • 1119 Pre- and Post-order Traversals (30 分)

    2019-05-02

    #PAT#树

  • 1118 Birds in Forest (25 分)

    2019-04-27

    #PAT#并查集

  • 1114 Family Property (25 分)

    2019-04-26

    #PAT#并查集

  • 1112 Stucked Keyboard (20 分)

    2019-04-26

    #PAT

  • 1111 Online Map (30 分)

    2019-04-25

    #PAT#dijkstra#图

  • 1110 Complete Binary Tree (25 分)

    2019-04-24

    #PAT#树

  • 1109 Group Photo (25 分)

    2019-04-24

    #PAT

  • 1107 Social Clusters (30 分)

    2019-04-22

    #PAT#并查集

  • 1105 Spiral Matrix (25 分)

    2019-04-21

    #PAT

  • 1103 Integer Factorization (30 分)

    2019-04-20

    #PAT#dfs

  • 1101 Quick Sort (25 分)

    2019-04-18

    #PAT

  • 1099 Build A Binary Search Tree (30 分)

    2019-04-17

    #PAT#树

  • 1097 Deduplication on a Linked List (25 分)

    2019-04-16

    #PAT#链表

  • 1096 Consecutive Factors (20 分)

    2019-04-16

    #PAT

  • 1095 Cars on Campus (30 分)

    2019-04-15

    #PAT

  • 1093 Count PAT's (25 分)

    2019-04-14

    #PAT

  • 1091 Acute Stroke (30 分)

    2019-04-13

    #PAT#bfs

  • 1087 All Roads Lead to Rome (30 分)

    2019-04-11

    #PAT#dijkstra#图

  • 1086 Tree Traversals Again (25 分)

    2019-04-10

    #PAT#树

  • [坑][自闭]1082 Read Number in Chinese (25 分)

    2019-04-09

    #PAT

  • MacOS 10.14 Mojave 安装caffe记录

    2019-04-09

    #caffe

  • 1080 Graduate Admission (30 分)

    2019-04-08

    #PAT#模拟

  • 1078 Hashing (25 分)

    2019-04-07

    #PAT#哈希

  • 1077 Kuchiguse (20 分)

    2019-04-07

    #PAT

  • 1076 Forwards on Weibo (30 分)

    2019-04-07

    #PAT#bfs

  • [天坑]1075 PAT Judge (25 分)

    2019-04-04

    #PAT

  • 1074 Reversing Linked List (25 分)

    2019-04-04

    #PAT#链表

  • 1072 Gas Station (30 分)

    2019-04-03

    #PAT#dijkstra

  • 删了一波水题

    2019-04-02

  • 1068 Find More Coins (30 分)

    2019-04-02

    #PAT#需复习#动态规划#深搜

  • 1067 Sort with Swap(0, i) (25 分)

    2019-04-01

    #PAT#需复习#排序

  • 1066 Root of AVL Tree (25 分)

    2019-04-01

    #PAT#AVL树

  • 1065 A+B and C (64bit) (20 分)

    2019-03-31

    #PAT#整数溢出

  • 1064 Complete Binary Search Tree (30 分)

    2019-03-31

    #PAT#二叉树#完全二叉树

  • 1063 Set Similarity (25 分)

    2019-03-31

    #PAT#STL

  • [2019春]各大厂实习生招聘要求

    2019-03-31

    #实习#bat

  • [天坑]1060 Are They Equal (25 分)

    2019-03-30

    #PAT

  • 1059 Prime Factors (25 分)

    2019-03-29

    #PAT#质因数

  • [未解决]1057 Stack (30 分)

    2019-03-29

    #PAT#需复习#树状数组

  • 1056 Mice and Rice (25 分)

    2019-03-29

    #PAT#STL

  • 1055 The World's Richest (25 分)

    2019-03-28

    #PAT#排序

  • 1053 Path of Equal Weight (30 分)

    2019-03-28

    #PAT#树#STL

  • 1052 Linked List Sorting (25 分)

    2019-03-28

    #PAT#链表

  • 1051 Pop Sequence (25 分)

    2019-03-27

    #PAT#栈

  • 1049 Counting Ones (30 分)

    2019-03-27

    #PAT

  • 恭喜我喜提华中师范大学

    2019-03-27

  • 1045 Favorite Color Stripe (30 分)

    2019-03-20

    #PAT#需复习#LCS

  • 1044 Shopping in Mars (25 分)

    2019-03-19

    #PAT#需复习#子序列

  • 1043 Is It a Binary Search Tree (25 分)

    2019-03-18

    #PAT#二叉树

  • 1042 Shuffling Machine (20 分)

    2019-03-18

    #PAT

  • 1040 Longest Symmetric String (25 分)

    2019-03-17

    #PAT#需复习#动态规划

  • 1038 Recover the Smallest Number (30 分)

    2019-03-17

    #PAT

  • 1037 Magic Coupon (25 分)

    2019-03-17

    #PAT#贪心

  • 1033 To Fill or Not to Fill (25 分)

    2019-03-16

    #PAT#需复习

  • 1032 Sharing (25 分)

    2019-03-15

    #PAT#链表

  • 1031 Hello World for U (20 分)

    2019-03-15

    #PAT

  • 1030 Travel Plan (30 分)

    2019-03-15

    #PAT#dijkstra#图

  • 1029 Median (25 分)

    2019-03-14

    #PAT#需复习

  • 1026 Table Tennis (30 分)

    2019-03-13

    #PAT#模拟

  • 1024 Palindromic Number (25 分)

    2019-03-13

    #PAT#大整数

  • 1018 Public Bike Management (30 分)

    2019-03-11

    #PAT#dijkstra#图#dfs

  • 1021 Deepest Root (25 分)

    2019-03-11

    #PAT#图#树

  • 1017 Queueing at Bank (25 分)

    2019-03-11

    #PAT#模拟

  • 1016 Phone Bills (25 分)

    2019-03-10

    #PAT#STL

  • 1019 General Palindromic Number (20 分)

    2019-03-10

    #PAT#进制转换

  • 1015 Reversible Primes (20 分)

    2019-03-10

    #PAT#进制转换

  • 1014 Waiting in Line (30 分)

    2019-03-09

    #PAT#模拟

  • 1013 Battle Over Cities (25 分)

    2019-03-09

    #PAT#图#dfs#连通分量

  • 1010 Radix (25 分)

    2019-03-08

    #PAT#进制转换#二分

  • 1009 Product of Polynomials (25 分)

    2019-03-08

    #PAT#多项式

  • 1007 Maximum Subsequence Sum (25 分)

    2019-03-06

    #PAT#最大子序列

  • 1004 Counting Leaves (30 分)

    2019-03-05

    #PAT#树

  • 1003 Emergency (25 分)

    2019-03-05

    #PAT#dijkstra#图

  • 1002 A+B for Polynomials (25 分)

    2019-03-04

    #PAT#多项式

  • 1001 A+B Format (20 分)

    2019-03-04

    #PAT

很惭愧

只做了一点微小的工作