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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#ifndef LOCAL
#include "dzilib.h"
#endif

#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#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())
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

#ifdef LOCAL
namespace BIB {
LL llmul(LL a, LL b, LL m) {
	return LL(__int128_t(a) * b % m);
}
LL llpowi(LL a, LL n, LL m) {
	for (LL ret = 1;; n /= 2) {
		if (n == 0)
			return ret;
		if (n % 2)
			ret = llmul(ret, a, m);
		a = llmul(a, a, m);
	}
}
bool miller_rabin(LL n) {
	if(n < 2) return false;
	int r = 0;
	LL d = n - 1;
	while(d % 2 == 0)
		d /= 2, r++;
	for(int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) {
		if (a % n == 0) continue;
		LL x = llpowi(a, d, n);
		if(x == 1 || x == n - 1)
			continue;
		bool composite = true;
		REP(i, r - 1) {
			x = llmul(x, x, n);
			if(x == n - 1) {
				composite = false;
				break;
			}
		}
		if(composite) return false;
	}
	return true;
}
LL rho_pollard(LL n) {
	if(n % 2 == 0) return 2;
	for(LL i = 1;; i++) {
		auto f = [&](LL x) { return (llmul(x, x, n) + i) % n; };
		LL x = 2, y = f(x), p;
		while((p = __gcd(n - x + y, n)) == 1)
			x = f(x), y = f(f(y));
		if(p != n) return p;
	}
}
vector<LL> factor(LL n) {
	if(n == 1) return {};
	if(miller_rabin(n)) return {n};
	LL x = rho_pollard(n);
	auto l = factor(x), r = factor(n / x);
	l.insert(l.end(), r.begin(), r.end());
	return l;
}
vector<pair<LL, int>> get_pairs(LL n) {
	auto v = factor(n);
	sort(v.begin(), v.end());
	vector<pair<LL, int>> ret;
	REP(i, ssize(v)) {
		int x = i + 1;
		while (x < ssize(v) and v[x] == v[i])
			++x;
		ret.emplace_back(v[i], x - i);
		i = x - 1;
	}
	return ret;
}
vector<LL> all_factors(LL n) {
	auto v = get_pairs(n);
	vector<LL> ret;
	function<void(LL,int)> gen = [&](LL val, int p) {
		if (p == ssize(v)) {
			ret.emplace_back(val);
			return;
		}
		auto [x, cnt] = v[p];
		gen(val, p + 1);
		REP(i, cnt) {
			val *= x;
			gen(val, p + 1);
		}
	};
	gen(1, 0);
	return ret;
}
}
mt19937_64 _rng(random_device{}());
LL _rd(LL l, LL r) {
	return uniform_int_distribution<LL>(l, r)(_rng);
}
int _t, _it, _q, _iq;
LL _n, _c;
LL _x;
int GetT() {
	assert(_t > 0);
	return _it;
}
int GetQ() {
	assert(_t > 0);
	return _iq;
}
LL GetN() {
	assert(_t > 0);
	return _n;
}
LL GetC() {
	assert(_t > 0);
	return _c;
}
LL Ask(LL y) {
	assert(_q > 0);
	--_q;
	assert(_t > 0);
	assert(0 <= y && y <= _c);
	return ssize(BIB::all_factors(_x + y));
}
void Answer(LL z) {
	assert(_t > 0);
	assert(z == _x);
	_x = _rd(1, _n);
	--_t;
}
#endif

mt19937_64 rng(321984127);
LL rd(LL l, LL r) {
	return uniform_int_distribution<LL>(l, r)(rng);
}

namespace OtherRho {
// ---- gcd ----

uint64_t gcd_stein_impl( uint64_t x, uint64_t y ) {
    if( x == y ) { return x; }
    const uint64_t a = y - x;
    const uint64_t b = x - y;
    const int n = __builtin_ctzll( b );
    const uint64_t s = x < y ? a : b;
    const uint64_t t = x < y ? x : y;
    return gcd_stein_impl( s >> n, t );
}

uint64_t gcd_stein( uint64_t x, uint64_t y ) {
    if( x == 0 ) { return y; }
    if( y == 0 ) { return x; }
    const int n = __builtin_ctzll( x );
    const int m = __builtin_ctzll( y );
    return gcd_stein_impl( x >> n, y >> m ) << ( n < m ? n : m );
}

// ---- is_prime ----

uint64_t mod_pow( uint64_t x, uint64_t y, uint64_t mod ) {
    uint64_t ret = 1;
    uint64_t acc = x;
    for( ; y; y >>= 1 ) {
        if( y & 1 ) {
            ret = __uint128_t(ret) * acc % mod;
        }
        acc = __uint128_t(acc) * acc % mod;
    }
    return ret;
}

bool miller_rabin( uint64_t n, const std::initializer_list<uint64_t>& as ) {
    return std::all_of( as.begin(), as.end(), [n]( uint64_t a ) {
        if( n <= a ) { return true; }

        int e = __builtin_ctzll( n - 1 );
        uint64_t z = mod_pow( a, ( n - 1 ) >> e, n );
        if( z == 1 || z == n - 1 ) { return true; }

        while( --e ) {
            z = __uint128_t(z) * z % n;
            if( z == 1 ) { return false; }
            if( z == n - 1 ) { return true; }
        }

        return false;
    });
}

bool is_prime( uint64_t n ) {
    if( n == 2 ) { return true; }
    if( n % 2 == 0 ) { return false; }
    if( n < 4759123141 ) { return miller_rabin( n, { 2, 7, 61 } ); }
    return miller_rabin( n, { 2, 325, 9375, 28178, 450775, 9780504, 1795265022 } );
}

// ---- Montgomery ----

class Montgomery {
    uint64_t mod;
    uint64_t R;
public:
    Montgomery( uint64_t n ) : mod(n), R(n) {
       for( size_t i = 0; i < 5; ++i ) {
          R *= 2 - mod * R;
       }
    }

    uint64_t fma( uint64_t a, uint64_t b, uint64_t c ) const {
        const __uint128_t d = __uint128_t(a) * b;
        const uint64_t    e = c + mod + ( d >> 64 );
        const uint64_t    f = uint64_t(d) * R;
        const uint64_t    g = ( __uint128_t(f) * mod ) >> 64;
        return e - g;
    }

    uint64_t mul( uint64_t a, uint64_t b ) const {
        return fma( a, b, 0 );
    }
};

// ---- Pollard's rho algorithm ----

uint64_t pollard_rho( uint64_t n ) {
    if( n % 2 == 0 ) { return 2; }
    const Montgomery m( n );

    constexpr uint64_t C1 = 1;
    constexpr uint64_t C2 = 2;
    constexpr uint64_t M = 512;

    uint64_t Z1 = 1;
    uint64_t Z2 = 2;
retry:
    uint64_t z1 = Z1;
    uint64_t z2 = Z2;
    for( size_t k = M; ; k *= 2 ) {
        const uint64_t x1 = z1 + n;
        const uint64_t x2 = z2 + n;
        for( size_t j = 0; j < k; j += M ) {
            const uint64_t y1 = z1;
            const uint64_t y2 = z2;

            uint64_t q1 = 1;
            uint64_t q2 = 2;
            z1 = m.fma( z1, z1, C1 );
            z2 = m.fma( z2, z2, C2 );
            for( size_t i = 0; i < M; ++i ) {
                const uint64_t t1 = x1 - z1;
                const uint64_t t2 = x2 - z2;
                z1 = m.fma( z1, z1, C1 );
                z2 = m.fma( z2, z2, C2 );
                q1 = m.mul( q1, t1 );
                q2 = m.mul( q2, t2 );
            }
            q1 = m.mul( q1, x1 - z1 );
            q2 = m.mul( q2, x2 - z2 );

            const uint64_t q3 = m.mul( q1, q2 );
            const uint64_t g3 = gcd_stein( n, q3 );
            if( g3 == 1 ) { continue; }
            if( g3 != n ) { return g3; }

            const uint64_t g1 = gcd_stein( n, q1 );
            const uint64_t g2 = gcd_stein( n, q2 );

            const uint64_t C = g1 != 1 ? C1 : C2;
            const uint64_t x = g1 != 1 ? x1 : x2;
            uint64_t       z = g1 != 1 ? y1 : y2;
            uint64_t       g = g1 != 1 ? g1 : g2;

            if( g == n ) {
                do {
                    z = m.fma( z, z, C );
                    g = gcd_stein( n, x - z );
                } while( g == 1 );
            }
            if( g != n ) {
                return g;
            }

            Z1 += 2;
            Z2 += 2;
            goto retry;
        }
    }
}

void factorize_impl( uint64_t n, std::vector<uint64_t>& ret ) {
    if( n <= 1 ) { return; }
    if( is_prime( n ) ) { ret.push_back( n ); return; }

    const uint64_t p = pollard_rho( n );

    factorize_impl( p, ret );
    factorize_impl( n / p, ret );
}

std::vector<uint64_t> factorize( uint64_t n ) {
    std::vector<uint64_t> ret;
    factorize_impl( n, ret );
    std::sort( ret.begin(), ret.end() );
    return ret;
}
}
using OtherRho::factorize;

tuple<LL, LL, LL> extended_gcd(LL a, LL b) {
	if(a == 0)
		return {b, 0, 1};
	auto [gcd, x, y] = extended_gcd(b % a, a);
	return {gcd, y - x * (b / a), x};
}
LL crt(LL a, LL m, LL b, LL n) {
	if(n > m) swap(a, b), swap(m, n);
	auto [d, x, y] = extended_gcd(m, n);
	assert((a - b) % d == 0);
	LL ret = (b - a) % n * x % n / d * m + a;
	return ret < 0 ? ret + m * n / d : ret;
}

vector<bool> comp;
vector<int> primes, prime_div;
void sieve(int n) {
	primes.clear();
	comp.resize(n + 1);
	prime_div.resize(n + 1);
	FOR(i, 2, n) {
		if (!comp[i]) primes.emplace_back(i), prime_div[i] = i;
		for (int p : primes) {
			int x = i * p;
			if (x > n) break;
			comp[x] = true;
			prime_div[x] = p;
			if (i % p == 0) break;
		}
	}
}

unordered_map<LL, LL> known_queries;
LL ask(LL y) {
	if (!known_queries.count(y))
		known_queries[y] = Ask(y);
	return known_queries[y];
}

int main() {
#ifdef LOCAL
	cin.tie(0)->sync_with_stdio(0);
	cin >> _t >> _n >> _q >> _c;
	_it = _t;
	_iq = _q;
	_x = _rd(1, _n);
	debug(_x);
#endif

	/*
	const int PREP = 5e7;
	sieve(PREP);
	is_comp = comp;
	prime_div_rho = prime_div;
	const int SMALL_PREP = 10;
	sieve(SMALL_PREP);
	small_primes = primes;
	*/

	int tests = GetT();
	LL n = GetN();
	LL c = GetC();
	int q = GetQ();
	debug(tests, n, c, q);

	const int Z = min(200, q / tests);
	debug(Z);
	sieve(Z);
	vector<int> imp, nimp;
	for (int p : primes) {
		if (p * p <= Z)
			imp.emplace_back(p);
		else
			nimp.emplace_back(p);
	}
	debug(imp, nimp);
	int sz = ssize(primes);
	vector<int> min_wyk(sz);
	vector<LL> min_val(sz);
	REP (i, sz) {
		int p = primes[i];
		int pp = 1;
		while (Z / pp / p) {
			pp *= p;
			++min_wyk[i];
		}
		min_val[i] = pp;
	}
	debug(min_wyk);
	debug(min_val);
	vector<vector<LL>> pots(sz);
	REP (i, sz) {
		pots[i].emplace_back(1);
		while ((n + c) / pots[i].back() / primes[i])
			pots[i].emplace_back(pots[i].back() * primes[i]);
	}
	const int AC = 10'000;
	vector<int> max_wyk(sz);
	REP (i, sz) {
		REP (j, ssize(pots[i])) {
			if (pots[i][j] / pots[i][min_wyk[i]] <= AC)
				max_wyk[i] = j;
			else
				break;
		}
	}
	debug(max_wyk);
	debug(pots);
	vector<vector<double>> log_pots(sz);
	REP (i, sz)
		for (auto v : pots[i])
			log_pots[i].emplace_back(log(v));
	debug(log_pots);

	auto calc_diff = [&](int p, int beg, int wyk) {
		vector<int> diff(Z, 1);
		LL pp = 1;
		FOR (i, 1, wyk) {
			pp *= p;
			LL w = beg;
			while (w >= 0) {
				++diff[w];
				w -= pp;
			}
			w = beg + pp;
			while (w < Z) {
				++diff[w];
				w += pp;
			}
		}
		return diff;
	};

	vector diff(sz, vector(Z, vector<vector<int>>()));
	REP (i, sz) {
		REP (j, Z) {
			diff[i][j].resize(ssize(pots[i]));
			FOR (pot, min_wyk[i], ssize(pots[i]) - 1) {
				diff[i][j][pot] = calc_diff(primes[i], j, pot);
			}
		}
	}

	REP (test, tests) {
		LL offset = rd(c * 0.99, c);
		debug(offset);
		const int INF = 1e9;
		vector<LL> answers(Z);
		REP (i, Z)
			answers[i] = ask(offset + i);
		debug(answers);
		vector<vector<pair<int, int>>> positions(sz);
		REP (i, sz) {
			FOR (pot, min_wyk[i], max_wyk[i]) {
				REP (j, Z) {
					bool ok = true;
					REP (k, Z)
						if (answers[k] % diff[i][j][pot][k]) {
							ok = false;
							break;
						}
					if (ok)
						positions[i].emplace_back(j, pot);
				}
			}
			debug(i, positions[i]);
		}

		auto modu = [&](LL val, LL mod) {
			return (val % mod + mod) % mod;
		};

		vector<pair<LL, int>> kol;
		REP (i, Z)
			kol.emplace_back(answers[i], i);
		sort(kol.rbegin(), kol.rend());
		unordered_set<LL> checked;
		auto check = [&](LL x) {
			if (checked.count(x))
				return false;
			checked.emplace(x);
			if (ssize(checked) % 100'000 == 0)
				cerr << "checked: " << ssize(checked) << endl;
			debug("check", x);
			for (auto [cnt, i] : kol) {
				auto v = factorize(x + i + offset);
				int mno = 1;
				int ost = v[0];
				int ile = 0;
				for (int u : v) {
					if (u != ost) {
						mno *= ile + 1;
						ile = 0;
						ost = u;
					}
					++ile;
				}
				if (mno * (ile + 1) != cnt)
					return false;
			}
			return true;
		};

		set<pair<LL, LL>> evaled;
		auto eval = [&](LL mod, LL val) {
			if (evaled.count(pair(mod, val)))
				return -1ll;
			evaled.emplace(mod, val);
			debug("eval", mod, val);
			while (val <= n) {
				if (check(val))
					return val;
				val += mod;
			}
			return -1ll;
		};

		const int LIM = 1e3;
		debug(mno);
		bool found = false;
		double CUTOFF = 1e-2;
		double CUT2 = 2.0;
		double CUT3 = 4.0;
		double INI = log(offset);
		double DELTA = 2.0;
		vector<double> material(Z, INI);
		REP (xd, 10) {
			function<void(int, LL, LL, double)> rek = [&](int i, LL mod, LL val, double pro) {
				if (found)
					return;
				if (n / mod <= LIM) {
					auto x = eval(mod, val);
					if (x != -1) {
						Answer(x);
						found = true;
					}
					return;
				}
				assert(i < sz);
				double material_sum = 0;
				REP (i, Z) {
					material_sum += material[i];
					if (material[i] < CUT2)
						return;
				}
				material_sum /= Z;
				if (material_sum < CUT3)
					return;
				/*
				int zli = 0;
				REP (i, Z)
					if (answers[i] < 3 && material[i] > INI - DELTA)
						++zli;
				if (zli > 40)
					return;
					*/
				for (auto [pos, pot] : positions[i]) {
					double npro = pro / double(pots[i][pot] / pots[i][min_wyk[i]]);
					if (npro < CUTOFF)
						continue;
					if (answers[pos] % (pot + 1) || answers[pos] == (pot + 1))
						continue;
					bool ok = true;
					REP (j, Z)
						if (answers[j] % diff[i][pos][pot][j] ||
								answers[j] == diff[i][pos][pot][j]) {
							ok = false;
							break;
						}
					if (!ok)
						continue;
					REP (j, Z) {
						answers[j] /= diff[i][pos][pot][j];
						material[j] -= log_pots[i][diff[i][pos][pot][j]];
					}
					rek(i + 1, mod * pots[i][pot], crt(val, mod, modu(-offset - pos, pots[i][pot]), pots[i][pot]), npro);
					REP (j, Z) {
						answers[j] *= diff[i][pos][pot][j];
						material[j] += log_pots[i][diff[i][pos][pot][j]];
					}
				}
			};
			rek(0, 1, 0, 1.0);
			CUTOFF /= 5;
			CUT2 += 1;
			CUT3 += 2;
		}
		assert(found);
	}
#ifdef LOCAL
	assert(_t == 0);
	cout << "OK\n";
#endif
}