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
#include<bits/stdc++.h>
#include "message.h"
#include "futbol.h"
#define ll long long
#define pb push_back
#define MP make_pair

using namespace std;

void EE(ll a, ll b, ll& x, ll& y)
{
    if(a%b == 0)
    {
        x=0;
        y=1;
        return;
    }
    EE(b,a%b,x,y);
    ll temp = x;
    x = y;
    y = temp - y*(a/b);
}

ll inverse(ll a, ll m)
{
    ll x,y;
    EE(a,m,x,y);
    if(x<0) x += m;
    return x % m;
}

//ll GetN(){return 1e9;}
//ll GetK(){return 1e9;}
//ll GetP(){return 1e9+7;}

int main()
{
    ll N = GetN(), K = GetK(), P = GetP();
    int v = MyNodeId();
    ll a = v * K / 100 + 1, b = (v + 1) * K / 100;
    ll last = 1, sum = 0;
	//if(a <= b) cout << "Licze na przedziale [" << a << ", " << b << "]" << endl;
    for(ll i = a; i <= b; i++){
        last = (last * (N - i + 1)) % P;
		//cout << "last=" << last << endl;
        last = (last * inverse(i, P)) % P;
        sum += last;
        sum %= P;
    }
    if(v != 0){
        int next = (v + 1) % 100, before = (v + 99) % 100;
        Receive(before);
        ll before_last = GetLL(before);
        ll sum_last = GetLL(before);
        last = (last * before_last) % P;
        sum = (before_last * sum + sum_last) % P;
        PutLL(next, last);
        PutLL(next, sum);
        Send(next);
    }
    else{
        PutLL(1, last);
        PutLL(1, sum);
        Send(1);
        Receive(99);
        GetLL(99);
        cout << GetLL(99) + 1;
    }
	return 0;
}