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

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;

const int INF = 1000000009;
const LL LINF = 1000000000000000009LL;

#define FOR(i, b, e) for(int i = b; i <= e; ++i) 
#define FORD(i, b, e) for(int i = b; i >= e; --i) 
#define REP(i, n) FOR(i, 0, n-1)
#define REV(i, n) FORD(i, n-1, 0)
#define PB push_back
#define PP pop_back
#define MP make_pair
#define ST first
#define ND second
#define SIZE(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()

#define DEBUG(s) s

const int N = 1000000+5;

//~ int rmc[] = {5, 2, 4, 4, 3};

//~ int GetN() { return 1000;}
//~ int GetElement(int i) {return (1000-i)%10000+1;}

//~ int GetN() { return 100000000;}
//~ int GetElement(int i) {return (100000000-i)%1000000+1;}

//~ int GetN() { return int(1e8); }
//~ int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; }

//~ int GetN() { return 100 * 1000 * 1000; }
//~ int GetElement(int i) {return 1 + (((LL)i * 27) + i & (i %113) + ((i % 37) ^ (i % 20)) * 3571) % 1000000;}

LL n, id, k, sq;

LL nr, pos;

bool init()
{
	k = NumberOfNodes();
	n = GetN();
	k = min(n, k);
	id = MyNodeId();
	
	nr = 1;
	int sum = 0;
	
	while(sum <= k)
	{
		sum+=nr;
		nr++;
		//~ cout<<id;
		//~ cout<<sum<<"\n";
	}
	
	sq = nr-2;
	k = sq*(sq+1ll)/2ll;
	
	nr = 1;
	sum = 0;
	
	while(sum <= id)
	{
		sum+=nr;
		nr++;
		//~ cout<<id;
		//~ cout<<sum<<"\n";
	}
	nr = nr-2;
	pos = id - sum+nr+1;
	//~ cout<<id<<" "<<nr<<" "<<pos<<"\n";
	//~ cout<<"\n";
	return nr<sq;
	
	
}


LL res;
LL tree[N];
int zlicz[N];

int M = 1000000;

inline int pot(int x)
{
	return x & (-x);
}

inline LL beforeeq(int x)
{
	LL sum = 0;
	while(x > 0)
	{
		sum += tree[x];
		x -= pot(x);
	}
	
	return sum;
}

inline void update(int x, int v)
{
	while(x <= M)
	{
		tree[x] += v;
		x += pot(x);
	}
}



int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	bool rmc = init();
	if(!rmc) return 0;
	
	//~ cout<<id<<" "<<nr<<" "<<pos<<"\n";
	
	LL poc = n*nr/sq, kon = n*(nr+1ll)/sq-1ll;
	
	if(pos == nr)
	{
		FOR(i, poc, kon)
		{
			LL tabi = GetElement(i);
			LL tmp = i-poc - beforeeq(tabi);
			res+=tmp;
			update(tabi, 1);
		}
	}
	else
	{
		
		LL poc0 = n*pos/sq, kon0 = n*(pos+1ll)/sq-1ll;
		//~ cout<<id<<" "<<poc0<<" "<<kon0<<" "<<poc<<" "<<kon<<"\n";
		FOR(i, poc0, kon0)
		{
			LL tabi = GetElement(i);
			update(tabi, 1);
		}
		
		FOR(i, poc, kon)
		{
			LL tabi = GetElement(i);
			LL tmp = (kon0-poc0+1) - beforeeq(tabi);
			res+=tmp;
		}
	}
	
	/*******KOMUNIKACJA***********/
	
	if(id==0)
	{
		REP(i, k-1)
		{
			int instancja = Receive(-1);
			res+=GetLL(instancja);
		}
	}
	else
	{
		PutLL(0, res);
		Send(0);
	}
	
	if(id == 0)
	cout<<res<<"\n";
	
	return 0;
}