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
#include <bits/stdc++.h>
using namespace std;            //10^14 (0 ... limit - 1)
const long long limit = 299'771'058'255'060;
const int chars = 772;          //2^10 - (10 choose 5)
const int totalPos = 6;         //other 4 rows are control ones

long long binom(int top, int bot) {
    long long res = 1;
    long long div = 1;
    for(int i=1; i<=bot; i++) {
        res *= (top-i+1);
        div *= i;
    }
    return res/div;
}

//encoder
vector<int> encodeLookup() {            //mangled values in order
    vector<vector<int>> translate(11);
    for(int i=0; i<(1<<10); i++)
        translate[__popcount(i)].push_back(i);

    vector<int> result;
    for(int i=0; i<=10; i++) {
        if(i == 5)          //rowSum of 5 is used by control rows
            continue;
        for(auto a : translate[i])
            result.push_back(a);
    }

    return result;
}
vector<int> mangle(long long val) {
    vector<int> values(6);
    int iter = 0;
    for(int pos=0; pos<6; pos++) {
        while(val >= binom(chars + totalPos - 2 - iter - pos, totalPos - 1 - pos)) {
            val -= binom(chars + totalPos - 2 - iter - pos, totalPos - 1 - pos);
            iter++;
        }
        values[pos] = iter;
    }
    return values;
}
void writeMask(int mask) {
    for(int bit=9; bit>=0; bit--)
        cout << ((mask>>bit) & 1);
    cout << "\n";
}

void encoder() {
    vector<int> lookup = encodeLookup();

    long long n, t;
    cin >> n >> t;
    while(t--) {
        long long val;
        cin >> val;
        val %= limit;           //avoid issues on values to big

        //encode the matrix
        vector<int> values = mangle(val);

        //cout the matrix
        writeMask(0b0010101101);
        writeMask(0b0101100101);
        writeMask(0b0011100011);
        writeMask(0b0000011111);
        
        for(auto a : values)
            writeMask(lookup[a]);
        cout.flush();
    }
}


//decoder
unordered_map<int, int> decodeLookup() {            //{mangledValueMask, index}
    vector<int> reverse = encodeLookup();
    unordered_map<int, int> result;

    for(int i=0; i<reverse.size(); i++)
        result[reverse[i]] = i;

    return result;
}
void swapCols(vector<vector<int>> &matrix, int a, int b) {
    for(int i=0; i<10; i++)
        swap(matrix[i][a], matrix[i][b]);
}
void preSortRows(vector<vector<int>> &matrix) {
    vector<int> rowSums(10, 0);
    for(int row=0; row<10; row++)
        for(int col=0; col<10; col++)
            rowSums[row] += matrix[row][col];
    
    for(int cur=0; cur<4; cur++) {          //separate coding rows
        for(int next=cur; next<10; next++) {
            if(rowSums[next] == 5) {
                swap(rowSums[cur], rowSums[next]);
                swap(matrix[cur], matrix[next]);
            }
        }
    }
}
void sortCodingRows(vector<vector<int>> &matrix) {
    set<int> ones;
    set<int> threes;
    for(int j=0; j<10; j++) {
        int sum = matrix[0][j] + matrix[1][j] + matrix[2][j] + matrix[3][j];
        if(sum == 1) {
            for(int i=0; i<4; i++)
                if(matrix[i][j] == 1)
                    ones.insert(i);
        }
        else if(sum == 3) {
            for(int i=0; i<4; i++)
                if(matrix[i][j] == 0)
                    threes.insert(i);
        }
    }
    
    int none, one, three, both;
    for(auto a : threes) {
        if(ones.count(a) == 1)
            both = a;
        else
            three = a;
    }
    for(auto a : ones)
        if(both != a)
            one = a;
    none = 6 - one - three - both;

    vector<int> back(4);            //currentPosition -> targetPosition
    back[none] = 0;
    back[one] = 1;
    back[three] = 2;
    back[both] = 3;

    for(int cur=0; cur<3; cur++) {
        int pos = cur;
        int smallest = back[pos];
        for(int next=cur; next<4; next++) {
            if(back[next] < smallest) {
                smallest = back[next];
                pos = next;
            }
        }
        swap(matrix[cur], matrix[pos]);
        swap(back[cur], back[pos]);
    }
}    
void sortCols(vector<vector<int>> &matrix) {
    vector<int> codingValues(10, 0);
    for(int j=0; j<10; j++)
        for(int i=0; i<4; i++)
            codingValues[j] |= ((matrix[i][j] & 1) << i);

    for(int cur=0; cur<9; cur++) {
        int pos = cur;
        int smallest = codingValues[pos];
        for(int next=cur; next<10; next++) {
            if(codingValues[next] < smallest) {
                smallest = codingValues[next];
                pos = next;
            }
        }
        swap(codingValues[cur], codingValues[pos]);
        swapCols(matrix, cur, pos);
    }
}
void unscrambleMatrix(vector<vector<int>> &matrix) {
    preSortRows(matrix);
    sortCodingRows(matrix);
    sortCols(matrix);
}
int getMask(vector<vector<int>> &matrix, int row) {
    int mask = 0;
    for(auto a : matrix[row])
        mask = (mask<<1) | (a&1);
    return mask;
}
long long unmangle(vector<int> &values) {
    sort(values.begin(), values.end());
    long long result = 0;
    int iter = 0;
    for(int pos=0; pos<6; pos++) {
        while(iter < values[pos]) {
            result += binom(chars + totalPos - 2 - iter - pos, totalPos - 1 - pos);
            iter++;
        }
    }

    return result;
}

void decoder() {
    unordered_map<int, int> lookup = decodeLookup();

    long long n, t;
    cin >> n >> t;
    while(t--) {
        //input the matrix
        vector<vector<int>> matrix(10, vector<int>(10));
        for(int i=0; i<10; i++) {
            string row;
            cin >> row;
            for(int j=0; j<10; j++)
                matrix[i][j] = row[j]&1;
        }

        //decode the matrix
        unscrambleMatrix(matrix);

        vector<int> values(6);
        for(int i=0; i<6; i++)
            values[i] = lookup[getMask(matrix, 4+i)];

        cout << unmangle(values) << "\n";
        cout.flush();
    }
}


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

    string s;
    cin >> s;
    if(s[0] == 'A')         //Algosia - encoder
        encoder();
    else            //Bajtek - decoder
        decoder();

    return 0;
}