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
#include <bits/stdc++.h>
#include "message.h"
#include "teatr.h"
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define scanf(...) scanf(__VA_ARGS__)?:0
#define FOR(i, a, b) for (int i=(a); i<=(b); ++i)
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, PLL> PP;
typedef unsigned int ui;
const int mod = 1e9+696969;
const int inf = 1e9+9;
const ll INF = 1e18;

const int INST = 100;
const int LEN = 1000000;
const int maxn = 1000100;
int dr[maxn];

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

inline void add(int p) {
	for (; p < maxn; p += p & (-p)) dr[p]++;
}
inline int query(int p) {
	ll ret = 0;
	for (; p > 0; p -= p & (-p)) ret += dr[p];
	return ret;
}

int pref[LEN + 5];
int addon[LEN + 5];

int main()
{
	ll me = MyNodeId(), n = GetN();
	ll result = 0;
	
	ll pie = me * LEN;
	ll ost = min(n - 1, (me + 1) * LEN - 1);
	for (int i = pie; i <= ost; ++i) {
		int val = a(i);
		result += query(val);
		add(val);
	}
	
	if (me != 0) {
		PutLL(0, result);
		Send(0);
	}
	
	if (me == 0) {
		for (int i = 1; i < INST; ++i) {
			Receive(i);
			result += GetLL(i);
		}
		
		//czas pobawic sie z jedna instancja
		int ch = LEN;
		int buflen = 0;
		for (int i = 0; i < n; ++i) {
			if (ch == i) {
				ll delta = 0;
				FOR(j, 1, LEN + 2) {
					delta += addon[j];
					addon[j] = 0;
					pref[j] += delta;
				}
				
				ch += LEN;
			}
			
			int val = a(i);
			addon[val]++;
			result += pref[val];
		}
		cout << (ll)n * (ll)(n - 1)/2LL - result;
		return 0;
	}
}