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
#include <bits/stdc++.h>
using namespace std;

using grid_t = array<string, 10>;
const grid_t EMPTY_GRID{
    "0000000000",
    "0000000001",
    "0000000010",
    "0000000011",
    "0000000100",
    "0000000101",
    "0000000110",
    "0000111001",
    "0011001011",
    "0101010111",
};

const int64_t rnd = 17055074294104;

auto transposed(const grid_t& grid) {
    grid_t res = grid;
    for (int i = 0; i < 10; ++i) {
        for (int j = i + 1; j < 10; ++j) {
            swap(res[i][j], res[j][i]);
        }
    }
    return res;
}

auto get_perms(const grid_t& grid) {
    vector<tuple<int, int, int>> res;
    for (int c0 = 0; c0 < 10; ++c0) {
        for (int c1 = 0; c1 < 10; ++c1) {
            for (int c2 = 0; c2 < 10; ++c2) {
                if (c0 == c1 || c0 == c2 || c1 == c2) { continue; }
                vector<int> cnt(8);
                for (int r = 0; r < 10; ++r) {
                    int num = (grid[r][c0] == '1') + 2 * (grid[r][c1] == '1') + 4 * (grid[r][c2] == '1');
                    ++cnt[num];
                }
                if (cnt == vector{1, 2, 1, 2, 1, 1, 1, 1}) {
                    res.emplace_back(c0, c1, c2);   
                }
            }
        }
    }
    return res;
}

grid_t move_to_end(const grid_t& grid, tuple<int, int, int> perm) {
    grid_t res;
    auto [i0, i1, i2] = perm;
    res[9] = grid[i0];
    res[8] = grid[i1];
    res[7] = grid[i2];
    int j = 0;
    for (int i = 0; i < 10; ++i) {
        if (i != i0 && i != i1 && i != i2) {
            res[j++] = grid[i];
        }
    }
    return res;
}

vector<grid_t> permuted(const grid_t& grid) {
    grid_t res;
    vector<vector<string>> tmp(8);
    for (int r = 0; r < 10; ++r) {
        int num = (grid[r][9] == '1') + 2 * (grid[r][8] == '1') + 4 * (grid[r][7] == '1');
        tmp[num].push_back(grid[r]);
    }
    vector ss{1, 2, 1, 2, 1, 1, 1, 1};
    for (int i = 0; i < 8; ++i) {
        if (tmp[i].size() != ss[i]) {
            return {};
        }
    }
    return {
        {tmp[0][0], tmp[1][0], tmp[2][0], tmp[3][0], tmp[4][0], tmp[5][0], tmp[6][0], tmp[1][1], tmp[3][1], tmp[7][0]},
        {tmp[0][0], tmp[1][1], tmp[2][0], tmp[3][0], tmp[4][0], tmp[5][0], tmp[6][0], tmp[1][0], tmp[3][1], tmp[7][0]},
        {tmp[0][0], tmp[1][0], tmp[2][0], tmp[3][1], tmp[4][0], tmp[5][0], tmp[6][0], tmp[1][1], tmp[3][0], tmp[7][0]},
        {tmp[0][0], tmp[1][1], tmp[2][0], tmp[3][1], tmp[4][0], tmp[5][0], tmp[6][0], tmp[1][0], tmp[3][0], tmp[7][0]},
    };
}

bool check(const grid_t& grid) {
    for (int i = 0; i < 10; ++i) {
        for (int j = 0; j < 10; ++j) {
            if (i >= 7 || j >= 7) {
                if (grid[i][j] != EMPTY_GRID[i][j]) {
                    return false;
                }
            }
        }
    }
    return true;
}

vector<int64_t> decode_all(const grid_t& grid) {
    auto good_cols = get_perms(grid);
    auto good_rows = get_perms(transposed(grid));

    set<int64_t> res;
    for (auto c : good_cols) {
        for (auto r : good_rows) {
            auto grid0 = move_to_end(grid, r);
            grid0 = transposed(grid0);
            grid0 = move_to_end(grid0, c);
            for (auto grid1 : permuted(grid0)) {
                for (auto grid2 : permuted(transposed(grid1))) {
                    if (!check(grid2)) { continue; }
                    int64_t num = 0;
                    for (int i = 0; i < 49 - 5; ++i) {
                        num += (int64_t)(grid2[i / 7][i % 7] == '1') << i;
                    }
                    res.insert(num);
                }
            }
        }
    }
    vector<int64_t> ress;
    for (auto a : res) {
        ress.push_back(a);
    }
    return ress;
}

int64_t decode(const grid_t& grid) {
    vector<int64_t> res = decode_all(grid);
    auto i = bitset<5>(grid[6].substr(2, 5)).to_ulong();
    return res[i];
}

void encode(int64_t n) {
    grid_t grid = EMPTY_GRID;

    for (int i = 0; i < 49 - 5; ++i) {
        grid[i / 7][i % 7] = ((n >> i) & 1 ? '1' : '0');
    }
    auto decoded = decode_all(grid);
    for (int i = 0; i < (int)decoded.size(); ++i) {
        if (decoded[i] == n) {
            string xx = bitset<5>(i).to_string();
            for (int j = 0; j < 5; ++j) {
                grid[6][2 + j] = xx[j];
            }
            break;
        }
    }

    for (auto row : grid) {
        cout << row << endl;
    }
}


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

    string name;
    cin >> name;
    int64_t maxn;
    int t;
    cin >> maxn >> t;
    while (t--) {
        if (name == "Algosia") {
            int64_t n;
            cin >> n;
            n ^= rnd;
            encode(n);
        }
        else {
            grid_t grid;
            for (int r = 0; r < 10; ++r) {
                cin >> grid[r];
            }
            int64_t res = decode(grid);
            res ^= rnd;
            cout << res << endl;
        }
    }
    return 0;
}