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
#include <cstdint>
#include <algorithm>
#include <string>
#include <vector>
#include <iostream>
using namespace std;

const int N = 10;
const int K = 6;

const int code[N] = {0, 1, 2, 3, 4, 5, 6, 7, 10, 11};

const int h1 = (1 << 8) | (1 << 9);
const int h2 = (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7);
const int h3 = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9);
const int h4 = (1 << 1) | (1 << 3) | (1 << 5) | (1 << 7) | (1 << 9);

const int p2x = 4, p2y = 0;
const int p3[4] = {2, 2, 2, 0};
const int p4[8] = {1, 1, 1, 1, 0, 1, 0, 0};

vector<int> good;
int posIn[1 << N];
int64_t c[900][7];

int bit(int mask, int pos) {
    return (mask >> pos) & 1;
}

bool same(const vector<int>& a, const int* b, int n) {
    for (int i = 0; i < n; i++) if (a[i] != b[i]) return false;
    return true;
}

pair<int,int> prof1(int row, int a) {
    int x = 0, y = 0;
    for (int i = 0; i < N; i++) if (bit(row, i)) {
        if (bit(a, i)) y++;
        else x++;
    }
    return {x, y};
}

vector<int> prof2(int row, int a, int b) {
    vector<int> res(4);
    for (int i = 0; i < N; i++) if (bit(row, i)) {
        int id = bit(a, i) * 2 + bit(b, i);
        res[id]++;
    }
    return res;
}

vector<int> prof3(int row, int a, int b, int c) {
    vector<int> res(8);
    for (int i = 0; i < N; i++) if (bit(row, i)) {
        int id = bit(a, i) * 4 + bit(b, i) * 2 + bit(c, i);
        res[id]++;
    }
    return res;
}

string maskToString(int mask) {
    string s = "";
    for (int i = 0; i < N; i++) s += char(bit(mask, i) + '0');
    return s;
}

int stringToMask(string s) {
    int mask = 0;
    for (int i = 0; i < N; i++) if (s[i] == '1') mask |= 1 << i;
    return mask;
}

void buildC() {
    for (int i = 0; i < 900; i++) {
        for (int j = 0; j <= 6; j++) c[i][j] = 0;
    }
    c[0][0] = 1;
    for (int i = 1; i < 900; i++) {
        c[i][0] = 1;
        for (int j = 1; j <= 6; j++) c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
    }
}

void buildGood() {
    fill(posIn, posIn + (1 << N), -1);
    for (int row = 0; row < (1 << N); row++) {
        if (__builtin_popcount(row) == 2) continue;
        if (prof1(row, h1) == make_pair(p2x, p2y)) continue;
        if (same(prof2(row, h1, h2), p3, 4)) continue;
        if (same(prof3(row, h1, h2, h3), p4, 8)) continue;

        posIn[row] = good.size();
        good.push_back(row);
    }
}

int64_t getCap() {
    int m = good.size();
    return c[m + K - 1][K];
}

vector<int> unrank(int64_t rank, int m) {
    int n = m + K - 1, lim = n;
    vector<int> b(K + 1), a(K);

    for (int i = K; i >= 1; i--) {
        int l = i - 1, r = lim;
        while (l < r) {
            int mid = (l + r + 1) / 2;
            if (c[mid][i] <= rank) l = mid;
            else r = mid - 1;
        }
        b[i] = l;
        rank -= c[l][i];
        lim = l;
    }

    for (int i = 1; i <= K; i++) a[i - 1] = b[i] - (i - 1);
    return a;
}

int64_t getRank(vector<int>& a) {
    int64_t res = 0;
    for (int i = 0; i < K; i++) res += c[a[i] + i][i + 1];
    return res;
}

int normalize(int row, vector<int>& pos) {
    int res = 0;
    for (int i = 0; i < N; i++) if (bit(row, pos[i])) res |= 1 << i;
    return res;
}

void encode(int64_t x) {
    int64_t cap = getCap();
    if (x < 1) x = 1;
    if (x > cap) x = (x - 1) % cap + 1;

    int m = good.size();
    vector<int> ids = unrank(x - 1, m), rows;
    rows.push_back(h1);
    rows.push_back(h2);
    rows.push_back(h3);
    rows.push_back(h4);
    for (int id : ids) rows.push_back(good[id]);

    for (int row : rows) cout << maskToString(row) << '\n';
    cout.flush();
}

int64_t decode(vector<int>& rows) {
    int i1 = -1, i2 = -1, i3 = -1, i4 = -1;
    int c1 = 0, c2 = 0, c3 = 0, c4 = 0;

    for (int i = 0; i < N; i++) {
        if (__builtin_popcount(rows[i]) == 2) {
            i1 = i;
            c1++;
        }
    }
    if (c1 != 1) return 1;

    for (int i = 0; i < N; i++) {
        if (i == i1) continue;
        if (prof1(rows[i], rows[i1]) == make_pair(p2x, p2y)) {
            i2 = i;
            c2++;
        }
    }
    if (c2 != 1) return 1;

    for (int i = 0; i < N; i++) {
        if (i == i1 || i == i2) continue;
        if (same(prof2(rows[i], rows[i1], rows[i2]), p3, 4)) {
            i3 = i;
            c3++;
        }
    }
    if (c3 != 1) return 1;

    for (int i = 0; i < N; i++) {
        if (i == i1 || i == i2 || i == i3) continue;
        if (same(prof3(rows[i], rows[i1], rows[i2], rows[i3]), p4, 8)) {
            i4 = i;
            c4++;
        }
    }
    if (c4 != 1) return 1;

    vector<int> col(N), got, want, pos(N, -1), other;
    for (int i = 0; i < N; i++) {
        col[i] = bit(rows[i1], i) * 8 + bit(rows[i2], i) * 4 + bit(rows[i3], i) * 2 + bit(rows[i4], i);
        got.push_back(col[i]);
        want.push_back(code[i]);
    }
    sort(got.begin(), got.end());
    sort(want.begin(), want.end());
    if (got != want) return 1;

    for (int i = 0; i < N; i++) {
        int x = -1;
        for (int j = 0; j < N; j++) if (code[j] == col[i]) {
            x = j;
            break;
        }
        if (x == -1 || pos[x] != -1) return 1;
        pos[x] = i;
    }

    for (int i = 0; i < N; i++) {
        if (i == i1 || i == i2 || i == i3 || i == i4) continue;
        int x = normalize(rows[i], pos);
        if (posIn[x] == -1) return 1;
        other.push_back(posIn[x]);
    }

    sort(other.begin(), other.end());
    return getRank(other) + 1;
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);

    buildC();
    buildGood();

    string who; cin >> who;
    int64_t n;
    int t;
    cin >> n >> t;

    if (who == "Algosia") {
        while (t--) {
            int64_t x; cin >> x;
            encode(x);
        }
    } else {
        while (t--) {
            vector<int> rows(N);
            for (int i = 0; i < N; i++) {
                string s; cin >> s;
                rows[i] = stringToMask(s);
            }
            cout << decode(rows) << '\n';
            cout.flush();
        }
    }
    return 0;
}