-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem_#41_Pandigital_prime.cpp
62 lines (58 loc) · 1.25 KB
/
Problem_#41_Pandigital_prime.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int prime(int x){
for(int i=2;i*i<=x;i++){
if(x%i == 0){
return false;
}
}
return true;
}
int main() {
vector<int> anss;
for(int k=9;k>=2;k--){
vector<int> ds(k);
for(int i=0;i<k;i++){
ds[i] = k-i;
}
ll num = 0;
for(int i=0;i<k;i++){
num = num*10+ds[i];
}
if(prime(num)){
anss.push_back(num);
}
//cout << num <<endl;
while(prev_permutation(ds.begin(), ds.end())){
num = 0;
for(int i=0;i<k;i++){
num = num*10+ds[i];
}
if(prime(num)){
anss.push_back(num);
}
}
}
// reverse(anss.begin(), anss.end());
int t; cin >> t;
while(t--){
ll n; cin >> n;
int found = false;
for(int i=0;i<anss.size();i++){
if(anss[i] <= n){
found = true;
cout << anss[i]<<endl;
break;
}
}
if(!found){
cout << -1 <<endl;
}
}
return 0;
}