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
#include <iostream>
#include "message.h"
#include "futbol.h"
using namespace std;

int Divider( int a, int b )
{
    // kod tej funkcji został skopiowany z tej strony:
    // https://eduinf.waw.pl/inf/alg/001_search/0009.php
    int u,w,x,z,q;

    u = 1; w = a;
    x = 0; z = b;
    while(w)
    {
        if(w < z)
        {
            q = u; u = x; x = q;
            q = w; w = z; z = q;
        }
        q = w / z;
        u -= q * x;
        w -= q * z;
    }
    if ( x < 0 ) x += b;
    return x;
}

int main()
{
    int id = MyNodeId(), n = GetN(), k = GetK(), p = GetP();
    int start = id * (k/100), meta = (id+1) * (k/100);
    int last = 1, sum = 0;
    int a, b;
    
    if ( n == 0 )
    {
        if ( id == 99 ) cout << 7 << endl;
        return 0;
    }

    long long one = 1;

    if ( MyNodeId() == 99 ) meta = k+1;

    if ( start == 0 && start != meta ) sum = start = 1;

    for ( int i = start; i < meta; ++i )
    {
        last = ( one * last * ( n - i + 1 ) ) % p;
        last = ( one * last * Divider( i, p ) ) % p;
        sum = ( sum + last ) % p;
    }

    if ( id == 0 )
    {
        PutInt( id+1, sum );
        PutInt( id+1, last );
        Send( id+1 );
    }
    else if ( id != 99 )
    {
        Receive( id-1 );
        a = GetInt( id-1 );
        b = GetInt( id-1 );

        sum = ( one * sum * b ) % p;
        sum = ( sum + a ) % p;
        last = ( one * last * b ) % p;

        PutInt( id+1, sum );
        PutInt( id+1, last );
        Send( id+1 );
    }
    else
    {
        Receive( id-1 );
        a = GetInt( id-1 );
        b = GetInt( id-1 );

        sum = ( one * sum * b ) % p;
        sum = ( sum + a ) % p;
        cout << sum << endl;
    }
}