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

typedef long long ll;

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

using namespace std;

const int maxh = 1000005;

int N;
int nodes;
int id;

ll fen_tree[maxh];
int cnt[maxh];

ll fen_read(int idx)
{
	ll sum = 0;
	while(idx > 0)
	{
		sum += fen_tree[idx];
		idx -= (idx & -idx);
	}
	return sum;
}

void fen_update(int idx, ll val)
{
	while(idx < maxh)
	{
		fen_tree[idx] += val;
		idx += (idx & -idx);
	}
}

ll solve(int start, int end)
{
	ll ans = 0;
	for(int i = end; i >= start; i--)
	{
		int height = GetElement(i);
		cnt[height]++;
		ll x = fen_read(height - 1);
		ans += x;
		fen_update(height, 1);
	}
	return ans;
}

int main()
{
	N = GetN();
	nodes = NumberOfNodes();
	id = MyNodeId();
	
	if(N <= 1000000)
	{
		if(id == 0)
			cout << solve(0, N - 1) << "\n";
		return 0;
	}
	
	if(id == 0)
	{
		ll sum = 0;
		for(int i = 1; i < nodes; i++)
		{
			Receive(i);
			ll psum = GetLL(i);
			for(int j = 1; j <= 5; j++)
			{
				ll il = GetLL(i);
				for(int k = j + 1; k <= 5; k++)
					sum += il * cnt[k];
				cnt[j] += il;
			}
			sum += psum;
		}
		
		cout << sum << "\n";
	}
	else
	{
		int len = (N - 1) / (nodes - 1) + 1;
		int start = (id - 1) * len;
		int end = min(N - 1, start + len - 1);
		
		ll res = solve(start, end);
		PutLL(0, res);
		
		for(int i = 1; i <= 5; i++)
			PutLL(0, cnt[i]);
		
		Send(0);
	}
	
	return 0;
}