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
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>
#include "message.h"

//#warning LOCAL_TEST
//#define LOCAL_TEST

#ifdef LOCAL_TEST
int GetN() { return 1e9 - 67; }
int GetK() { return 12345; }
int GetP() { return 1e9 - 63; }
#else
#include "futbol.h"
#endif


using namespace std;
#define PB push_back
#define MP make_pair
#define LL long long
#define int LL
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define RE(i,n) FOR(i,1,n)
#define REP(i,n) FOR(i,0,(int)(n)-1)
#define VI vector<int>
#define PII pair<int,int>
#define LD double
#define FI first
#define SE second
#define st FI
#define nd SE
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())


template<class C> void mini(C &a4, C b4) { a4 = min(a4, b4); }
template<class C> void maxi(C &a4, C b4) { a4 = max(a4, b4); }

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream& os, vector<T> V) {
  os << "["; for (auto vv : V) os << vv << ","; return os << "]";
}
template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}

#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define debug(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

int N, K, P;

int PowMod(int a, int x) {
  int r = 1;
  while (x) {
    if (x & 1) { r = (LL)r * a % P; }
    x >>= 1;
    a = (LL)a * a % P;
  }
  return r;
}

int my_node;
int num_nodes;
vector<int32_t> binomial_sums;
vector<int32_t> binomials;
vector<int32_t> inv_denoms;
int my_rows_from;
int my_rows_to;

void Preproc() {
  const int rows_from = K + 1;
  const int rows_to = P;
  const int num_rows = rows_to - rows_from;
  my_rows_from = rows_from + (LL)num_rows * my_node / num_nodes;
  my_rows_to = rows_from + (LL)num_rows * (my_node + 1) / num_nodes;

  const int my_size = my_rows_to - my_rows_from;
  binomial_sums.resize(my_size + 2);
  binomials.resize(my_size + 2);
  inv_denoms.resize(my_size + 2);
  inv_denoms[0] = 1;
  for (int i = 0; i < my_size + 1; ++i)
    inv_denoms[i + 1] = (LL)inv_denoms[i] * (my_rows_from - K + i) % P;
  int inv_prod = PowMod(inv_denoms.back(), P - 2);
  for (int i = my_size; i >= 0; --i) {
    inv_denoms[i] = (LL)inv_denoms[i] * inv_prod % P;
    inv_prod = (LL)inv_prod * (my_rows_from - K + i) % P;
  }
  inv_denoms.pop_back();

  debug(my_rows_from, my_rows_to);

  debug(inv_denoms);
  
  binomials[0] = 1; // Binomial(my_rows_from, K + 1) in reality;
  for (int i = 0; i < my_size; ++i) {
    const int m = my_rows_from + i + 1;
    LL new_val = (LL)binomials[i] * m % P;
    new_val = new_val * inv_denoms[i] % P;
    binomials[i + 1] = new_val;
  }

  debug(binomials);

  for (int i = 1; i <= my_size; ++i) {
    binomial_sums[i] = (2LL * binomial_sums[i - 1] + binomials[i - 1]) % P;
  }
  debug(binomial_sums);
}

int GetNodeSub(int first_binom) {
  // My rows: [my_rows_from, my_rows_to)
  // Task rows: [0, N)

  debug(first_binom);

  if (my_rows_from >= my_rows_to) { return 0; }
  if (my_rows_from == N) { return first_binom; }
  if (my_rows_from >= N) { return 0; }
  const int end_row_id = min(N, my_rows_to);
  const int sum_base = (LL)first_binom * binomial_sums[end_row_id - my_rows_from] % P;
  const int end_add =
    (my_rows_to > N ? (LL)first_binom * binomials[N - my_rows_from] : 0) % P;

  debug(end_row_id, sum_base, end_add);

  return ((LL)sum_base * PowMod(2, N - end_row_id) + end_add) % P;
}

int32_t main() {
  N = GetN();
  K = GetK();
  P = GetP();
  debug(N, K, P);
  my_node = MyNodeId();
  num_nodes = NumberOfNodes();

  Preproc();

  int total_sub = 0;
  int first_binom;
  int next_binom;

  const int prev_node = (my_node + num_nodes - 1) % num_nodes;
  const int next_node = (my_node + 1) % num_nodes;

  if (my_node == 0) {
    first_binom = 1;
    if (K == 0) {
      total_sub = PowMod(2, N) - 1;
    } else if (K == N) {
      total_sub = 0;
    } else {
      total_sub = GetNodeSub(1);
    }

  } else {
    Receive(prev_node);
    N = GetInt(prev_node);
    first_binom = GetInt(prev_node);
    total_sub = GetInt(prev_node);
    if (0 < K && K < N) {
      total_sub = (total_sub + GetNodeSub(first_binom)) % P;
    }
  }

  next_binom = (LL)first_binom * binomials[my_rows_to - my_rows_from] % P;
  PutInt(next_node, N);
  PutInt(next_node, next_binom);
  PutInt(next_node, total_sub);
  Send(next_node);

  if (my_node) { return 0; }

  Receive(prev_node);
  const int N_prev = GetInt(prev_node);
  GetInt(prev_node);
  total_sub = GetInt(prev_node);
  assert(N == N_prev);

  const int total = PowMod(2, N);
  cout << (total - total_sub + P) % P << "\n";
}