-
Notifications
You must be signed in to change notification settings - Fork 0
/
1015.cpp
54 lines (50 loc) · 1.2 KB
/
1015.cpp
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
#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
string intToString(int n, int radix) {
string number = "";
while(n) {
number += (n % radix + '0');
n /= radix;
}
reverse(number.begin(), number.end());
// cout << number << endl;
return number;
}
int stringToInt(string number, int radix) {
int result = 0;
for(int i = 0; i < number.length(); i++) {
result *= radix;
result += number[i] - '0';
}
// cout << number << endl;
return result;
}
bool judgePrime(int n) {
if(n < 2) return 0;
for(int i = 2; i <= sqrt(n); i++) {
if(n % i == 0) return 0;
}
return 1;
}
int main() {
int number;
cin >> number;
while(number >= 0) {
int radix;
cin >> radix;
bool flag = true;
if(!judgePrime(number)) flag = false;
else {
string s = intToString(number, radix);
reverse(s.begin(), s.end());
if(!judgePrime(stringToInt(s, radix))) flag = false;
}
if(flag) cout << "Yes" << endl;
else cout << "No" << endl;
// cout << "--------------------" << endl;
cin >> number;
}
}