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
#pragma GCC optimize("O3,inline,unroll-loops")
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;

mt19937 rng(679);

// const __int128_t mod = (__int128_t) 44326214376618ll * 1'000'000'000'000ll + 333352652423ll; // 367
// const __int128_t mod = (__int128_t) 97253461433805ll * 1'000'000'000'000ll + 715000527043ll; // 397
// const __int128_t mod = (__int128_t) 83515593923598ll * 1'000'000'000'000ll + 484276028347ll; // 391
const __int128_t mod = (__int128_t) 57998064692731ll * 1'000'000'000'000ll + 100928561521ll; // 377



const __int128_t hash_a = (__int128_t) 7.2345343e21 + 495834299828341ll;
__int128_t hash_b = (__int128_t) 2.3423e20 + 62342342372352453ll;

__int128_t mul(__int128_t a, __int128_t b) {
    unsigned int * barr = (unsigned int*) (&b);
    __int128_t ans = a*barr[2] % mod;
    ans = ((ans<<32) + a * barr[1]) % mod;
    ans = ((ans<<32) + a * barr[0]) % mod;
    return ans;
}

__int128_t modpow(__int128_t a, __int128_t e) {
    __int128_t ans = 1;
    while(e){
        if (e&1) ans = mul(ans, a);
        a = mul(a,a);
        e/=2;
    }return ans;
}

const int B = 377;
const __int128_t B5 = (__int128_t) B * B * B * B * B;

int masks[B], toid[1<<10];
bool ismask[1<<10];

vector<__int128_t> precalc(array<int,5> a, bool islarge) {
    vector<__int128_t> ans;
    sort(all(a));
    do{
        __int128_t cur = 0;
        rep(i,0,5) cur = cur * B + a[i];
        if (islarge){
            cur = (mul(cur, B5) + hash_a) % mod;
        }
        cur = mul(cur, hash_b);
        ans.push_back(cur);
    }while(next_permutation(all(a)));
    sort(all(ans));
    ans.erase(unique(all(ans)),end(ans));
    return ans;
}

int main(){
    // cin.tie(NULL),ios::sync_with_stdio(false);
    
    ll BOUND = 3e16 + 67;
    int t;

    {
        map<int,int> cntpops;
        vi a(1<<10);
        iota(all(a),0);
        shuffle(all(a),rng);
        rep(i,0,B){
            masks[i] = a[i];
            toid[a[i]] = i;
            ismask[a[i]] = 1;
        }
    }

    string player; cin >> player;
    if (player == "Algosia"){
        const __int128_t hash_b_inv = modpow(hash_b, mod-2);
        // assert(mul(hash_b,hash_b_inv) == 1);
        cin >> BOUND >> t;
        BOUND += 67;

        while(t--){

            ll n; cin >> n;

            auto myhash = (mul(n, hash_b_inv) - hash_a + mod) % mod;

            vi baseB(10);
            rep(i,0,10){
                baseB[i] = masks[myhash%B];
                myhash /= B;
            }

            for(auto c : baseB){
                rep(i,0,10){
                    if (c&(1<<i)) cout << '1';
                    else cout << '0';
                }cout << endl;
            }
        }
    }else{
        /*
            To make faster, precompute all possible 
        */
        cin >> BOUND >> t;
        BOUND += 67;

        while(t--){
            bool finished = 0;
            array<string,10> inp;
            rep(i,0,10) cin >> inp[i];
            {
                vi secperm(10);
                iota(all(secperm),0);
                shuffle(all(secperm),rng);
                rep(i,0,10) {
                    rep(j,0,10){
                        swap(inp[i][j], inp[i][secperm[j]]);
                    }
                }
            }

            array<int,10> perm;
            rep(i,0,10) perm[i] = 1<<i;

            int AAAA = 0;

            do{
                bool good = 1;
                rep(i,0,10){
                    int cur = 0;
                    rep(j,0,10) if (inp[i][j]=='1') {
                        cur |= perm[j];
                    }
                    if (!ismask[cur]) {
                        good = 0;
                        break;
                    }
                }

                if (good) {
                    array<int,10> a;
                    rep(i,0,10){
                        int cur = 0;
                        rep(j,0,10) if (inp[i][j]=='1') {
                            cur |= perm[j];
                        }
                        a[i] = toid[cur];
                    }

                    rep(msk,0,(1<<10)) if (__builtin_popcount(msk) == 5 and !finished) {
                        // high bits
                        array<int,5> hb, lb;
                        int ath = 0, atl = 0;
                        rep(i,0,10) {
                            if (msk&(1<<i)) hb[ath++] = a[i];
                            else lb[atl++] = a[i];
                        }

                        auto HBS = precalc(hb, true);
                        auto LBS = precalc(lb, false);

                        int at = 0;
                        for(auto lo : LBS) {
                            while((lo + HBS[at])%mod > (lo + HBS[(at + sz(HBS) - 1) % sz(HBS)])%mod) {
                                at = (at + sz(HBS) - 1) % sz(HBS);
                            }
                            __int128_t ans = (lo + HBS[at])% mod ;
                            if (ans < BOUND) {
                                cout << ll(ans) << endl;
                                finished = true;
                                break;
                            }
                        }
                    }
                }
            }while(next_permutation(all(perm)) and !finished);
        }
    }
}

/*

1001110100
1001110110
1011111000
1100001101
0110101010
0110011110
1000011000
0101010011
1111001110
1000110000

*/