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
// Przykładowe niepoprawne rozwiązanie do zadania Dzielniki.
#include "dzilib.h"
#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define all(v) (v).begin(),(v).end()
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)


#define PII pair<int,int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define lint long long int

using namespace std;

int t, q;
lint c, n;

int zapytalem;
lint srednio;
bool gamble;
bitset<50> gamble_on;

int divs(lint x) {
	int dcnt = 0;
	int d;
	for (d = 1; d * 1LL * d <= x; d++) {
		if (x % d == 0) dcnt+=2;
	}
	if (d * 1LL * d == x) dcnt--;
	if ((d-1)*1LL*(d-1) == x) dcnt--;
	return dcnt;
}

// dla nieparzystych i ma zachodzic
// x + kon[i] = 2^(i-1) mod (2^i)
lint kon[50];
lint offset = 2137666LL;
int final_power;

vector<pair<lint, lint> > asked_so_far;

lint my_ask(lint x) {
	FOR(i,0,asked_so_far.size()) {
		if (asked_so_far[i].st == x) return asked_so_far[i].nd;
	} 
	lint ile = Ask(x+offset);
	zapytalem++;
	asked_so_far.pb({x,ile});
	return ile;
}

bool sprawdz(lint r, int k) {
	FOR(i,0,asked_so_far.size()) {
		if (asked_so_far[i].st%(1LL<<k) == r) {
			if (asked_so_far[i].nd % k == 0) return true;
			else return false;
		}
	}
	lint ile = my_ask(r);
	if (ile % k == 0) return true;
	return false;
}


void wyznacz3() {
	vector<lint> cands, ncands;
	FOR(i,0,8) {
		if (i == 7 && cands.size() == 0) {
			kon[3] = 7;
			return;
		}
		if (sprawdz(i, 3)) {
			cands.pb(i);
		}
	}
	
	lint found = -1;
	while (found == -1) {
		FOR(i,0,cands.size()) {
			lint co = cands[i];
			if (i == cands.size() - 1 && ncands.size() == 0) {
				found = cands[i];
				break;
			}
			lint ile = my_ask(co + 8);
			if (ile % 3 == 0) {
				ncands.pb(co+8);
			}
		}
		cands = ncands;
		ncands.clear();
	}
	kon[3] = found;
}

void wyznacz_nowe(int pos) {
	vector<lint> cands, ncands;
	FOR(i,0,4) {
		lint co = kon[pos-2]+(1LL<<(pos-3)) + i * ((1LL<<(pos-2))); 
		if (i == 3 && cands.size() == 0) {
				kon[pos] = co;
				return;
		}
		if (sprawdz(co, pos)) {
			if (gamble && gamble_on[pos]) {
				kon[pos] = co;
				return;
			}
			cands.pb(co);
		}
	}

	lint found = -1;
	while (found == -1) {
		FOR(i,0,cands.size()) {
			lint co = cands[i];
			if (i == cands.size() - 1 && ncands.size() == 0) {
				found = cands[i];
				break;
			}
			lint ile = my_ask(co + (1LL<<pos));
			if (ile % pos == 0) {
				ncands.pb(co+(1LL << pos));
			}
		}
		cands = ncands;
		ncands.clear();
	}
	kon[pos] = found;
}

void solve() {
	FOR(i,0,48) kon[i] = -1;
	zapytalem = 0;
	asked_so_far.clear();
	int mln = 1e6;
	if (srednio < 95) {
		offset += rand() % mln;
	}
	wyznacz3();
	for (int i = 5; i <= final_power; i+=2) {
		wyznacz_nowe(i);
	}
	// x + kon[47] = 2**46 mod(2**47)
	if (final_power != 45) {
		lint ans = ((1LL<<(final_power-1)) - kon[final_power])%(1LL<<final_power);
		if (ans < 0) ans += (1LL<<final_power);
		Answer(ans - offset);
	}
	if (final_power == 45) {
		lint ans = ((1LL<<(final_power-1)) - kon[final_power])%(1LL<<final_power);
		if (ans < 0) ans += (1LL<<final_power);
		vector<lint> cands, ncands; 
		while (ans - offset <= 1e14) {
			if (ans - offset > 0) cands.pb(ans);
			ans += (1LL<<45);
		}
		FOR(pytanko, 0, asked_so_far.size()) {
			lint co = asked_so_far[pytanko].st;
			lint ile = asked_so_far[pytanko].nd;
			FOR(i,0,cands.size()) {
				if (divs(cands[i] + co) == ile) {
					ncands.pb(cands[i]);
				}
			}
			if (ncands.size() == 1) {
				Answer(ncands[0] - offset);
				return;
			}
			else {
				cands = ncands;
				ncands.clear();
			}
		}
	}
}

int main() {
	srand(time(NULL));
	VI gambling = {};
	FOR(i,0,gambling.size()) gamble_on[gambling[i]] = 1;
	gamble = false;

  t = GetT();
  q = GetQ();
  c = GetC();
  n = GetN();
	
	lint p12 = 1e6 * 1LL * 1e6;
	if (c == p12 ) {
		final_power = 33;
	} else {
		final_power = 47;
	}

	srednio = q/t;
	if (srednio < 95) {
		gamble = true; 
		if (srednio == 82) { 
			gambling = {29,31,37,41,43,47};
		}
		if (srednio == 75) {
			gambling = {17,19,23,29,31,37,39,41,43,47};
		}
		if (srednio == 72) {
			gambling = {11,13,17,19,23,29,31,33,37,39,41,43,47};
			final_power -= 2;
		}
	}
	FOR(i,0,gambling.size()) gamble_on[gambling[i]] = 1;
  while(t--) {
		solve();
  }
  return 0;
}