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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <cstdio>
#include <vector>
using namespace std;

/*
 *  BEFORE:      AFTER:
 *   a  b    ->   a  b
 *   0  0    ->   0  0
 *   0  1    ->   0  1
 *   1  0    ->   0  1
 *   1  1    ->   1  1
 */
struct command {
    int a;
    int b;
};

struct stp {
    char bits[36]; // 0 = 0, 1 = 1, 2 = unknown
    char ones;
    char zeros;
    bool skip;
};

vector<command> commands;
int numready = 0;
int ready_results[36];

bool add_1(bool *vals, int num) {
    bool added1 = false;
    for (int i = 0; i < num; ++i) {
        if (!vals[i]) {
            vals[i] = true;
            numready++;
            added1 = true;
            break;
        } else {
            vals[i] = false;
            numready--;
        }
    }
    return added1;
}

int can_fit_digits(int n, int k, int ones, int zeros, int digit_to_fit, int amount) {
    if (digit_to_fit == 0) {
        return zeros + amount <= n - k;
    } else {
        return ones + amount <= k;
    }
}

bool evaluate_setup(stp &setup, int n, int k) {
    // it can be either XX 0 XX 1 XX 0 XX
    // or               XX 0 XX 0 XX
    int maxones = 0;
    int result = 0;
    if (setup.ones > 0) {
        int potential_firstone = 0; // first non-zero in the valid part
        int firstone = 0; // first 1 in the valid part
        int lastone = 0; // last 1 in the valid part
        int potential_lastone = n - 1; // last non-zero in the valid part
        bool badsetup = false;
        int idx = 0;
        for (; idx < n; ++idx) {
            if (setup.bits[idx] == 0) {
                potential_firstone = idx + 1;
            }
            if (setup.bits[idx] == 1) {
                firstone = idx;
                lastone = idx;
                break;
            }
        }
        for (; idx < n; ++idx) {
            if (setup.bits[idx] == 1) {
                lastone = idx;
            }
            if (setup.bits[idx] == 0) {
                potential_lastone = idx - 1;
                break;
            }
        }
        for (; idx < n; ++idx) {
            if (setup.bits[idx] == 1) {
                badsetup = true;
            }
        }
        if (badsetup) {
            // printf("BAD SEQUENCE\n");
            return false;
        }

        int req_sequence = lastone - firstone + 1;
        int space_left = firstone - potential_firstone;
        int space_right = potential_lastone - lastone;

        if (req_sequence > k) {
             // printf("CAN'T FILL MAIN SEQUENCE\n");
            return false;
        }
        // printf("%d %d %d\n", space_left, req_sequence, space_right);
        if (space_left + req_sequence + space_right < k) {
            // printf("MAIN SEQUENCE TOO SHORT\n");
            return false;
        }
        int best_left = lastone - k + 1;
        int best_right = firstone + k - 1;

        int firststart = max(potential_firstone, best_left);
        int lastend = min(potential_lastone, best_right);
        int laststart = lastend - k + 1;

        result = laststart - firststart + 1;
        // printf("RESULT: %d\n", result);
        return result % 2 == 1;
    } else {
        // all intervals fit
        for (int i = 0; i < n; ++i) {
            if (setup.bits[i] == 2) {
                maxones++;
            } else {
                // == 0
                if (maxones - k + 1 > 0) {
                    result += maxones - k + 1;
                }
                maxones = 0;
            }
        }
        if (maxones - k + 1 > 0) {
            result += maxones - k + 1;
        }
        // printf("RESULT: %d\n", result);
        return (result % 2 == 1);
    }
}

int main() {

    /* SETUP EVALUATOR

    stp some_setup;

    some_setup.bits[0] = 0;
    some_setup.bits[1] = 2;
    some_setup.bits[2] = 0;
    some_setup.bits[3] = 1;
    some_setup.bits[4] = 2;
    some_setup.bits[5] = 0;
    some_setup.bits[6] = 2;
    some_setup.bits[7] = 1;
    some_setup.bits[8] = 2;
    some_setup.bits[9] = 0;
    some_setup.ones = 1;
    some_setup.zeros = 2;

    evaluate_setup(some_setup, 4, 1);

    return 0;

    /* END OF SETUP EVALUATOR */

    int n, m;
    scanf ("%d %d", &n, &m);
    for (int i = 0; i < m; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        commands.push_back(command{a-1, b-1});
    }


    for (int k = 2; k < n; ++k) {
        // exactly k need to be =1
        stp starting_setup;
        for (int i = 0; i < n; ++i) {
            starting_setup.bits[i] = 2;
            // zeros, ones, skip = 0/false
        }
        starting_setup.zeros = 0;
        starting_setup.ones = 0;
        starting_setup.skip = false;

        vector<stp> setups;
        setups.push_back(starting_setup);

        for(int i = 0; i < m; ++i) {
//            printf("------------------\n");
//            printf("  current setups:\n");
//            for (stp &setup: setups) {
//                for (int j = 0; j < n; ++j) {
//                    printf("%d", setup.bits[j]);
//                }
//                printf(" (0:%d, 1:%d)\n", setup.zeros, setup.ones);
//            }
//            printf("K: %d COMMAND %d\n", k, i);
            command cmd = commands[i];
            int setups_num = setups.size();
            for(int sidx = 0; sidx < setups_num; sidx++) {
                stp *setup = &setups[sidx];
                if (setup->skip) {
//                    printf("SETUP SKIP IS ON FOR SOME REASON\n");
                    continue;
                }
                char bit_a = setup->bits[cmd.a];
                char bit_b = setup->bits[cmd.b];
                switch (bit_a) {
                    case 2:
                        switch (bit_b) {
                            case 2: {
//                                printf("2-2 DUPLICATE\n");
                                // duplicate with 0, 0 or 1, 1
                                bool fitzeros = setup->zeros + 2 <= n - k;
                                bool fitones = setup->ones + 2 <= k;
                                if (fitones && fitzeros) {
                                    setup->bits[cmd.a] = 0;
                                    setup->bits[cmd.b] = 0;
                                    stp duplicated;
                                    copy(begin(setup->bits), end(setup->bits), begin(duplicated.bits));
                                    duplicated.bits[cmd.a] = 1;
                                    duplicated.bits[cmd.b] = 1;

                                    duplicated.zeros = setup->zeros;
                                    setup->zeros += 2;
                                    duplicated.ones = setup->ones + 2;
                                    duplicated.skip = false;
                                    setups.push_back(duplicated);
                                } else if (fitones) {
                                    setup->bits[cmd.a] = 1;
                                    setup->bits[cmd.b] = 1;
                                    setup->ones += 2;
                                } else if (fitzeros) {
                                    setup->bits[cmd.a] = 0;
                                    setup->bits[cmd.b] = 0;
                                    setup->zeros += 2;
                                } else {
//                                    printf("Setting skip!\n");
                                    // can't add 00 or 11 - so we add both 01 and 10 and they cancel each other
                                    setup->skip = true;
                                }
                                break;
                            }
                            case 1:
                                // no change - bits[a] can still be either 0 or 1
                                break;
                            case 0: {
                                // x 0 -> 1 0 or 0 0 -> 0 1 or 0 0
                                // duplicate with 0, 0 or 1, 1
                                bool fitzeros = setup->zeros + 1 <= n - k;
                                bool fitones = setup->ones + 1 <= k;
                                if (fitones && fitzeros) {
                                    setup->bits[cmd.a] = 0;
                                    stp duplicated;
                                    copy(begin(setup->bits), end(setup->bits), begin(duplicated.bits));
                                    duplicated.bits[cmd.a] = 0;
                                    duplicated.bits[cmd.b] = 1;

                                    duplicated.zeros = setup->zeros;
                                    setup->zeros += 1;
                                    duplicated.ones = setup->ones + 1;
                                    duplicated.skip = false;
                                    setups.push_back(duplicated);
                                } else if (fitones) { // no slots for another 0
                                    setup->bits[cmd.a] = 0;
                                    setup->bits[cmd.b] = 1;
                                    setup->ones += 1;
                                } else if (fitzeros) { // no slots for another 1
                                    setup->bits[cmd.a] = 0;
                                    setup->zeros += 1;
                                } else {
                                    // this will never happen
                                    setup->skip = true;
                                }
                                break;
                            }
                        }
                        break;
                    case 1:
                        switch (setup->bits[cmd.b]) {
                            case 2: {
                                // 1 x -> 1 0 or 1 1 -> 0 1 or 1 1
                                // duplicate with 1, 1 or 0, 1
                                bool fitzeros = setup->zeros + 1 <= n - k;
                                bool fitones = setup->ones + 1 <= k;
                                if (fitones && fitzeros) {
                                    setup->bits[cmd.a] = 0;
                                    setup->bits[cmd.b] = 1;
                                    stp duplicated;
                                    copy(begin(setup->bits), end(setup->bits), begin(duplicated.bits));
                                    duplicated.bits[cmd.a] = 1;
                                    duplicated.bits[cmd.b] = 1;

                                    duplicated.zeros = setup->zeros;
                                    setup->zeros += 1;
                                    duplicated.ones = setup->ones + 1;
                                    duplicated.skip = false;
                                    setups.push_back(duplicated);
                                } else if (fitones) { // no slots for another 0 so we make it 11
                                    setup->bits[cmd.a] = 1;
                                    setup->bits[cmd.b] = 1;
                                    setup->ones += 1;
                                } else if (fitzeros) { // no slots for another 1 se we make it 0 1
                                    setup->bits[cmd.a] = 0;
                                    setup->bits[cmd.b] = 1;
                                    setup->zeros += 1;
                                } else {
                                    // this will never happen
                                    setup->skip = true;
                                }
                                break;
                            }
                            case 1:
                                // no change
                                break;
                            case 0:
                                setup->bits[cmd.a] = 0;
                                setup->bits[cmd.b] = 1;
                                // swap 1, 0 into 0, 1
                                break;
                        }
                        break;
                    case 0:
                        // no change
                        break;
                }
            }
        }

//        printf("PROCESSED K %d:\n", k);
//        printf("  final setups:\n");
//        for (stp &setup: setups) {
//            for (int j = 0; j < n; ++j) {
//                printf("%d", setup.bits[j]);
//            }
//            printf(" (0:%d, 1:%d)\n", setup.zeros, setup.ones);
//        }
//        printf("-----------------\n");

        bool parity = false;
        for (stp setup: setups) {
            if (setup.skip) continue;
            bool x = evaluate_setup(setup, n, k);
            if (x) {
                parity = !parity;
            }
        }
        ready_results[k] = parity;
    }

    ready_results[1] = (n % 2 == 1);
    ready_results[n] = 1;

    for (int i = 1; i <= n; ++i) {
        printf("%d ", ready_results[i]);
    }
    printf("\n");
    return 0;
}