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
#include <iostream>
#include <vector>
#include <set>
#include <cmath>

using namespace std;

// #define MODE_BOTH
// #define MODE_DEBUG

#ifdef MODE_DEBUG
    #define DEBUG(x) x
#else
    #define DEBUG(x)
#endif

#define REP(x,n) for(int x=0;x<(n);++x)
#define FOREACH(x, c) for(auto x = (c).begin(); x != (c).end(); ++x)

string encoded[10];

string encode(long long number) {
    int trimmed = number & 0x3F;
    string result;
    result.reserve(6);

    REP(x,6) {
        result += ('0' + (trimmed & 1));
        trimmed >>= 1;
    }
    return result;
}

bool inline allEndWithOne() {
    for(int x=4;x<10;++x) {
        if (encoded[x][9] == '0') {
            return false;
        }
    }
    return true;
}

void algosia() {
    long long number;
    cin >> number;
    if (number >> 37) {
        cerr << "overflow!" << endl;
    }

    if (number >> 36) {
        // exclude 010 & 011, add 3x 011 (result 4/5/6)
        encoded[0] = "0111000000";
        encoded[1] = "1011000011";
        encoded[2] = "1011001101";
        encoded[3] = "1011010111";
        encoded[4] = "1000" + encode(number >> 30);
        encoded[5] = "1001" + encode(number >> 24);
        encoded[6] = "1100" + encode(number >> 18);
        encoded[7] = "1101" + encode(number >> 12);
        encoded[8] = "1110" + encode(number >> 6);
        encoded[9] = "1111" + encode(number);
    } else {
        // exclude 001 & 101, add 3x 001 (result 3/4/5)
        encoded[0] = "0111000000";
        encoded[1] = "1001000011";
        encoded[2] = "1001001101";
        encoded[3] = "1001010111";
        encoded[4] = "1000" + encode(number >> 30);
        encoded[5] = "1010" + encode(number >> 24);
        encoded[6] = "1011" + encode(number >> 18);
        encoded[7] = "1100" + encode(number >> 12);
        encoded[8] = "1110" + encode(number >> 6);
        encoded[9] = "1111" + encode(number);
    }
    
    if (allEndWithOne()) {
        encoded[0][9] = '1';
    }

    REP(x,10) {
        cout << encoded[x] << endl;
    }
    cout.flush();
}

int sumsPerRow[10];
int sumsPerCol[10];

int inline findIndex(int* arr, int value) {
    REP(x,10) {
        if (arr[x] == value)
            return x;
    }
    cerr << "### failed to find " << value << " in array";
    REP(x,10) { cerr << " " << arr[x];}
    cerr << endl;
    return -1;
}
int inline findColValueIndex(int columnIndex, char bit) {
    REP(x,10) {
        if (encoded[x][columnIndex] == bit) {
            return x;
        }
    }
    cerr << "### failed to find " << bit << " in encoded[" << columnIndex << "]";
    REP(x,10) { cerr << " " << encoded[x][columnIndex];}
    cerr << endl;
    return -1;
}
void extractRowIndices(int indicesToUse[3], int* targetNumbers) {
    REP(x,10) {
        targetNumbers[x] = 4*(encoded[x][indicesToUse[0]]-'0') + 2*(encoded[x][indicesToUse[1]]-'0') + (encoded[x][indicesToUse[2]]-'0');
    }
}
void extractColumnIndices(int indicesToUse[3], int* targetNumbers) {
    REP(x,10) {
        targetNumbers[x] = 4*(encoded[indicesToUse[0]][x]-'0') + 2*(encoded[indicesToUse[1]][x]-'0') + (encoded[indicesToUse[2]][x]-'0');
    }
}

void extractOrderedRows(int* allIndices, int* target, int pointerToExclude, int* addressIndicesToExclude, int encodingMode) {
    if (encodingMode == 1) {
        REP(x,10) {
            if (x == pointerToExclude || x == addressIndicesToExclude[0] || x == addressIndicesToExclude[1] || x == addressIndicesToExclude[2]) {}
            else switch(allIndices[x]) {
                case 0: target[0] = x; break;
                case 1: target[1] = x; break;
                case 4: target[2] = x; break;
                case 5: target[3] = x; break;
                case 6: target[4] = x; break;
                case 7: target[5] = x; break;
            }
        }
    } else {
        REP(x,10) {
            if (x == pointerToExclude || x == addressIndicesToExclude[0] || x == addressIndicesToExclude[1] || x == addressIndicesToExclude[2]) {}
            else switch(allIndices[x]) {
                case 0: target[0] = x; break;
                case 2: target[1] = x; break;
                case 3: target[2] = x; break;
                case 4: target[3] = x; break;
                case 6: target[4] = x; break;
                case 7: target[5] = x; break;
            }
        }
    }
}

void extractOrderedCols(int* allIndices, int* target, int pointerToExclude, int* addressIndicesToExclude) {
    REP(x,10) {
        if (x == pointerToExclude || x == addressIndicesToExclude[0] || x == addressIndicesToExclude[1] || x == addressIndicesToExclude[2]) {}
        else switch(allIndices[x]) {
            case 0: target[0] = x; break;
            case 1: target[1] = x; break;
            case 2: target[2] = x; break;
            case 3: target[3] = x; break;
            case 5: target[4] = x; break;
            case 7: target[5] = x; break;
        }
    }
}

long long buildNumber(int* rows, int* columns) {
    long long number = 0;
    REP(x, 6) {
        REP(y, 6) {
            number <<= 1;
            DEBUG(cerr << encoded[rows[x]][columns[5-y]];)
            if (encoded[rows[x]][columns[5-y]] == '1') {
                number |= 1;
            }
        }
    }
    DEBUG(cerr<<endl;)
    return number;
}

void bajtek() {
#ifndef MODE_BOTH
    REP(x,10) {
        cin >> encoded[x];
    }
#endif
    REP(x,10) {
        sumsPerRow[x] = sumsPerCol[x] = 0;
    }
    REP(x,10) {
        REP(y,10) {
            sumsPerRow[x] += encoded[x][y]-'0';
            sumsPerCol[y] += encoded[x][y]-'0';
        }
    }
    DEBUG(cerr << "sums per row:"; REP(x,10) { cerr << " " << sumsPerRow[x]; } cerr << endl;
          cerr << "sums per col:"; REP(x,10) { cerr << " " << sumsPerCol[x]; } cerr << endl;)

    int pointerColIndex = findIndex(sumsPerCol, 9);
    DEBUG(cerr << "found helper column at index " << pointerColIndex << endl;)
    int addressPointerRowIndex = findColValueIndex(pointerColIndex, '0');
    DEBUG(cerr << "found address indicator row at index " << addressPointerRowIndex << endl;)

    int encodingMode = 0;
    REP(x,10) {
        if (encoded[addressPointerRowIndex][x] == '1' && sumsPerCol[x] == 7) {
            encodingMode = 1;
            break;
        }
    }

    int addressColumnsIndices[3];
    REP(x,10) {
        if (encoded[addressPointerRowIndex][x] == '1' && sumsPerCol[x] != 10) {
            addressColumnsIndices[sumsPerCol[x]-4-encodingMode] = x;
        }
    }
    DEBUG(cerr << "found row addresses at columns " << addressColumnsIndices[0] << ", " << addressColumnsIndices[1] << ", " << addressColumnsIndices[2] << endl;)
    int rowIndices[10];
    extractRowIndices(addressColumnsIndices, rowIndices);
    DEBUG(cerr << "Row indices: "; REP(x,10) {cerr << " " << rowIndices[x];} cerr << endl;)

    int addressRowIndices[3];
    REP(x,10) {
        if (rowIndices[x] == (encodingMode == 1 ? 3 : 1)) {
            addressRowIndices[sumsPerRow[x]-4-encodingMode] = x;
        }
    }
    DEBUG(cerr << "found column addresses at rows " << addressRowIndices[0] << ", " << addressRowIndices[1] << ", " << addressRowIndices[2] << endl;)
    int columnIndices[10];
    extractColumnIndices(addressRowIndices, columnIndices);
    DEBUG(cerr << "Column indices: "; REP(x,10) {cerr << " " << columnIndices[x];} cerr << endl;)

    int orderedRows[6];
    int orderedColumns[6];
    
    extractOrderedRows(rowIndices, orderedRows, addressPointerRowIndex, addressRowIndices, encodingMode);
    extractOrderedCols(columnIndices, orderedColumns, pointerColIndex, addressColumnsIndices);
    DEBUG(cerr << "ordered rows "; REP(x,6) {cerr << " " << orderedRows[x];} cerr << endl;)
    DEBUG(cerr << "ordered cols "; REP(x,6) {cerr << " " << orderedColumns[x];} cerr << endl;)

    long long result = buildNumber(orderedRows, orderedColumns);
    if (encodingMode) {
        DEBUG(cerr<<" adding 1<<36 due to encoding mode" << endl;)
        result |= (1LL<<36);
    }
    cout << result << endl;
    cout.flush();
}

#ifdef MODE_BOTH
#include <random>
#include <ctime>

void swapRow(int a, int b) {
    swap(encoded[a], encoded[b]);
}
void swapColumn(int a, int b) {
    REP(x,10) {
        swap(encoded[x][a], encoded[x][b]);
    }
}

void random_perm() {
    srand(time(NULL));
    REP(x,10) {
        int a = rand()%10;
        int b = rand()%10;
        // swapRow(a, b);
    }
    REP(x,10) {
        int a = rand()%10;
        int b = rand()%10;
        swapColumn(a, b);
    }
    DEBUG(cerr<<"after permutations: "<<endl;
            REP(x,10) { cerr << encoded[x] << endl;})
}
#endif

int main() {
#ifndef MODE_BOTH
    string mode;
    cin >> mode;
#endif
    long long n;
    int t;
    cin>>n>>t;

    REP(x,10) {
        encoded[x].resize(10);
    }

#ifdef MODE_BOTH
    REP(x,t) {
        algosia();
        random_perm();
        bajtek();
    }
#else
    if ("Algosia" == mode) {
        REP(x,t) {
            algosia();
        }
    } else {
        REP(x,t) {
            bajtek();
        }
    }
#endif
    return 0;
}