-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49 - Prime permutations.cpp
69 lines (59 loc) · 1.5 KB
/
49 - Prime permutations.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define ii array<int,2>
bool is_prime(int n) {
if(n < 2)
return false;
if(n == 2)
return true;
for(int i = 2; i * i <= n; i++) {
if(n % i == 0)
return false;
}
return true;
}
vi digits(int n) {
vi ans;
while(n)
ans.push_back(n%10), n/= 10;
return ans;
}
bool is(array<int, 3> arr) {
vector<vector<int>> aux;
for(auto a:arr) {
aux.push_back(digits(a));
sort(aux.back().begin(), aux.back().end());
}
return aux[0] == aux[1] and aux[0] == aux[2];
}
void solve() {
vi primes;
for(int i = 1000; i < 10000; i++) {
if(is_prime(i) and i != 1487 and i != 4817 and i != 8147)
if(is_prime(i))
primes.push_back(i);
}
int len = primes.size();
for(int i = 0; i < len; i++) {
for(int j = i+1; j < len; j++) {
for(int k = j+1; k < len; k++) {
// prume
if(primes[k] - primes[j] > primes[j] - primes[i])
break;
if(primes[k] - primes[j] == primes[j] - primes[i]) {
array<int, 3> aux = { primes[i], primes[j], primes[k] };
if(is(aux))
cout << aux[0] << aux[1] << aux[2] << endl, exit(0);
}
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}