forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path51.cpp
More file actions
134 lines (118 loc) · 2.51 KB
/
51.cpp
File metadata and controls
134 lines (118 loc) · 2.51 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <iostream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <cmath>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long LL;
vector<string> split( const string& s, const string& delim =" " ) {
vector<string> res;
string t;
for ( int i = 0 ; i != s.size() ; i++ ) {
if ( delim.find( s[i] ) != string::npos ) {
if ( !t.empty() ) {
res.push_back( t );
t = "";
}
} else {
t += s[i];
}
}
if (!t.empty() ) {
res.push_back(t);
}
return res;
}
vector<int> splitInt( const string& s, const string& delim =" " ) {
vector<string> tok = split( s, delim );
vector<int> res;
for ( int i = 0 ; i != tok.size(); i++ )
res.push_back( atoi( tok[i].c_str() ) );
return res;
}
LL gcd(LL a,LL b){
return b?gcd(b,a%b):a;
}
inline LL lcm(LL a,LL b){
return a/gcd(a,b)*b;
}
int s2i(string s) {
stringstream ss;
ss << s;
int res;
ss >> res;
return res;
}
string i2s(int n) {
stringstream ss;
ss << n;
string res;
ss >> res;
return res;
}
int plist[100000],pcount=0;
set<int> mm;
int prime(int n){
int i;
if ((n!=2&&!(n%2))||(n!=3&&!(n%3))||(n!=5&&!(n%5))||(n!=7&&!(n%7)))
return 0;
for (i=0;plist[i]*plist[i]<=n;i++)
if (!(n%plist[i]))
return 0;
return n>1;
}
string str = "000000";
void initprime(){
int i;
for (plist[pcount++]=2,i=3;i<1000000;i+=2)
if (prime(i)) {
plist[pcount++]=i;
if (i >= 100000) mm.insert(i);
}
}
void doit() {
if (str[0] == '0') return;
if (str.find('*') == string::npos) return;
if (str[5] == '0' || str[5] == '2' || str[5] == '4' || str[5] == '5' || str[5] == '6' || str[5] == '8') return;
vector<int> dd;
REP(i,10) {
if (i == 0 && str[0] == '*') continue;
string s = str;
REP(j,s.length()) {
if (s[j] == '*') s[j] = i + '0';
}
int val = s2i(s);
if (mm.find(val) != mm.end()) dd.push_back(val);
}
if (dd.size() == 8) {
REP(i,dd.size()) cout << dd[i] << " ";
cout << endl;
return;
}
}
void dfs(int idx) {
if (idx == 6) {
//doit();
return;
}
str[idx] = '*';
dfs(idx + 1);
REP(i,10) {
str[idx] = i + '0';
dfs(idx + 1);
}
}
void run() {
initprime();
dfs(0);
}
int main() {
run();
}