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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "dzilib.h"
using namespace std;

// #define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
#define ssize(x) int(x.size())

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define losuj(a, b) uniform_int_distribution<long long>(a, b)(rng)

using vi = vector<int>;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll, ll>;
using pull = pair<ull, ull>;
using vll = vector<ll>;
using ii = pair<int, int>;

namespace PollardRho
{
    mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
    const int P = 1e6 + 9;
    ll seq[P];
    int primes[P], spf[P];
    inline ll add_mod(ll x, ll y, ll m)
    {
        return (x += y) < m ? x : x - m;
    }
    inline ll mul_mod(ll x, ll y, ll m)
    {
        ll res = __int128(x) * y % m;
        return res;
        // ll res = x * y - (ll)((long double)x * y / m + 0.5) * m;
        // return res < 0 ? res + m : res;
    }
    inline ll pow_mod(ll x, ll n, ll m)
    {
        ll res = 1 % m;
        for (; n; n >>= 1)
        {
            if (n & 1)
                res = mul_mod(res, x, m);
            x = mul_mod(x, x, m);
        }
        return res;
    }
    // O(it * (logn)^3), it = number of rounds performed
    inline bool miller_rabin(ll n)
    {
        if (n <= 2 || (n & 1 ^ 1))
            return (n == 2);
        if (n < P)
            return spf[n] == n;
        ll c, d, s = 0, r = n - 1;
        for (; !(r & 1); r >>= 1, s++)
        {
        }
        // each iteration is a round
        for (int i = 0; primes[i] < n && primes[i] < 32; i++)
        {
            c = pow_mod(primes[i], r, n);
            for (int j = 0; j < s; j++)
            {
                d = mul_mod(c, c, n);
                if (d == 1 && c != 1 && c != (n - 1))
                    return false;
                c = d;
            }
            if (c != 1)
                return false;
        }
        return true;
    }
    void init()
    {
        int cnt = 0;
        for (int i = 2; i < P; i++)
        {
            if (!spf[i])
                primes[cnt++] = spf[i] = i;
            for (int j = 0, k; (k = i * primes[j]) < P; j++)
            {
                spf[k] = primes[j];
                if (spf[i] == spf[k])
                    break;
            }
        }
    }
    // returns O(n^(1/4))
    ll pollard_rho(ll n)
    {
        while (1)
        {
            ll x = rnd() % n, y = x, c = rnd() % n, u = 1, v, t = 0;
            ll *px = seq, *py = seq;
            while (1)
            {
                *py++ = y = add_mod(mul_mod(y, y, n), c, n);
                *py++ = y = add_mod(mul_mod(y, y, n), c, n);
                if ((x = *px++) == y)
                    break;
                v = u;
                u = mul_mod(u, abs(y - x), n);
                if (!u)
                    return __gcd(v, n);
                if (++t == 32)
                {
                    t = 0;
                    if ((u = __gcd(u, n)) > 1 && u < n)
                        return u;
                }
            }
            if (t && (u = __gcd(u, n)) > 1 && u < n)
                return u;
        }
    }
    vector<ll> factorize(ll n)
    {
        if (n == 1)
            return vector<ll>();
        if (miller_rabin(n))
            return vector<ll>{n};
        vector<ll> v, w;
        while (n > 1 && n < P)
        {
            v.push_back(spf[n]);
            n /= spf[n];
        }
        if (n >= P)
        {
            ll x = pollard_rho(n);
            v = factorize(x);
            w = factorize(n / x);
            v.insert(v.end(), w.begin(), w.end());
        }
        return v;
    }

    vector<pair<ll, int>> get_pairs(vector<ll> factors)
    {
        sort(factors.begin(), factors.end());
        vector<pair<ll, int>> ret;
        assert(sz(factors));
        ret.pb({factors[0], 0});
        for (auto &f : factors)
        {
            if (ret.back().f == f)
            {
                ret.back().s++;
            }
            else
            {
                ret.pb({f, 1});
            }
        }
        return ret;
    }
    
    int num_divisors(ll val)
    {
        auto factors = get_pairs(factorize(val));
        
        int ret = 1;
        for (auto &[p, cnt] : factors)
        {
            ret *= (cnt + 1);
        }
        return ret;
    }
}

const ll BIG_INF = 1e18;

ll pow(ll a, int b)
{
    ll ret = 1;
    while (b)
    {
        if (b & 1)
        {
            if (ret > BIG_INF / a)
                return BIG_INF;
            ret *= a;
        }
        if (b > 1)
        {
            if (a > BIG_INF / a)
                return BIG_INF;
            a *= a;
        }
        b >>= 1;
    }
    return ret;
}

const int N = 2e7 + 5;
int sito[N];
int divs[N];
const ll C = 1e17;
vi primes;

// ll min_val(int ile, set<int> used)
// {
//     auto factors = get_pairs(factorize(ile));
//     vi wykladniki;
//     for (auto & [p, cnt] : factors)
//     {
//         wykladniki.pb(cnt);
//     }
//     ll ret = 1;

//     sort(all(wykladniki));
//     reverse(all(wykladniki));

//     int last_prime = 0;
//     for (auto & wyk : wykladniki)
//     {
//         while (used.count(primes[last_prime]))
//             last_prime++;
//         ll more = pow(primes[last_prime], wyk);
//         last_prime++;
//         if (ret > BIG_INF / more)
//             return BIG_INF;
//         ret *= more;
//     }
//     return ret;
// }

void prep()
{
    for (int i = 2; i < N; i++)
    {
        if (sito[i] == 0)
        {
            primes.pb(i);
            for (ll j = 1ll * i * i; j < N; j += i)
            {
                if (sito[j] == 0)
                    sito[j] = i;
            }
        }
    }

    for (int i = 1; i < N; i++)
    {
        for (int j = i; j < N; j += i)
        {
            divs[j]++;
        }
    }
}

int main()
{
    PollardRho::init();
    prep();

    // int good = 0;
    // vector<ll> num_divs;
    // ll sum = 0;
    // for (int i = 0; i < 70; ++i)
    // {
    //     ll val = losuj(C - N / 2, C);
    //     // cout << "FACTORIZING " << val << endl;
    //     auto f = PollardRho::factorize(val);

    //     vector<pair<ll, int>> factors = get_pairs(f);
    //     num_divs.pb(num_divisors(factors));
    //     sum += num_divs.back();
    // }
    // sort(all(num_divs));
    // cout << C << ' ' << min_val({2, 2, 2, 2, 2, 2, 2, 2, 2, 2}, {}) << endl;
    // for (int i = 0; i < 30; i++)
    // {
    //     cout << num_divs[i] << ' ';
    // }
    // cout << endl;
    // for (int i = 0; i < 30; i++)
    // {
    //     cout << num_divs[sz(num_divs) - 1 - i] << ' ';
    // }
    // cout << "AVG " << sum / sz(num_divs) << endl;
    // exit(0);
    int t = GetT();
    int q = GetQ();
    long long c = GetC();
    long long n = GetN();

    int queries = q / t;

    // cout << t << ' '  << q << ' ' << c << ' ' << n << endl;

    while (t--)
    {
        if (n <= 1000000)
        {
            vector<int> candidates;
            for (int i = 1; i <= n; i++)
            {
                candidates.pb(i);
            }
            for (int i = 0; i < c; i++)
            {
                int x = Ask(i);
                vector<int> new_candidates;
                for (auto &u : candidates)
                {
                    if (divs[u + i] == x)
                    {
                        new_candidates.pb(u);
                    }
                }
                swap(candidates, new_candidates);
                // assert(sz(candidates));
                if (candidates.size() == 1)
                {
                    break;
                }
            }
            Answer(candidates[0]);
        }
        else
        {
            assert(false);
        }
    }
    return 0;
}