-
Notifications
You must be signed in to change notification settings - Fork 0
/
PE051.hpp
43 lines (42 loc) · 815 Bytes
/
PE051.hpp
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
#include <bits/stdc++.h>
using namespace std;
int PE051(int num) {
function<bool(int)> isPrime = [] (int val) {
for (int i = 2; i * i <= val; i += 1 + (i != 2)) {
if (!(val % i)) {
return false;
}
}
return val > 1;
};
for (int i = 2; ; i += 1 + (i != 2)) {
if (isPrime(i)) {
vector<int> pos[10];
string s = to_string(i);
int len = s.length();
for (int i = 0; i < len; i ++) {
pos[s[i] - '0'].push_back(i);
}
for (int j = 0; j < 10; j ++) {
if (pos[j].size()) {
int cnt = 0;
for (int k = 0; k < 10; k ++) {
if (j != k) {
string t = s;
for (int x: pos[j]) {
t[x] = k + '0';
}
if (isPrime(stoi(t))) {
cnt ++;
}
}
}
if (cnt == num) {
return i;
}
}
}
}
}
return -1;
}