-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpxs.cpp
135 lines (126 loc) · 3 KB
/
pxs.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
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
#include <bits/stdc++.h>
using namespace std;
using vs = vector<string>;
using vi = vector<int>;
int N, STATES;
string mark(string &S, int s) {
if (!s) return "0";
string res;
for (int i = 0; i < N; ++i) {
if (s & 1) res += S[i];
s >>= 1;
}
return res;
}
int restore(int a, int b, int j, string &line) {
int i, res = 0;
j >>= 1;
for (i = 1 << (N - 1); i; i >>= 1) {
if (a & i) {
if (!(b & j)) res |= i;
j >>= 1;
}
}
res = (STATES - 1) & res;
i = 1;
for (j = 0; j < N; ++j, i <<= 1) {
if (!(res & i)) continue;
if (line[j] == '0') res ^= i;
else break;
}
return res;
}
void debug(int k) {
for (int i = 1 << (N - 1); i; i >>= 1) {
cout << bool(k & i);
}
cout << endl;
}
vi next_states(string &line, int cur) {
vi res;
string sub = mark(line, cur);
int t = stoi(sub), k;
if (!t) return res;
int states = 1 << to_string(t).size();
for (int i = 1; i < states - 1; ++i) {
string s = mark(sub, i);
if (s.empty()) continue;
k = stoi(s);
if (k <= 1) continue;
if (t % k == 0) {
printf("%d / %d = 0\n", t, k);
k = restore(cur, i, states, line);
cout << " " << mark(line, k) << " ";
debug(k);
res.push_back(k);
}
}
return res;
}
vi resolve(string &line, int s, vi &P) {
vi res;
while (s != -1) {
cout << mark(line, s) << ",";
res.push_back(stoi(mark(line, s)));
s = P[s];
}
cout << endl;
reverse(res.begin(), res.end());
return res;
}
vi solve(string &line) {
N = line.size();
STATES = 1 << line.size();
int cur = STATES - 1;
// Parents, States
vi P(STATES, -1), S(STATES, 0);
vi final_states = {cur};
queue<tuple<int, int>> Q;
Q.emplace(1, cur); S.back() = 1;
int step, max_step = 1;
while (!Q.empty()) {
tie(step, cur) = Q.front(); Q.pop();
auto new_states = next_states(line, cur);
for (auto s : new_states) {
printf("new state: %s, cur step: %d, %d \n", mark(line, s).c_str(), step, S[s]);
if (S[s] > step) continue;
S[s] = step + 1;
if (step + 1 == max_step) {
final_states.push_back(s);
} else if (step + 1 > max_step) {
final_states = {s};
max_step = S[s];
}
if (P[s] == -1) P[s] = cur;
else if (S[P[s]] + 1 < S[s]) P[s] = cur;
else if (
S[P[s]] + 1 == S[s] &&
stoi(mark(line, cur)) < stoi(mark(line, s))
) P[s] = cur;
Q.emplace(S[s], s);
cout << step << " " << mark(line, cur) << " ";
cout << S[s] << " " << mark(line, s) << endl;
}
}
vector<vi> G(final_states.size());
for (size_t i = 0; i < G.size(); ++i) {
G[i] = resolve(line, final_states[i], P);
}
sort(G.begin(), G.end(), [](vi &a, vi &b) {
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] != b[i]) return a[i] < b[i];
}
return false;
});
return G[0];
}
int main() {
string line;
while (getline(cin, line)) {
if (line == "0") break;
auto S = solve(line);
for (auto &s : S) cout << s << " ";
cout << endl;
}
return 0;
}