-
Notifications
You must be signed in to change notification settings - Fork 0
/
naivepavings.cc
305 lines (267 loc) · 7.74 KB
/
naivepavings.cc
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//------------------------------------------------------------------------------
//
// Naive pavings: programm to enumerate all tight pavings
// refer to: https://oeis.org/A285357
//
//------------------------------------------------------------------------------
//
// This programm uses very naive approach: it just lists all possible pavings
// of MxN square by M+N-1 blocks for all possible blocks and outputs those
// results, which are checked to be paving
//
//------------------------------------------------------------------------------
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "field.hpp"
#include "hind.hpp"
using std::cout;
using std::endl;
using std::fill;
using std::find_if;
using std::make_pair;
using std::map;
using std::max;
using std::minmax_element;
using std::next_permutation;
using std::pair;
using std::sort;
using std::stol;
using std::strlen;
struct genconfig {
bool only_stat = false;
bool no_stat = false;
bool only_vtype = false;
bool only_count = false;
};
size_t naive_gen(size_t N, size_t M, genconfig gcf) {
size_t count_ss = 0;
size_t count_np = 0;
size_t count_nt = 0;
size_t count_tp = 0;
size_t count_vt = 0;
// 1. enumerate possible block types
// (1 .. M-1) x (1 .. N-1) + 1 x N + 1 x M
// say 2:1 and 1:2 both have counter 2
//
// btypecnts[2] == 2
// btypes[<2, 0>] = <2, 1>
// btypes[<2, 1>] = <1, 2>
size_t nbtypes = max({N, M, (N - 1) * (M - 1)});
vector<size_t> btypecnts(nbtypes + 1);
map<pair<size_t, size_t>, pair<size_t, size_t>> btypes{};
map<vector<size_t>, size_t> genfunc;
btypes[make_pair(N, btypecnts[N])] = make_pair(N, 1);
btypecnts[N] += 1;
btypes[make_pair(M, btypecnts[M])] = make_pair(1, M);
btypecnts[M] += 1;
for (size_t horz = 1; horz < N; ++horz)
for (size_t vert = 1; vert < M; ++vert) {
btypes[make_pair(horz * vert, btypecnts[horz * vert])] =
make_pair(horz, vert);
btypecnts[horz * vert] += 1;
}
// 2. generate all type signatures of M+N-1 buckets
// with discrete allowed number of balls in each
//
// n is allowed <=> btypecnts[n] != 0
// 1 is always allowed (1x1 block)
//
// say for M*N = 9 and allowed numbers 1, 2, 3, 4
// possible signatures:
//
// 1, 1, 1, 2, 4
// 1, 1, 1, 3, 3
// 1, 1, 2, 2, 3
// 1, 2, 2, 2, 2
//
// and all permutations
size_t bsize = 1;
for (size_t cnt = 2; cnt < btypecnts.size(); ++cnt)
if (btypecnts[cnt] > 0)
bsize = cnt;
size_t mback = (M + N - 1);
size_t nballs = M * N;
vector<size_t> bcnt(mback, 1);
size_t excessballs = nballs - mback;
// form minimal signature 1, 1 ... 1, r, n ... n
// 2x2: 1, 1, 2
// 2x3: 1, 1, 1, 3
// 3x3: 1, 1, 1, 2, 4
// 4x3: 1, 1, 1, 1, 2, 6
// 4x4: 1, 1, 1, 1, 1, 2, 9
// suppose that r is always available number. As shown above it is always true
// for small numbers, but general case shall be proven separately, I think
size_t curback = mback - 1;
while (excessballs > 0) {
if (excessballs > bsize - 1) {
bcnt[curback] = bsize;
excessballs -= (bsize - 1);
assert(curback > 0);
curback -= 1;
} else {
assert(btypecnts[excessballs + 1] > 0);
assert(bcnt[curback] == 1);
bcnt[curback] += excessballs;
excessballs = 0;
}
}
do {
if (bcnt.end() != find_if(bcnt.begin(), bcnt.end(),
[&](size_t b) { return btypecnts[b] == 0; }))
continue;
do {
// 3. for given signature generate all mixed-radix tuples
// say for 2, 1, 1, 2, 3
// btypecnts[1] == 1, btypecnts[2] == 2, btypecnts[3] == 2
// solutions are:
//
// 0, 0, 0, 0, 0
// 0, 0, 0, 0, 1
// 0, 0, 0, 1, 0
// 0, 0, 0, 1, 1
// 1, 0, 0, 0, 0
// 1, 0, 0, 0, 1
// 1, 0, 0, 1, 0
// 1, 0, 0, 1, 1
vector<size_t> bmix(mback, 0);
for (;;) {
// 4. for mixed-radix tuple and signature fill field with btypes[<i, j>]
// blocks
// filter out non-pavings
// filter-out non-tight pavings
Field f(N, M);
bool succ = true;
for (size_t idx = 0; idx < mback; ++idx) {
auto elt = btypes[make_pair(bcnt[idx], bmix[idx])];
succ = f.put(elt.first, elt.second);
if (!succ)
break;
}
count_ss += 1;
if (!f.all())
count_np += 1;
if (f.all() && !f.tight()) {
count_nt += 1;
}
if (f.all() && f.tight()) {
count_tp += 1;
if (!gcf.only_stat && !gcf.only_vtype) {
f.dump(cout);
cout << endl;
}
if (f.vtype()) {
count_vt += 1;
auto v = f.vtype_signature();
if (!gcf.only_stat && gcf.only_vtype) {
f.dump(cout);
cout << '\t';
for (auto s : v)
cout << s << ' ';
cout << endl;
}
sort(v.begin(), v.end());
genfunc[v] += 1;
}
}
size_t digit = mback;
while (digit > 0) {
digit -= 1;
if (bmix[digit] + 1 < btypecnts[bcnt[digit]]) {
bmix[digit] += 1;
break;
}
bmix[digit] = 0;
}
size_t bsum = 0;
for (auto b : bmix)
bsum += b;
if (bsum == 0)
break;
}
} while (next_permutation(bcnt.begin(), bcnt.end()));
} while (next_break_of(M * N, M + N - 1, bcnt.begin(), bcnt.end()));
if (!gcf.no_stat) {
if (!gcf.only_count) {
cout << "Statistics: " << endl;
cout << "Search space size: " << count_ss << endl;
cout << "Not a pavings: " << count_np << endl;
cout << "Not tight pavings: " << count_nt << endl;
}
cout << "Tight pavings: " << count_tp << endl;
if (!gcf.only_count) {
cout << "Vertical types: " << count_vt << endl;
cout << "Generation function: ";
for (auto g : genfunc) {
if (g != *genfunc.begin())
cout << "+";
if (g.second > 1)
cout << g.second;
for (auto f: g.first)
cout << "f" << f;
}
cout << endl;
}
}
// 5. return result
return count_tp;
}
void printusage(char *argv0) {
cout << "Usage: " << argv0 << " n m [options]" << endl;
cout << "\tWhere n is horizontal size" << endl;
cout << "\t m is vertical size" << endl;
cout << "Note: m and n shall be >= 2" << endl;
cout << "Options supported are:" << endl;
cout << "\t-s -- show statistics only" << endl;
cout << "\t-n -- show no statistics" << endl;
cout << "\t-c -- only count pavings" << endl;
cout << "\t-v -- show vtype pavings" << endl;
}
int main(int argc, char **argv) {
if (argc < 3) {
printusage(argv[0]);
return -1;
}
auto n = stol(argv[1]);
auto m = stol(argv[2]);
if (n < 2 || m < 2) {
printusage(argv[0]);
return -1;
}
genconfig gcf;
for (size_t nopt = 3; nopt < argc; ++nopt) {
if (argv[nopt][0] != '-') {
printusage(argv[0]);
cout << "Please prepend options with - and pass separately" << endl;
return -1;
}
if (strlen(argv[nopt]) != 2) {
printusage(argv[0]);
cout << "Note: any option is one char after - sign" << endl;
return -1;
}
switch (argv[nopt][1]) {
case 's':
gcf.only_stat = true;
break;
case 'n':
gcf.no_stat = true;
break;
case 'c':
gcf.only_count = true;
break;
case 'v':
gcf.only_vtype = true;
break;
default:
printusage(argv[0]);
cout << "Note: only available options are listed above" << endl;
return -1;
}
}
naive_gen(n, m, gcf);
}