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
#include<bits/stdc++.h>
#include "dzilib.h"
using namespace std;

//272962625

int soldierMasks[31];
int primes[350000];
int allPrimes[4000000];
multimap<int,int> setOfPrimes;

int firstPrimes[] = {2,3,5,7,11,13,17,19,23,29};

void printSoldier(int soldier)
{
	int soldier2=soldier;
	for (int i=0;i<30;i++)
	{
		//cerr<<soldier<<" "<<soldierMasks[n-1]<<endl;
		//cerr<<((soldier & soldierMasks[29])>>(29));
		soldier = soldier<<1;
	}
	cerr<<" "<<soldier2<<endl;
}

void printSoldier2(long long soldier)
{
	long long soldier2=soldier;
	for (int i=0;i<64;i++)
	{
		//cerr<<soldier<<" "<<soldierMasks[n-1]<<endl;
		cerr<<((soldier & (((long long)soldierMasks[29])<<33))>>(62));
		soldier = soldier<<1;
	}
	cerr<<" "<<soldier2<<endl;
}

void initSoldierMasks()
{
	soldierMasks[0]=1;
	for(int i=1;i<32;i++)
	soldierMasks[i] = soldierMasks[0]<<i;
}

void SetNonPrime(int numberToSet)
{
	int index = (numberToSet-1)/30;
	int bitIndex = (numberToSet-1)%30;
	primes[index] &= ~soldierMasks[bitIndex];
}

bool IsPrime(int number)
{
	int index = (number-1)/30;
	int bitIndex = (number-1)%30;
	return primes[index] & soldierMasks[bitIndex];
}

void initPrimes()
{
	for(int i=0;i<10;i++)
	{
		//cerr<<"setting "<<firstPrimes[i]<<" with "<<soldierMasks[firstPrimes[i]-1]<<endl;
		primes[0] |= soldierMasks[firstPrimes[i]-1];
		//cerr<<"primes after setting "<<primes[0]<<endl;;
	}
	for(int i=31;i<=60;i++)
	{
		if(i%2!=0 && i%3!=0 && i%5!=0)
			primes[1] |= soldierMasks[i-31];
	}
	for(int i=2;i<350000;i++)
		primes[i]=primes[1];
	for(int i=3;i<10;i++)
	{
		for(int j=firstPrimes[i]*firstPrimes[i];j<=10000100;j+=firstPrimes[i]*2)
		{
			SetNonPrime(j);
		}
	}
	for(int i=31;i<=4000;i++)
	{
		if(IsPrime(i))
		{
			for(int j=i*i;j<10000000;j+=2*i)
			{
				SetNonPrime(j);
			}
		}
	}
}

void FillPrimes2()
{
	int j=0;
	for(int i=2;i<10000100;i++)
	{
		if(IsPrime(i))
		{
			allPrimes[j]=i;
			j++;
			if(j>=4000000)
			{
				//cerr<<"outside primes2 bounds at i = "<<i<<endl;
				return;
			}
		}
	}
}

long long liczbaDzielnikow(long long n)
{
	if(n==0)return 0;
	else if(n==1)return 1;
	//cerr<<"a"<<endl;
	map<int, int> dzielniki;
	int sqrtOfN = sqrt(n);
	for(int i=0;allPrimes[i]<sqrtOfN+1 && n>1;i++)
	{
		//if(allPrimes[i]==0)
		//cerr<<"ALLPRIMES[i]=0. i = "<<i<<" "<<allPrimes[i-1]<<endl;
	    while(n%(long long)(allPrimes[i])==0)
		{
			//cerr<<"c";
			dzielniki[i]++;
			//cerr<<"d";
			n/=(long long)allPrimes[i];
			//cerr<<"e";
			//cerr<<i<<" "<<sqrtOfN<<" "<<n<<" "<<allPrimes[i];
		}		


	}
	if(n>1)
		dzielniki[-1]++;
		//cerr<<"f"<<endl;
	long long result=1;
	for(auto kv: dzielniki)
	{
		result*=(kv.second+1);
	}
	//cerr<<"b"<<endl;
	return result;
}

int main()
{
	long long t,n,q,c;
	n = GetN();
	q = GetQ();
	c = GetC();
	t = GetT();
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	initSoldierMasks();
	initPrimes();
	FillPrimes2();
	for(int i=0;i<10000000/30;i++)
	{
		setOfPrimes.insert({primes[i],i});
	}

	//for(long long j= 100;j<1000000000000000ll;j*=10)
	//{
	//	long long result = 0;
	//	for(long long i=j;i<j+100;i++)
	//	{
	//		result += liczbaDzielnikow(i);
	//	}
	//	cerr<<j<<" "<<result<<endl;;
	//}
	//cerr<<endl;
	long long dzielniki[100];
	//cerr<<t<<" "<<n<<" "<<q<<" "<<c<<" "<<endl;;	
	vector<int> solutions;
	for(;t>0;t--)
	{
		solutions.clear();
		long long hash=0;
		for(int i=0;i<72;i++)
		{
			dzielniki[i] = Ask(i);
		}
		for(int i=0;i<30;i++)
		{
			if(dzielniki[i]==2)
				hash = hash | soldierMasks[i];
		}
			//cerr<<hash<<endl;;
		for(int i=0;i<30;i++)
		{
			if(dzielniki[i+30]==2)
			{
				hash = hash | (((long long)soldierMasks[i])<<30);
				//cerr<<"tu "<<i<<" "<<hash<<endl;
			}
		}
			//cerr<<hash<<endl;;
		long long hash2=0;

		//hash2 = (((long long)primes[1])<<30) | primes[0];
		for(int i=1;i<10000000;i++)
		{
			if(i==68)
			{
				//printSoldier2(hash);
				//printSoldier2(hash2);

				//cerr<<hash<<" "<<hash2<<endl;
			}
			hash2 = hash2>>1;
			if(hash==hash2)
			{
				solutions.push_back(i-60);
				
			}
			if(IsPrime(i))
				hash2 = hash2 | (((long long)soldierMasks[30])<<30);
			//	cerr<<"found candidate "<<i<<endl;
		}
		bool ok=false;
		for(auto solution:solutions)
		{
			ok=true;
			int dzielniki2[100];
			for(int i=0;i<70;i++)
				if(dzielniki[i] != liczbaDzielnikow(solution+i))
					ok=false;
			if(ok)
			{
				Answer(solution);
				break;
			}
		}
		if(!ok)
			Answer(1);
		//if(hash!=hash2)cerr<<"no luck"<<endl;
			//for(int i=0;i<60;i++)cerr<<dzielniki[i]<<endl;
			//cerr<<(((long long)soldierMasks[15])<<30)<<endl;;
	}
}