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
#include "dzilib.h"
#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   vi         = vector<int>;
using   pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

namespace factorization {
	ll modAdd(ll x, ll y, ll m) {
		x += y;
		return x < m ? x : x-m;
	}

	ll modMul(ll a, ll b, ll m) {
		ll c = ll((long double)a * b / m);
		ll r = (a*b - c*m) % m;
		return r < 0 ? r+m : r;
	}

	ll modPow(ll x, ll e, ll m) {
		ll t = 1;
		while (e) {
			if (e & 1) t = modMul(t, x, m);
			e >>= 1;
			x = modMul(x, x, m);
		}
		return t;
	}

	constexpr ll BASES[] = {
		2, 325, 9'375, 28'178,
		450'775, 9'780'504, 1'795'265'022
	};

	bool isPrime(ll p) {
		if (p <= 2) return p == 2;
		if (p%2 == 0) return 0;

		ll d = p-1, t = 0;
		while (d%2 == 0) d /= 2, t++;

		each(a, BASES) if (a%p) {
			ll b = modPow(a%p, d, p);
			if (b == 1 || b == p-1) continue;
			rep(i, 1, t) if ((b = modMul(b, b, p)) == p-1) break;
			if (b != p-1) return 0;
		}

		return 1;
	}

	using Factor = pair<ll, int>;

	void rho(vector<ll>& out, ll n) {
		if (n <= 1) return;
		if (isPrime(n)) out.pb(n);
		else if (n%2 == 0) rho(out,2), rho(out,n/2);
		else for (ll a = 2;; a++) {
			ll x = 2, y = 2, d = 1;
			while (d == 1) {
				x = modAdd(modMul(x, x, n), a, n);
				y = modAdd(modMul(y, y, n), a, n);
				y = modAdd(modMul(y, y, n), a, n);
				d = __gcd(abs(x-y), n);
			}
			if (d != n) return rho(out,d),rho(out,n/d);
		}
	}

	vector<Factor> factorize(ll n) {
		vector<Factor> ret;
		vector<ll> raw;
		rho(raw, n);
		sort(all(raw));
		each(f, raw) {
			if (ret.empty() || ret.back().x != f)
				ret.pb({ f, 1 });
			else
				ret.back().y++;
		}
		return ret;
	}

	ll divs(ll n) {
		ll ret = 1;
		each(f, factorize(n)) ret *= f.y+1;
		return ret;
	}
};

mt19937_64 rnd(248394427);
ll N, C;

vector<array<int, 2>> trie;
vi mod, agg;

ll randLong(ll a, ll b) {
	return a + ll(rnd() % (b-a+1));
}

void add(int i, ll mask, int bits) {
	if (bits == 0) {
		mod[i]++;
		agg[i]++;
		return;
	}

	ll b = mask & 1;

	if (!trie[i][b]) {
		trie[i][b] = sz(trie);
		trie.pb({0, 0});
		mod.pb(0);
		agg.pb(0);
	}

	add(trie[i][b], mask >> 1, bits-1);
	agg[i] = max(agg[trie[i][0]], agg[trie[i][1]]) + mod[i];
}

void add(ll mask, int bits) {
	add(1, mask, bits);
}

pair<ll, int> sample(int bits) {
	ll mask = 0;
	int i = 1, depth = 0;
	rep(j, 0, bits) {
		if (agg[i] > 0) {
			int a = trie[i][0], b = trie[i][1];
			int c = (agg[a] != agg[b] ? agg[a] < agg[b] : rnd() % 2);
			mask |= ll(c) << j;
			i = (c ? b : a);
			depth++;
		} else {
			mask |= ll(rnd() % 2) << j;
		}
	}
	return {mask, depth};
}

ll solve() {
	constexpr int MIN_ITERS = 40;
	constexpr int BRUTE = 700;

	int bits = 0;
	while ((1ll<<bits) <= N/BRUTE) bits++;

	vector<pair<ll, ll>> history;
	trie = {{0, 0}, {0, 0}};
	mod = agg = {0, 0};

	for (int i = 0;; i++) {
		auto [guess, depth] = sample(bits);
		ll a = (randLong(1, (C+guess) >> bits) << bits) - guess;
		ll divs = Ask(a);
		history.pb({a, divs});

		if (i >= MIN_ITERS && depth == bits) {
			for (ll x = guess; x <= N; x += 1ll<<bits) {
				bool ok = 1;
				each(p, history) {
					if (factorization::divs(x+p.x) != p.y) {
						ok = 0;
						break;
					}
				}
				if (ok) return x;
			}
		}

		rep(p, 0, 60) {
			if (divs % (p+1) == 0) {
				ll mask = (1ll << (p+1)) - a;
				mask ^= 1ll << p;
				add(mask, p+1);
			}
		}
	}
}

void run() {
	int T = GetT();
	N = GetN();
	C = GetC();
	rep(i, 0, T) Answer(solve());
}

int main() {
#ifdef LOC
	while (true) {
		run();
		MyReset();
	}
#else
	run();
#endif
}