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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
#include "dzilib.h"
#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,auto x)->decltype(x.end(),o);
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";}
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

namespace yosupo {
	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;
}

}


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;
}

mt19937_64 rng_64(0);
LL rd(LL l, LL r) {
	return uniform_int_distribution<LL>(l, r)(rng_64);
}

namespace acmlib {

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

} // acmlib

struct Sieve {
	LL l, r;
	vector<LL> val, cnt;
	Sieve(LL n) : l(-1), r(-1) {
		const int s = int(sqrt(n) + 10);
		acmlib::sieve(s);
	}
	void sieve(LL _l, LL _r) {
		l = max(1ll, _l);
		r = _r;
		assert(l <= r);
		const int len = int(r - l + 1);
		val.resize(len);
		iota(val.begin(), val.end(), l);
		cnt.resize(len);
		fill(cnt.begin(), cnt.end(), 1);
		for (int p : acmlib::primes) {
			for (LL n = (l + p - 1) / p * p; n <= r; n += p) {
				int cur = 1;
				while (val[n - l] % p == 0) {
					++cur;
					val[n - l] /= p;
				}
				cnt[n - l] *= cur;
			}
		}
		REP(i, len) {
			if (val[i] > 1)
				cnt[i] *= 2;
		}
	}
	LL operator()(LL n) {
		assert(n >= l and n <= r);
		return cnt[n - l];
	}
};

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;
}
// BEGIN HASH
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;
} // END HASH
vector<pair<LL, int>> get_pairs(LL n) {
	/*
	auto v = factor(n);
	sort(v.begin(), v.end());
	*/
	auto v = yosupo::factorize(n);
	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;
}
int cnt_factors(LL n) {
	int ret = 1;
	for (auto [_, x] : get_pairs(n))
		ret *= 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;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	const int t = GetT();
	const int q = GetQ();
	const int limit = q / t;
	const LL n = GetN();
	debug(t, q, limit, n);

	Sieve s(10 * n);

	const int sub2 = int(1e6);
	const int sub3 = int(1e9);

	if (n <= sub2) {
		s.sieve(1, 3 * n);
		REP(tt, t) {
			vector<pair<LL, LL>> queries(limit);
			for (auto& [y, ans] : queries) {
				y = rd(0, n);
				ans = Ask(y);
			}
			auto ok = [&](int x) {
				for (auto [y, ans] : queries) {
					if (s(x + y) != ans) {
						return false;
					}
				}
				return true;
			};
			bool found = false;
			FOR(x, 1, n) {
				if (ok(x)) {
					Answer(x);
					found = true;
					break;
				}
			}
			assert(found);
		}
		return 0;
	}

	if (n == sub3) {
		const int query_range = n;
		const int L = n + query_range;
		acmlib::sieve(1e6);
		const auto primes = acmlib::primes;
		REP(tt, t) {
			vector<pair<LL, LL>> queries(limit);
			for (auto& [y, ans] : queries) {
				y = rd(0, query_range);
				ans = Ask(y);
			}
			auto ok = [&](LL x) {
				if (x < 1 or x > n)
					return false;
				for (auto [y, ans] : queries) {
					if (cnt_factors(x + y) != ans) {
						return false;
					}
				}
				return true;
			};

			pair<LL, LL> best;
			for (auto [y, ans] : queries)
				best = max(best, pair(ans, y));
			const auto [val, y] = best;
			debug(val, y);

			bool found = false;
			vector<int> exponents;
			function<void(LL)> rec = [&](LL left) {
				if (left == 1) {
					function<void(LL, int, int)> gen = [&](LL x, int id_expo, int id_prime) {
						if (id_expo == ssize(exponents)) {
							if (ok(x - y)) {
								debug(x - y);
								Answer(x - y);
								found = true;
							}
							return;
						}
						LL lower_bound = x;
						FOR(i, id_expo, ssize(exponents) - 1) {
							REP(j, exponents[i]) {
								lower_bound *= primes[id_prime + i - id_expo];
								if (lower_bound > L)
									return;
							}
						}
						FOR(i, id_prime, ssize(primes)) {
							LL cur = x;
							REP(j, exponents[id_expo]) {
								cur *= primes[i];
								if (cur > L)
									return;
							}
							gen(cur, id_expo + 1, i + 1);
							if (found)
								return;
						}
					};
					gen(1, 0, 0);
					return;
				}
				for (auto f : all_factors(left)) {
					if (f == 1)
						continue;
					exponents.emplace_back(f - 1);
					rec(left / f);
					exponents.pop_back();
				}
			};
			rec(val);
			assert(found);
		}
		return 0;
	}

	acmlib::sieve(100);
	const auto primes = acmlib::primes;
	const vector<int> squares = {4, 9};
	const int S = ssize(primes);
	const int Q = ssize(squares);

	REP(tt, t) {
		const LL q_min = 1e8;
		const LL q_max = 1e9;

		vector<pair<LL, LL>> queries(limit);
		int hura = 0;
		vector<int> prime_queries;
		vector<int> nearly_primes;
		vector<int> nearly_primes2, can6, can12;
		map<int, int> hh;
		for (auto& [y, ans] : queries) {
			y = rd(q_min, q_max);
			ans = Ask(y);
			if (ans == 2) {
				prime_queries.emplace_back(y);
			}
			if (ans == 4) {
				nearly_primes.emplace_back(y);
			}
			if (ans == 8) {
				nearly_primes2.emplace_back(y);
			}
			if (ans == 6) {
				can6.emplace_back(y);
			}
			if (ans == 12) {
				can12.emplace_back(y);
			}
			hh[ans]++;
		}
		vector<pair<int, int>> yy(hh.begin(), hh.end());
		for (auto& [a, b] : yy)
			swap(a, b);
		sort(yy.rbegin(), yy.rend());
		debug(yy);
		debug(ssize(prime_queries));
		debug(ssize(nearly_primes));
		debug(ssize(nearly_primes2));
		debug(ssize(can6));
		debug(ssize(can12));

		LL M = 1;
		vector<LL> values = {0};
		vector<vector<int>> seen(S), seen6(Q);
		vector<pair<int, int>> pairs;
		auto reduce = [](LL y, int p) {
			y %= p;
			y = -y;
			y += p;
			y %= p;
			return y;
		};

		REP(i, S) {
			const int p = primes[i];
			seen[i].resize(p);
			for (auto y : prime_queries) {
				y = reduce(y, p);
				seen[i][y] = true;
			}
		}
		REP(i, Q) {
			const int p = squares[i];
			seen6[i].resize(p);
			for (auto y : prime_queries) {
				y = reduce(y, p);
				seen6[i][y] = true;
			}
		}
		REP(i, S)
			debug(i, primes[i], seen[i]);

		vector<int> okay(S), okay6(Q);
		auto do_okay = [&] {
			REP(i, S) {
				const int p = primes[i];
				if (accumulate(seen[i].begin(), seen[i].end(), 0) == p - 1)
					okay[i] = true;
			}
			REP(i, Q) {
				const int p = squares[i];
				if (accumulate(seen6[i].begin(), seen6[i].end(), 0) >= p - int(sqrt(p)))
					okay6[i] = true;
			}
			debug(okay6, seen6);
		};

		auto do_qua = [&] {
			do_okay();
			vector<pair<int, int>> quasi_primes;
			REP(i, S) {
				if (not okay[i])
					continue;
				const int p = primes[i];
				debug(i, p);
				for (auto y : nearly_primes) {
					auto z = reduce(y, p);
					if (not seen[i][z]) {
						quasi_primes.emplace_back(y, p);
					}
				}
			}

			REP(i, Q) {
				if (not okay6[i])
					continue;
				const int p = squares[i];
				debug(i, p);
				for (auto y : can6) {
					auto z = reduce(y, p);
					if (not seen6[i][z]) {
						quasi_primes.emplace_back(y, int(sqrt(p)));
						debug("hurra");
					}
				}
			}

			debug(quasi_primes);
			REP(i, S) {
				const int p = primes[i];
				for (auto [y, forbidden_p] : quasi_primes) {
					if (p == forbidden_p)
						continue;
					y = reduce(y, p);
					seen[i][y] = true;
				}
			}
		};

		auto do_qua2 = [&] {
			do_okay();
			vector<tuple<int, int, int>> quasi_primes2;
			REP(i, S) {
				if (not okay[i])
					continue;
				const int p1 = primes[i];
				REP(j, i) {
					if (not okay[j])
						continue;
					const int p2 = primes[j];
					debug(i, p1, p2);
					for (auto y : nearly_primes2) {
						auto z1 = reduce(y, p1);
						auto z2 = reduce(y, p2);
						if (not seen[i][z1] and not seen[j][z2]) {
							quasi_primes2.emplace_back(y, p1, p2);
						}
					}
				}
			}
			REP(i, S) {
				if (not okay[i])
					continue;
				const int p1 = primes[i];
				REP(j, Q) {
					if (not okay6[j])
						continue;
					const int p2 = squares[j];
					if (p1 * p1 == p2)
						continue;
					debug(i, p1, p2);
					for (auto y : can12) {
						auto z1 = reduce(y, p1);
						auto z2 = reduce(y, p2);
						if (not seen[i][z1] and not seen6[j][z2]) {
							quasi_primes2.emplace_back(y, p1, int(sqrt(p2)));
						}
					}
				}
			}
			debug(quasi_primes2);
			REP(i, S) {
				const int p = primes[i];
				for (auto [y, f1, f2] : quasi_primes2) {
					if (p == f1 or p == f2)
						continue;
					y = reduce(y, p);
					seen[i][y] = true;
				}
			}
		};

		REP(i, 5) {
			do_qua();
			do_qua2();
		}

		REP(i, S) {
			int not_seen = 0;
			for (int x : seen[i])
				not_seen += not x;
			pairs.emplace_back(not_seen, -i);
		}

		sort(pairs.begin(), pairs.end());
		for (auto& [_, i] : pairs)
			i = -i;
		debug(pairs);

		for (auto [_, id] : pairs) {
			const int p = primes[id];
			const LL new_M = M * p;
			vector<LL> new_values;
			REP(i, p) {
				if (seen[id][i])
					continue;
				for (auto x : values) {
					auto temp = crt(x, M, i, p);
					if (temp <= n)
						new_values.emplace_back(temp);
				}
			}
			swap(values, new_values);
			debug(p, M, ssize(values));
			M = new_M;
			if (M > n)
				break;
		}
		debug(ssize(values));

		for (auto x : values) {
			if ([&] {
				for (auto [y, ans] : queries) {
					if (cnt_factors(x + y) != ans)
						return false;
				}
				return true;
			}()) {
				Answer(x);
				break;
			}
		}
	}
}