forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path93.cpp
More file actions
136 lines (116 loc) · 2.21 KB
/
93.cpp
File metadata and controls
136 lines (116 loc) · 2.21 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
135
136
#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;
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
string i2s(LL n) {
stringstream ss;
ss << n;
string res;
ss >> res;
return res;
}
int idx, mx;
double t;
int mm[4];
string res;
vector<int> tmp;
vector<int> vv;
void go(int now) {
if (now == 4) {
if (t >= 1) {
if (zero(t - (int)t)) vv.push_back((int)t);
}
return;
}
double bk = t;
t = bk * tmp[now];
go(now + 1);
t = bk + tmp[now];
go(now + 1);
t = bk - tmp[now];
go(now + 1);
t = tmp[now] - bk;
go(now + 1);
if (bk != 0) {
t = tmp[now] / bk;
go(now + 1);
}
if (tmp[now] != 0) {
t = bk / tmp[now];
go(now + 1);
}
}
vector<double> doit(double a, double b) {
vector<double> ret;
ret.push_back(a + b);
ret.push_back(a * b);
ret.push_back(a - b);
ret.push_back(b - a);
if (b != 0) ret.push_back(a / b);
if (a != 0) ret.push_back(b / a);
return ret;
}
void check() {
tmp.clear();
REP(i,4) tmp.push_back(mm[i]);
vv.clear();
do {
t = tmp[0];
go(1);
} while (next_permutation(tmp.begin(), tmp.end()));
vector<double> aa = doit(tmp[0], tmp[1]), bb = doit(tmp[2], tmp[3]);
REP(i,aa.size()) {
REP(j,bb.size()) {
vector<double> tt = doit(aa[i], bb[j]);
REP(k,tt.size()) {
if (tt[k] >= 1) {
if (zero(tt[k] - (int) tt[k])) vv.push_back((int)tt[k]);
}
}
}
}
sort(vv.begin(), vv.end());
vv.erase(unique(vv.begin(), vv.end()), vv.end());
int n = 0, last = 0;
REP(i,vv.size()) {
if (vv[i] != last + 1) break;
++n, ++last;
}
if (n > mx) {
mx = n;
res = "";
REP(j,4) res += i2s(tmp[j]);
}
}
void dfs(int now) {
if (now == 4) {
check();
return;
}
int s = (now == 0 ? -1 : mm[now - 1]);
FOR(i,s+1,9) {
mm[now] = i;
dfs(now + 1);
}
}
void run() {
mx = -1;
dfs(0);
cout << res << " " << mx << endl;
}
int main () {
run();
return 0;
}