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

typedef long long int lli;

// int GetN() { return 100 * 1000 * 1000; }
// int GetElement(int i) {
// if (i < 100 * 1000 * 1000 - 3) return 1 + i / 100;
// else return 100 * 1000 * 1000 - i;
// }

const int MAXV = 1000005;
const int MAXD = 100000005;
const int SPL = 13;
const int INTL = MAXD/SPL+100;
const int INF = MAXV;

int cnt[MAXV];
int tsp[MAXV];
int t1[INTL];
int t2[INTL];
int l1, l2;
int numz;
int TIM = 1;

void touch(int k) {
	if (tsp[k] < TIM) {
		cnt[k] = 0;
		tsp[k] = TIM;
	}
}

int numrows;

int GetEl(int k) {
	if (k >= numrows)
		return INF;
	
	return GetElement(k);
}

void getVals(lli id, int *t, int &l) {
	int it = 0;
	int allcnt = NumberOfNodes();
	
	int st = (id*numrows)/SPL;
	int nd = ((id+1)*numrows)/SPL;
	
	for (int i=st; i<nd; i++) {
		t[it] = GetEl(i);
		it++;
	}
	
	l = it;
}

int main() {
	int num = MyNodeId();
	numrows = GetN();
	
// #ifdef LOCAL
// 	assert(NumberOfNodes() == 100);
// #endif
	
	int it = 0;
	int i1 = -1, i2 = -1;
	
	for (int i=0; i<SPL; i++) {
		for (int j=0; j<=i; j++) {
			if (it == num) {
				i1 = i;
				i2 = j;
				break;
			}
			
			it++;
		}
		
		if (i1 != -1)
			break;
	}
	
	if (i1 == -1) {
		PutLL(0, 0);
		Send(0);
		return 0;
	}
	
#ifdef LOCAL
	assert(i2 <= i1);
#endif
	
	getVals(i2, t1, l1);
	getVals(i1, t2, l2);
	numz = 0;
	lli res = 0;
	
	if (i1 == i2) {
		while (2) {
			TIM++;
// 			if (num == 0) {
// 				printf("%lld\n", res);
// 				for (int i=0; i<10; i++)
// 					printf("%d ", t1[i]);
// 				printf("\n");
// 			}
			
			if (numz == l1)
				break;
			numz = 0;
			
			for (int i=0; i<l1; i++) {
				touch(t1[i]);
				if (t1[i]%2 == 0) {
					touch(t1[i]+1);
					res += cnt[t1[i]+1];
				}
				cnt[t1[i]]++;
				t1[i] /= 2;
				if (t1[i] == 0)
					numz++;
			}
		}
	}
	else {
		while (2) {
			TIM++;
			if (numz == l1+l2)
				break;
			numz = 0;
			
			for (int i=0; i<l1; i++) {
				touch(t1[i]);
				cnt[t1[i]]++;
				t1[i] /= 2;
				if (t1[i] == 0)
					numz++;
			}
			
			for (int i=0; i<l2; i++) {
				if (t2[i]%2 == 0) {
					touch(t2[i]+1);
					res += cnt[t2[i]+1];
				}
				t2[i] /= 2;
				if (t2[i] == 0)
					numz++;
			}
		}
	}
	
	if (num != 0) {
		PutLL(0, res);
		Send(0);
		return 0;
	}
	
	// num == 0
	
	int nos = NumberOfNodes();
	
	for (int i=1; i<nos; i++) {
		int fr = Receive(-1);
		res += GetLL(fr);
	}
	
	printf("%lld\n", res);
	
	return 0;
}