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
#include <bits/stdc++.h>
#include "krazki.h"
#include "message.h"
using namespace std;
/*
const int _library_pipe_height = 200 * 1000 * 1000;
const int _library_number_of_discs = 200 * 1000 * 1000;
const long long int zakres = 1LL << 60;
int PipeHeight() {
return _library_pipe_height;
}
int NumberOfDiscs() {
return _library_number_of_discs;}
long long int HoleDiameter(long long int i) {
assert(1 <= i && i <= _library_pipe_height);
return ((i * i) % 608856 * 6783434LL + 3875348573) % zakres;}
long long int DiscDiameter(long long int j) {
assert(1 <= j && j <= _library_number_of_discs);
return (j * 608856LL * 6783434LL + 38734538573LL) % zakres;}*/
/*
 long long tab[1000 * 1000 + 10];

long long wzorcowka()
{
	int n,m;
	long long a;
	n = PipeHeight();
	m = NumberOfDiscs();
	tab[0]=1LL<<60;
	tab[1] = HoleDiameter(1);
	for (int i=2;i<=n;i++)
	{
		a = HoleDiameter(i);
		tab[i]=min(a,tab[i-1]);
	}
	long long ost=n;
	for (int i=0;i<m;i++)
	{
		a = DiscDiameter(i + 1);
		if (ost<0) continue;
		while(tab[ost]<a) ost--;
		if (ost>=0) {
// 			printf("%lld ", PipeHeight() -  ost + 1);
			ost--;
		}
	}
	printf("\nwzorcowka %lld\n", ost + 1);
	return ost + 1;
}
*/

const int N = 4 * 1000 * 1000 + 10;

long long pipes[N];
int n_pipes;
int start_pipes;

long long discs[N];
int n_discs;
int start_discs;

long long min_pipes[1000];
long long max_discs[1000];
long long p_discs[1000];

void readPipes() {
	int N_THREAD = NumberOfNodes();
	int MY_ID = MyNodeId();
	int n = PipeHeight();
	int ile = n/N_THREAD;
	int p = ile * MY_ID + 1;
	int k = ile * (MY_ID + 1) + 1;
// 	printf("przedzial %d %d -- %d\n", p, k, MY_ID);
	if (k > n || MY_ID == N_THREAD -1) k = n + 1;
	ile = k - p;
	n_pipes = ile;
	start_pipes = p;
	for (int i = 0; i < ile; i ++) {
		pipes[i] = HoleDiameter(n - p - i + 1);
	}
	for (int i = ile - 2; i >= 0; i --) {
		pipes[i] = min(pipes[i + 1], pipes[i]);
	}
	long long ans = pipes[0];
	if (MY_ID < N_THREAD - 1) {
		Receive(MY_ID + 1);
		for (int i = MY_ID + 1; i < N_THREAD; i ++) {
			min_pipes[i] = GetLL(MY_ID + 1);
			ans = min(ans, min_pipes[i]);
		}
	}
	
	min_pipes[MY_ID] = ans;
	if (MY_ID > 0) {
		for (int i = MY_ID; i < N_THREAD; i ++) {
			PutLL(MY_ID - 1, min_pipes[i]);
		}
		Send(MY_ID - 1);
	}
	//przyjmowanie informacji
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		for (int i = 0; i < N_THREAD; i ++) {
			min_pipes[i] = GetLL(MY_ID - 1);
		}
	}
	//wysyłanie informacji dalej
	if (MY_ID < N_THREAD - 1) {
		for (int i = 0; i < N_THREAD; i ++) {
			PutLL(MY_ID + 1, min_pipes[i]);
		}
		Send(MY_ID + 1);
	}
	
	if (MY_ID < N_THREAD - 1) {
		for (int i = 0; i < ile; i ++) {
			pipes[i] = min(pipes[i], min_pipes[MY_ID + 1]);
		}
	}
	/*
	//TESTOWANIE
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		GetInt(MY_ID - 1);
	} else printf("Pipes: ");
	for (int i = 0; i < ile; i ++) {
 		printf("%lld ", pipes[i]);
	}
		fflush(stdout);
	if (MY_ID < N_THREAD - 1) {
		PutInt(MY_ID + 1, -99);
// 		printf("wyślij pozwolenie %d\n", MY_ID);
		Send(MY_ID + 1);
	} else {
		printf("\n");
	}
	*/
}

void readDiscs() {
	int N_THREAD = NumberOfNodes();
	int MY_ID = MyNodeId();
	int n = NumberOfDiscs();
	int ile = max(NumberOfDiscs(), PipeHeight())/N_THREAD;
	int p = ile * MY_ID + 1;
	int k = ile * (MY_ID + 1) + 1;
	if (p > n) p = n + 1;
	if (k > n || MY_ID == N_THREAD -1) k = n + 1;
	ile = k - p;
	n_discs = ile;
// 	printf("watek %d ma %d dyskow\n", MY_ID, n_discs);
	start_discs = p;
	for (int i = 0; i < ile; i ++) {
		discs[i] = DiscDiameter(p + i);
	}
	for (int i = 1; i < ile; i ++) {
		discs[i] = max(discs[i - 1], discs[i]);
	}
	long long ans = discs[ile - 1];
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		for (int i = 0; i < MY_ID; i ++) {
	// 		printf("dostaliśmy %d\n", MY_ID);
			max_discs[i] = GetLL(MY_ID - 1);
			p_discs[i] = GetLL(MY_ID - 1);
			ans = max(ans, max_discs[i]);
		}
	}
// 	printf("wysylamy %d\n", MY_ID);
	max_discs[MY_ID] = ans;
	p_discs[MY_ID] = start_discs;
	if (MY_ID < N_THREAD - 1) {
		for (int i = 0; i <= MY_ID; i ++) {
			PutLL(MY_ID + 1, max_discs[i]);
			PutLL(MY_ID + 1, p_discs[i]);
		}
		Send(MY_ID + 1);
	}
	
	//przyjmowanie informacji
	if (MY_ID < N_THREAD - 1) {
		Receive(MY_ID + 1);
		for (int i = 0; i < N_THREAD; i ++) {
			max_discs[i] = GetLL(MY_ID + 1);
			p_discs[i] = GetLL(MY_ID + 1);
		}
	}
	//wysyłanie informacji dalej
	if (MY_ID > 0) {
		for (int i = 0; i < N_THREAD; i ++) {
			PutLL(MY_ID - 1, max_discs[i]);
			PutLL(MY_ID - 1, p_discs[i]);
		}
		Send(MY_ID - 1);
	}
	
// 	assert(p_discs[MY_ID] == start_discs);
	
	if (MY_ID > 0) {
		for (int i = 0; i < ile; i ++) {
//			printf("wynik dla %d -- %lld\n", MY_ID, max_discs[MY_ID - 1]);
			discs[i] = max(discs[i], max_discs[MY_ID - 1]);
		}
	}
	/*
	//TESTOWANIE
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		GetInt(MY_ID - 1);
	}
	for (int i = 0; i < ile; i ++) {
 		printf("%lld ", discs[i]); 
	}
	fflush(stdout);
	if (MY_ID < N_THREAD - 1) {
		PutInt(MY_ID + 1, 0);
		Send(MY_ID + 1);
	} else {
		printf("\n");
	}
	*/
}

long long positions[1000];
long long received_positions[1000];

long long my_start_position = 1;
long long my_end_position;


void computeStartingPositions() {
	int N_THREAD = NumberOfNodes();
	int MY_ID = MyNodeId();
	int n = NumberOfDiscs();
	
	my_end_position = n + 1;
	
	for (int i = 0; i < N_THREAD; i ++) {
		positions[i] = n + 1;
	}
	int wsk = MY_ID + 1;
	//policz to, co masz policzyć, na razie nie wiem co
	for (int i = 0; i < n_discs; i ++) {
		if (discs[i] <= min_pipes[wsk]) {
			//mieszczą się we wsk - 1
		}
		while(wsk < N_THREAD && min_pipes[wsk] < discs[i]) {
//   			printf("wsk %d -- min_pipes %lld -- discs %lld, id %d\n", wsk, min_pipes[wsk], discs[i], MY_ID);
// 			printf("pos %d -- %d\n", wsk, i +start_discs);
			positions[wsk] = i + start_discs;
			wsk ++;
		}
	}
	
	for (int i = wsk; i < N_THREAD; i ++) {
		positions[i] = n_discs + start_discs;
	}
	
	//odbierz wiadomości
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		for (int i = MY_ID; i < N_THREAD; i++) {
			received_positions[i] = GetLL(MY_ID - 1);
		}
		for (int i = MY_ID; i < N_THREAD; i ++) {
			if (i == MY_ID || received_positions[i] != start_discs) {
// 				printf("wstaw pozycję %lld na %d, id %d\n", received_positions[i], i, MY_ID);
				positions[i] = received_positions[i];
			}
		}
		my_start_position = received_positions[MY_ID];
	}
	//ustaw pozycję następnego
// 	printf("ustaw %d na %d -- %lld\n", MY_ID + 1, my_start_position + n_pipes);
	if (my_start_position + n_pipes < positions[MY_ID + 1] ) {
		positions[MY_ID + 1] = my_start_position + n_pipes;
	}
	//wyślij wiadomości
	if (MY_ID < N_THREAD - 1) {
		my_end_position = positions[MY_ID + 1];
		for (int i = MY_ID + 1; i < N_THREAD; i ++) {
			PutLL(MY_ID + 1, positions[i]);
		}
		Send(MY_ID + 1);
	}
	
	/**
	 * ASSERTY
	long long starty[1000];
	if (MY_ID > 0) {
		PutLL(0, my_start_position);
		Send(0);
	} else {
		starty[0] = my_start_position;
		for (int i = 1; i < N_THREAD; i ++) {
			Receive(i);
			starty[i] = GetLL(i);
		}
	}
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		for (int i = 0; i <N_THREAD; i ++) {
			starty[i] = GetLL(MY_ID - 1);
		}
	}
	for (int i = 0; i < N_THREAD; i++) {
		if (starty[i] > start_discs && starty[i] <= start_discs + n_discs) {
			assert(discs[starty[i] - start_discs - 1] <= min_pipes[i]);
		}
	}
	if (MY_ID < N_THREAD - 1) {
		for (int i = 0; i < N_THREAD; i ++) {
			PutLL(MY_ID + 1, starty[i]);
		}
		Send(MY_ID + 1);
	}
	
	/*
	 * 
	 ********/
	
	
	/*
	//TESTOWANIE
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		GetInt(MY_ID - 1);
	}
//	printf("%lld ", my_start_position);
	if (MY_ID < N_THREAD - 1) {
		PutInt(MY_ID + 1, 0);
		Send(MY_ID + 1);
	}
	*/
}

long long szukajWielkosc() {
	int N_THREAD = NumberOfNodes();
	int MY_ID = MyNodeId();
	int szukam = 0;
	long long war = 1;
	for (int i = 0; i < N_THREAD; i ++) {
		if (p_discs[i] <= my_start_position) {
			szukam = i;
			war = p_discs[i];
		}
	}
	long long w = 0;
	if (szukam > 0) w = max_discs[szukam - 1];
	for (long long i = war; i < my_start_position; i ++) {
		w = max(w, DiscDiameter(i));
	}
	return w;
}

void computeResult() {
	int N_THREAD = NumberOfNodes();
	int MY_ID = MyNodeId();
	long long udalo_sie = 0;
	long long nie_udalo_sie = 0;
	long long w = szukajWielkosc();
	long long last = -1;
	long long puste = 0;
	int pos = 0;
//   	printf("watek %d -- %lld %lld wwww %lld, pipes %lld\n", MY_ID, my_start_position, my_end_position, w, n_pipes);
	for (int i = my_start_position; i < my_end_position; i ++) {
// 		printf("loop %d\n", MY_ID);
		long long k = DiscDiameter(i);
		w = max(w, k);
		while(pos < n_pipes && pipes[pos] < w) {
			puste ++;
			pos ++;
		}
		if (pos < n_pipes) {
			udalo_sie ++;
			last = pos + start_pipes;
			pos ++;
		}
		else nie_udalo_sie++;
	}
	long long koniec = n_pipes - pos;
//    	printf("watek %d -- udalo sie %lld %lld last %d\n", MY_ID, udalo_sie, puste, last);
	
	if (MY_ID > 0) {
		Receive(MY_ID - 1);
		udalo_sie += GetLL(MY_ID - 1);
		long long zostalo = GetLL(MY_ID - 1);
//  		printf("anal %d zostalo %lld puste %lld\n", MY_ID, zostalo, puste);
		udalo_sie += min(zostalo, puste);
		zostalo -= min(zostalo, puste);
		if (min(zostalo, koniec) > 0) {
//  			printf("dumaj %d %d %d %lld\n", last, start_pipes, pos, min(zostalo, koniec));
			if (last == -1) last = start_pipes + pos + min(zostalo, koniec) - 1;
			else 
				last += min(zostalo, koniec);
//  			printf("podumalem %d\n", last);
			udalo_sie += min(zostalo, koniec);
			zostalo -= min(zostalo, koniec);
		}
		nie_udalo_sie += zostalo;
		if (last != -1) {
			last = PipeHeight() - last + 1;
		}
		long long received_last = GetLL(MY_ID - 1);
		if (udalo_sie == NumberOfDiscs()) {
			if (received_last != -1) {
				last = received_last;
			}
		} else {
			last = -1;
		}
	}
	else {
		if (udalo_sie != NumberOfDiscs()) last = -1;
		if (last != -1) {
			last = PipeHeight() - last + 1;
		}
	}
	
//   	printf("watek %d -- udalo sie ostatecznie %lld %d\n", MY_ID, udalo_sie, last);
	if (MY_ID < N_THREAD - 1) {
		PutLL(MY_ID + 1, udalo_sie);
		PutLL(MY_ID + 1, nie_udalo_sie);
		if (udalo_sie != NumberOfDiscs()) {
			last = -1;
		}
		PutLL(MY_ID + 1, last);
		Send(MY_ID + 1);
	} else {
		if (last == -1) last = 0;
		printf("%lld\n", last);
//  		assert(last == wzorcowka());
	}
}

int main() {
	readPipes();
	readDiscs();
	computeStartingPositions();
	computeResult();
}