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
#include<cstdio>
#include<deque>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<message.h>
#include<cstdlib>
#include<cassert>
#include "kollib.h"
using namespace std;

class mydeque
{
	deque<int>   Q;
	map<int,int> dist;
	bool startAtZero;
	
	void computeDist()
	{
		int d = 0, n = Q.size();
		if (startAtZero) 
			for (int i=0; i<n; i++)    dist[ Q[i] ] = d++;
		else
			for (int i=n-1; i>=0; i--) dist[ Q[i] ] = d++;
	}
	
public:
	mydeque()
	{
		startAtZero = 0;
	}
	
	void push(const int x)
	{
		dist[x] = size();
		if (startAtZero) Q.push_back(x); else Q.push_front(x);
	}
	
	void flip() { startAtZero = !startAtZero; computeDist(); }
	
	int operator[](const int i) const
	{
		if (startAtZero) return Q[i];
		else return Q[ Q.size()-i-1 ];
	}
	
	inline bool has( const int x ) { return distance(x) != -1; }
	

	
	int size() const { return Q.size(); }
	int back() const { return (startAtZero) ? Q[ Q.size()-1 ] : Q[0]; }
	int prevback() const { return (startAtZero) ? Q[ Q.size()-2 ] : Q[1]; }
	int distance(int x)
	{
		if ( dist.find(x) != dist.end() ) return dist[x];
		return -1;
	}
};

inline int First(int i)  { return FirstNeighbor(i+1)-1; }
inline int Second(int i) { return SecondNeighbor(i+1)-1; }
inline int Next(int i, int prev)
{
	int a = First(i), b = Second(i);
	return ( b==prev ) ? a : b;
}

const int C = NumberOfNodes();
const int myid = MyNodeId();
const int N = NumberOfStudents();

mydeque Q;

enum { STOP=0, ASK, ANSWERFORCHECKING, FINISH, CHECK, CHECKING, DOJOB };

void sendAll(char message, int last = 0)
{
	for (int i=0; i<C; i++) if ( i!=last) { PutChar(i,message); Send(i); }
	PutChar(last,message); Send(last);
}

// TODO milion
#define MILION 1000000

int main()
{
// Krok 1. Sprawdzenie czy rozmiar jest na tyle maly, aby można było go rozwiązać na jednym komputerze.
	if ( myid>0  and N<=max(C,MILION) ) return 0;
	if ( myid==0 and N<=max(C,MILION) )
	{
		vector<int> tab(N), dist(N);
		tab[0] = 0; 
		tab[1] = First(0);
		for (int i=2; i<N; i++) tab[i] = Next( tab[i-1], tab[i-2] );
		for (int i=0; i<N; i++) dist[ tab[i] ] = i;
		
		int nr = NumberOfQueries();
		for (int i=1; i<=nr; i++)
		{
			int a = QueryFrom(i)-1;
			int b = QueryTo(i)-1;
			printf("%d\n", min( (dist[a] - dist[b] + N)%N,  (dist[b] - dist[a] + N)%N ));
		}
		return 0;
	}

	int s;
/* Krok 2. (komputer 0) Wylosuj C liczb: x_0, x_1, ..., x_{C-1} */
	if (myid==0)
	{
		//fprintf(stderr, "Number of nodes = %d\n", C);
		srand( 8*C ); // todo 6*C
		vector<int> x(C);
		for (int i=0; i<C; i++)
		{
			while(true)
			{
				x[i] = rand()%N;
				bool res=false;
				for (int j=0; j<i; j++) if ( x[j]==x[i] ) { res=true; break; }
				if ( !res ) break;
			}
		}
		//fprintf(stderr, "x=["); for (auto i : x) fprintf(stderr, "%d,", i); fprintf(stderr, "]\n");
		s = x[0];
		for (int i=1; i<C; i++) { PutInt(i, x[i]); Send(i); }
	}
	else
	{
		Receive(0);
		s = GetInt(0);
	}
	
/* Krok 3 */
	//fprintf(stderr, "[%2d] : s=%d\n", myid, s);
	{
		int x = s, y = rand()%2 ? First(x) : Second(x);
		Q.push(x);
		Q.push(y);
		int n = min( 2*N/C, 5*MILION ), z;
		while (n--)
		{
			z = Next(y, x); x = y; y = z; Q.push(y);
			//fprintf(stderr, "[%2d] : Q.push(%d)   Q[%d]=%d\n", myid, y,  Q.size()-1, Q.back());
		}
	}					
	
	bool found = false;
	int where = -1, size = -1;
	long long offset = 0;
	int counter, finishedComputers;
	
	finishedComputers = 0;
	
	counter = 1;
	for (int k=0; k<C; k++) if (k!=myid) { PutChar(k,ASK); PutInt(k, Q.back()); Send(k); }
	
	while(true)
	{
		int komp = Receive(-1);
		int message = GetChar(komp);
		//fprintf(stderr, "[%2d] : message %d from %d\n", myid, message, komp);
		if (message==STOP) break;
		switch( message )
		{
			case ASK:
			{
				int y = GetInt(komp);
				PutChar(komp, ANSWERFORCHECKING);
				PutChar(komp, (char) Q.has(y));
				Send(komp);
				break;
			}
			case ANSWERFORCHECKING:
			{
				counter++;
				char answer = GetChar(komp);
				if ( answer == true ) { found = true; where = komp; }
				
				if ( counter==C )
				{ 
					if ( !found )
					{ 
						int x = Q.prevback(), y = Q.back();
						int n = Q.size(), z;
						if ( Q.size() > 8000000 ) n = 5000000;
						while (n--)
						{
							z = Next(y, x); x = y; y = z;
							Q.push(y);
							//fprintf(stderr, "[%2d] : Q.push(%d)   Q[%d]=%d\n", myid, y,  Q.size()-1, Q.back());
						}
						counter = 1;
						for (int k=0; k<C; k++) if (k!=myid) { PutChar(k,ASK); PutInt(k, Q.back()); Send(k); }
					}
					else
					{
						// Q.computeDist();
						//fprintf(stderr, "[%2d] : Finishing ... ( Q[0]=%d, ..., Q[%d]=%d which is in comp %d\n", myid, Q[0], Q.size()-1, Q.back(), where );
						PutChar(1, FINISH); Send(1);
					}
				}
				break;
			}
			case FINISH:
			{
				assert(myid==1);
				//fprintf( stderr, "%d has finished\n", komp);
				finishedComputers++;
				if ( finishedComputers==C ) sendAll(STOP, 1);
				break;
			}
		}
	}
	
	//if ( myid==0 ) fprintf(stderr, "KROK 4\n");

/* Krok 4  */
	if ( myid==0 )
	{
		offset = 0; 
		PutChar(0, DOJOB); PutInt(0,0); PutInt(0,where); PutLL(0, offset);
		Send(0);
	}
	bool cycle = false, checked = false;
	int cntChecks = 0;
	while(true)
	{
		int komp = Receive(-1);
		int message = GetChar(komp);
		//fprintf(stderr, "[%2d] : message %d from %d\n", myid, message, komp);
		if (message==STOP) break;
		
		switch(message)
		{
			case DOJOB:
			{
				assert(myid==0);
				int a = GetInt(komp);
				int b = GetInt(komp);
				long long off = GetLL(komp);
				//foundWhere[a] = b;
				PutChar(a,CHECK); Send(a);
				break;
			}	
			case CHECK:
			{
				// myid to komputer, w którym jest prawidłowy kierunek w kolejce Q.
				cntChecks++;
				checked = true;
				int b = where;
				if ( b==0 || cntChecks==2 ) // jeśli jesteś ostatnim komputerem, tj. takim, który widzi kolejkę komputera 0, to zakończ
					sendAll(STOP, 0);
				else
				{
					// wysyłamy do komputera b informację, aby sprawdził sobie, czy ma dobrą kolejność w kolejce Q
					PutChar(b, CHECKING); PutInt(b, Q.prevback()); PutInt(b, Q.back()); PutLL(b, offset + Q.size() - 1);
					Send(b);
				}
				break;
			}	
			case CHECKING:
			{
				// sprawdzamy czy myid ma dobrą kolejność
				int a = komp;
				int b = myid;
				int ax  = GetInt(a);
				int ay  = GetInt(a);
				long long dy = GetLL(a);
				//fprintf(stderr, "[%2d] : Sprawdzam czy mam dobrą kolejność (x=%d,y=%d)\n", myid, ax,ay);
				//fprintf(stderr, "[%2d] : Q = ", myid); for (int i=0; i<Q.size(); i++) fprintf(stderr, "%d,", Q[i]); fprintf(stderr, "\n");
				
				bool result = false, force = false;
				if ( Q[0]==ay ) result = ( Q[1] != ax );
				else if ( Q.back()==ay ) result = ( Q.prevback() == ax );
				else result = Q.distance(ax) < Q.distance(ay);
				
				if (result==false || Q.back()==ay)
				{
					if ( result==false )
					{
						//fprintf(stderr, "[%2d] : Flipping Q.\n", myid);
						Q.flip();
					}
					
					force = false;
					if ( Q.back()==ay ) force = true;
					
					found = false;
					while (!found)
					{
						int x = Q.prevback(), y = Q.back();
						if ( !force )
						{
							for (int k=0; k<C; k++) if (k!=myid) { PutChar(k,ASK); PutInt(k, y); Send(k); }
							for (int k=0; k<C; k++) if (k!=myid) { int kk = Receive(-1); int ans = GetInt(kk); if ( ans  ) { found = true; where = kk; } }
						}
						
						if (!found)
						{
							int n = Q.size(), z;
							while (n--)
							{
								z = Next(y, x); x = y; y = z;
								Q.push(y);
							}
							force = false;
						}
					}
					//Q.computeDist();
				}
				//Q.computeDist(true);
				long long mdy = Q.distance( ay );    //  solve(   offset + mdy = dy,   offset );
				offset = dy - mdy;
				PutChar(0, DOJOB); PutInt(0, b); PutInt(0, where); PutLL(0, offset); Send(0);
				break;
			}	
			case ASK:
			{
				int y = GetInt(komp);
				PutInt(komp, (int) Q.has(y));
				Send(komp);
				break;
			}
		}
	} 
	
	if ( checked ) {
		//Q.computeDist(true); 
		while ( offset < 0 ) offset += N; offset %= N;
	}
	
	//if ( myid==0 ) fprintf(stderr, "KROK 5\n");
/* Krok 5 */

	//fprintf(stderr, "[%2d] : %s Q = [%2lld]=%2d,%2d,...,%2d (size=%d)  -> %d\n", myid, (checked) ? "" : "(not)", offset, Q[0], Q[1], Q.back(), Q.size(), where); 

	if ( myid == 2 )
	{
		map<int, int> dist;
		
		int nr = NumberOfQueries();
		for (int query=1; query<=nr; query++)
		{
			int a = QueryFrom(query)-1, b = QueryTo(query)-1;
			if ( !checked || !Q.has(a) ) dist[a] = N;
			else
			{
				long long da = offset + Q.distance(a);
				while ( da < 0 ) da += N;
				da %= N;
				dist[a] =  da;
			}
			
			a = b;
			if ( !checked || !Q.has(a) ) dist[a] = N;
			else
			{
				long long da = offset + Q.distance(a);
				while ( da < 0 ) da += N;
				da %= N;
				dist[a] =  da;
			}
		}
		// wait for replies	
		for (int k = 0; k<C; k++) if ( k != 2 )
		{
			Receive(k);
			for (int query=1; query<=nr; query++)
			{
				int a = QueryFrom(query)-1, b = QueryTo(query)-1;
				dist[a] = min( dist[a], GetInt(k) );
				dist[b] = min( dist[b], GetInt(k) );
			}	
		}
		
		for (int query=1; query<=nr; query++)
		{
			int a = QueryFrom(query)-1, b = QueryTo(query)-1;				
			int da = dist[a], db = dist[b];
			if ( da == N ) da = rand()%N;
			if ( db == N ) db = rand()%N;
			printf("%d\n", (int) min( (da-db+N)%N, (db-da+N)%N));
		}
	}
	else
	{
		int nr = NumberOfQueries();
		for (int query=1; query<=nr; query++)
		{
			int a = QueryFrom(query)-1, b = QueryTo(query)-1;
			
			if ( !checked || !Q.has(a) ) PutInt(2, N);
			else
			{
				long long da = offset + Q.distance(a);
				while ( da < 0 ) da += N;
				da %= N;
				PutInt( 2, (int) da );
			}
			
			a = b;
			if ( !checked || !Q.has(a) ) PutInt(2, N);
			else
			{
				long long da = offset + Q.distance(a);
				while ( da < 0 ) da += N;
				da %= N;
				PutInt( 2, (int) da );
			}
		}
		Send(2);
	}
	return 0;
}