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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Jenda instancja wypisuje (n * k) mod p.

#include "message.h"
#include "futbol.h"
#include "bits/stdc++.h"
#include <iostream>
#include <iomanip> 
#include <algorithm> 

using namespace std;


// int GetN() { return 997365040;}
// int GetK() { return 496917823;}
// int GetP() { return 999287789;}





void xgcd(long long *result, long long a, long long b){
  long long aa[2] = {1, 0}, bb[2] = {0,1}, q, r;
  while(1) {
    q = a / b; a = a % b;
    aa[0] = aa[0] - q*aa[1];  bb[0] = bb[0] - q*bb[1];
    if (a == 0) {
        result[0] = b; result[1] = aa[1]; result[2] = bb[1];
        return;
    };
    q = b / a; b = b % a;
    aa[1] = aa[1] - q*aa[0];  bb[1] = bb[1] - q*bb[0];
    if (b == 0) {
        result[0] = a; result[1] = aa[0]; result[2] = bb[0];
        return;
    };
  };
}

long long power(long long a, long long n, long long p) {
  long long k = a;
  long long res = 1;
  while(n > 0) {
    if (n&1) {
      res = (res * k) % p;
    }
    k = (k * k) % p;
    n >>= 1;
  }
  return res;
}

long long modInverse(long long a, long long p) 
{ 
    long long res[3]; 
    xgcd(res, a, p); 
    return (res[1]%p + p) % p; 
} 


int NodeNo;
int NodeCount;

void knownN() {

  int n = GetN();
  long long p = GetP();
  int k = GetK();

  bool reversed = false;
  if (k * 2 > n) {
    reversed = true;
    k = n - k - 1;
  }

  int span = (k+1 + NodeCount-1)/NodeCount;

  int A = min(NodeNo * span, k + 1);
  int B = min((NodeNo+1) * span, k + 1);

  long long binomial = 1;
  long long sum = 0;

  for (long long k1 = A; k1 < B; k1++) {

    // cerr << k1 << ") " << binomial << endl;
    sum = (sum + binomial) % p;

    binomial = (binomial * (n - k1)) % p;
    binomial = (binomial * modInverse(k1 + 1, p)) % p;
  }

  long long mult = 1;
  long long total = 0;

  if (NodeNo > 0) {
    Receive(NodeNo - 1);
    mult = GetLL(NodeNo - 1);
    total = GetLL(NodeNo - 1);
    // cerr << "Received " << mult << " " << total << endl;
  }

  binomial = (binomial * mult) % p;
  sum = (sum * mult) % p;
  
  total = (total + sum) % p;

  if (NodeNo == NodeCount - 1) {
    if (reversed) {
      total = (power(2, n, p) - total + p) % p;
    }
    cout << total << endl;
  } else {
    PutLL(NodeNo + 1, binomial);
    PutLL(NodeNo + 1, total);

    Send(NodeNo + 1);
  }
}



void unknownN() {

  long long n = GetN();
  long long p = GetP();
  long long k = GetK();
  long long psquare = p*p;


  long long span = (p - k + NodeCount-1)/NodeCount;
  
  long long A = min(k + NodeNo * span, p);
  long long B = min(k + (NodeNo+1) * span, p);

  // cerr << A << " - " << B << " > " << span << endl;

  long long binomial = 1;
  long long sum = 0;

  int *vals = new int[B-A];
  vals[0] = 1;
  for (long long n1 = max(A, k+1); n1 < B; n1++) {

    // cerr << k1 << ") " << binomial << endl;
    // cerr << n1 << " po " << k << " = " << binomial << endl;

    binomial = (binomial * (n1)) % p;
    binomial = (binomial * modInverse(n1 - k, p)) % p;
    vals[n1-A] = binomial;
  }

  long long previousbinomial = 1;
  long long firstvalue = 1;

  if (NodeNo > 0) {
    Receive(NodeNo - 1);
    previousbinomial = GetLL(NodeNo - 1);
    firstvalue = GetLL(NodeNo - 1);
    n = GetLL(NodeNo - 1);
    // cerr << "Received " << firstbinomial << " " << total << endl;
  } else {
    firstvalue = power(2, k, p);
  }
  long long value = firstvalue;
  binomial = previousbinomial;
  long long n1;
  for (n1 = A; n1 < B; n1++) {
    if (n1 == n) {
      cout << value << endl;
    }
    binomial = (vals[n1-A] * previousbinomial);
    // cout << "P(" << n1 << ", " << k << ") = " << value << " (" << binomial << ")" <<  endl;
    value = (2 * value + psquare - binomial)%p;
  }
  binomial %= p;

  delete [] vals;
  if (NodeNo == NodeCount - 1) {
    // if (reversed) {
    //   total = (power(2, n, p) - total + p) % p;
    // }
    // cout << total << endl;
  } else {
    PutLL(NodeNo + 1, binomial);
    PutLL(NodeNo + 1, value);
    PutLL(NodeNo + 1, n);

    Send(NodeNo + 1);
  }
}


int main() {

  NodeNo = MyNodeId();
  NodeCount = NumberOfNodes();
  int n = GetN();
  long long p = GetP();
  int k = GetK();
  if (NodeNo == NodeCount - 1) {
    PutInt(0, n);
    Send(0);
  }
  if (NodeNo == 0) {
    Receive(NodeCount - 1);
    n = GetInt(NodeCount - 1);
    
  }
  if (n != 0) {
    knownN();
  } else {
    if (k < 5000000) {
      if(NodeNo == 0) {
        NodeCount = 1;
        knownN();
      }
    } else {
      unknownN();
    }
  }


  return 0;
}