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
// Jenda instancja wypisuje (n * k) mod p.

#include "bits/stdc++.h"
#include "message.h"
#include "futbol.h"
//int GetN() { return 997365040; }
//int GetK() { return 496917823; }
//int GetP() { return 999287789; }

using namespace std;

using ll = long long;

ll fpw(ll a, ll b, ll p){
    ll r = 1;
    while(b) {
        if(b&1) r = r*a%p;
        a = a*a%p; b /= 2;
    }
    return r;
}

int main() {
  ll n = GetN(), k = GetK(), p = GetP();

  // TODO: detect n==0

  //cerr << "p:" << p << endl;

  int nodes = NumberOfNodes();
  int i = MyNodeId();
  int slice = (k + nodes - 1) / nodes;

  int start = i * slice + 1;
  int end = (i+1) * slice + 1;
  end = min(end, (int)k + 1);

  ll sum = 0;
  ll curr = 1;

  for (int j=start; j < end; j ++) {
    curr *= n - j + 1;
    curr %= p;
    curr *= fpw(j, p - 2, p);
    curr %= p;
    sum += curr;
    sum %= p;
  }

  PutLL(0, curr);
  PutLL(0, sum);
  Send(0);

  if(MyNodeId() != 0) return 0;

  //cerr << "comp" << endl;
  ll mult = 1;
  ll res = 0;

  for (int pp=0; pp < nodes; pp ++) {
    Receive(pp);
    ll res_curr = GetLL(pp);
    ll res_sum = GetLL(pp);

    res += (res_sum * mult) % p;
    res %= p;
    mult *= res_curr;
    mult %= p;
  }

  cout << (res+1)%p << endl; // <----- FIXED!
}