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

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()

using namespace std;

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

const int MX = (int)1e6 + 5, P2 = (1 << 20), MTS = 2 * P2 + 5, inf = 1000000005, mod = 1000000007;
const LL INF = 1000000000000000005LL;

int T[MTS];
int ile[MX];

// UWAGA, to trzeba wypierdolic przed submitem!!!

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



//UWAGA, to trzeba wypierdolic przed submitem!!!

int N;

LL solve(int l, int p, int k)
{
	LL ret = 0LL;
	int a = l * p;
	int b = min(a + l - 1, N - 1);
	int c = l * k;
	int d = min(c + l - 1, N - 1);
	//printf("%d %d %d %d %d %d %d\n", l, p, k, a, b, c, d);
	for(int i = a; i <= b; ++i)
		ile[GetElement(i)]++;
	for(int i = MX - 2; i >= 0; --i)
		ile[i] += ile[i + 1];
	for(int i = c; i <= d; ++i)
		ret += ile[GetElement(i) + 1];
	return ret;
}	

inline void add(int pos)
{
	pos += P2;
	while(pos)
	{
		T[pos]++;
		pos >>= 1;
	}
}

inline int ask(int a)
{
	int b = P2 - 5, ret = 0;
	a += P2, b += P2;
	while(a < b)
	{
		if(a & 1)
			ret += T[a++];
		if(!(b & 1))
			ret += T[b--];
		a >>= 1, b >>= 1;
	}
	if(a == b)
		ret += T[a];
	return ret;
}

LL solve2(int l, int p)
{
	LL ret = 0LL;
	int a = l * p;
	int b = min(a + l - 1, N - 1);
	//printf("%d %d %d %d\n", l, p, a, b);
	int cur;
	for(int i = a; i <= b; ++i)
	{
		cur = GetElement(i);
		ret += ask(cur + 1);
		add(cur);
	}
	return ret;
}

int main()
{
	int S = NumberOfNodes();
	int sq = sqrt(S);
	//printf("%d\n", sq);
	N = GetN();
	int len = ((N + sq - 1) / sq);
	//printf("%d %d\n", N, len);
	int numerek = MyNodeId();
	int pocz = numerek / sq;
	int kon = numerek % sq;
	if(pocz > kon)
	{
		PutLL(0, 0LL);
		Send(0);
		return 0;
	}
	LL res;
	if(pocz == kon)
		res = solve2(len, pocz);
	else
		res = solve(len, pocz, kon);
	if(numerek == 0)
	{
		for(int i = 1; i < S; ++i)
		{
			Receive(i);
			res += GetLL(i);
		}
		printf("%lld", res);
	}
	else
	{
		PutLL(0, res);
		Send(0);
	}
}