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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "message.h"
#include "futbol.h"
#include "bits/stdc++.h"

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define debug(x) {cout <<#x <<" = " <<x <<endl; }
#define lint long long int
using namespace std;

int id, nodes, n, k, p, pol;

int powmod(int a, int b) {
	int res=1;
	for (;b;b>>=1,a=(a*1LL*a)%p) 
		if (b&1) res=(res*1LL*a)%p;
	return res;
}


//4000*2500

int bloki[105][5300];
int caly_blok[105];

int tloki[105][5300];
int war[105];

int p_tlok[105][5300];

int BLOCK_SIZE = 2000;
int BLOCKS = 5000;
int BND = 1e7;
int LAND = 1e5;
int minv[100007];

int inv(int a) {
	if (a > p) a %= p;
	if (a <= LAND) return minv[a];
	int U = p, V = a, R = 0, S = 1;
	while (V > 0) {
		if (U&1) {
			if (V&1) {
				if (U > V) {
					U -= V; R-= S;
					if (R < 0) R+= p;
				} else {
					V -= U;
					S -= R;
					if (S < 0) S+= p;
				}
			} else {
				V = V >> 1;
				if (S&1) {
					S = (S+p) >> 1;
				} else {
					S = S >> 1;
				}
			}
		} else {
			U = U >> 1;
			if (R&1) {
				R = (R + p) >> 1;
			} else {
				R >>= 1;
			}
		}
		if (U <= LAND) {
			return (minv[U]*1LL*R)%p;
		}
		if (V <= LAND) {
			return (minv[V]*1LL*S)%p;
		}
	}
	if (U > 1) return 0;
	if (R > p) R = R - p;
	if (R < 0) R = R + p;
	return R;
}


int silnia(int u) {
  if (u == 0) return 1;
	int ans = 1;
	int kon = 1;
	int ktory = 0;
	while (kon + BND <= u) {
		ans = (ans * 1LL * caly_blok[ktory]) % p;
		ktory++;
		kon += BND;
	}
	if (kon >= u) {
        return ans;
    }
	int lans = 1;

	int maly = (u-kon)/BLOCK_SIZE;
	kon += maly * BLOCK_SIZE;
	lans = (maly > 0) ?bloki[ktory][maly-1] : 1;
	ans = (ans * 1LL * lans) % p;
	
	while (kon <=u) {
		ans = (ans * 1LL * kon)%p;
		kon++;
	}
	return ans;
}

void wypelnij_invy() {
    minv[0] = 0;
    minv[1] = 1;
    FOR(i,2,min(LAND+1,p)) {
				int q = p/i;
        minv[i] = (minv[p-q*i] * 1LL * (p - q))%p;
    }
}

int get_const(int i) {
	int sil = silnia(k-1+BND*i);
	int sil2 = silnia(BND*i);
	int p2 = powmod(pol, BND*i);
	
	int inter = (sil * 1LL * inv(sil2))%p;
	return (inter * 1LL * p2) % p;
}

int main() {
  n = GetN(), k = GetK(), p = GetP();
	pol = (p - 1)/2;
	id = MyNodeId();
	nodes = NumberOfNodes();

    wypelnij_invy();
	int startN = id * BND+1;
	int cale = 1;
	int jak = 1;
	FOR(blok,0,BLOCKS) {
		FOR(j,startN + blok*BLOCK_SIZE,startN+(blok+1)*BLOCK_SIZE) {
			jak = (jak * 1LL * j) % p;
		}
		bloki[id][blok] = jak;
	}
	cale = jak;
	int myval = 0;
	int lval = 1;

	FOR(blok,0,BLOCKS) {
		FOR(j,startN + blok*BLOCK_SIZE,startN+(blok+1)*BLOCK_SIZE) {
			lval = (lval * 1LL * (k-1+j)) % p;
			lval = (lval * 1LL * inv(2*j)) % p;

			myval += lval;
			if (myval >= p) myval-= p;
		}
		tloki[id][blok] = myval;
		p_tlok[id][blok] = lval;
	}

	// szykuj wiadomosci 
	if (id != 0) {
		FOR(i,0,BLOCKS) {
			PutInt(0, bloki[id][i]);
		}
		PutInt(0,cale);
		FOR(i,0,BLOCKS) {
			PutInt(0, tloki[id][i]);
		}
		PutInt(0,myval);
		FOR(i,0,BLOCKS) {
			PutInt(0, p_tlok[id][i]);
		}
		Send(0);
		return 0;
	} else {
		FOR(i,0,nodes - 1) {
			int kto = Receive(-1);
			FOR(j,0,BLOCKS) {
				bloki[kto][j] = GetInt(kto);
			}
			caly_blok[kto] = GetInt(kto);
			FOR(j,0,BLOCKS) {
				tloki[kto][j] = GetInt(kto);
			}
			war[kto] = GetInt(kto);
			FOR(j,0,BLOCKS) {
				p_tlok[kto][j] = GetInt(kto);
			}
		}
	}
	// KOMBINUJ DZIEWCZYNO
	if (id == 0) {
			if (k == 0) {
				printf("1\n");
				return 0;
			} 
			if (n == k) {
				printf("%d\n", powmod(2, n));
				return 0;
			}
			if (k == 1) {
				printf ("%d\n", (n+1) % p);
				return 0;
			}
			if (p == 2) {
				// (n-1) po k % 2 LUCASEM GWIEZDNE WOJNY XDXDXD
				return 0;
			}
			caly_blok[0] = cale;
			war[0] = myval;
			
			int C = (powmod(2, n-k) * 1LL * inv(silnia(k-1)))%p;

			int result = 0;
			int start = -1;
			FOR(i,0,nodes) {
				if ((i+1)*BND <= n-k) {
					int con = get_const(i);
					result = (result + con * 1LL * war[i]) % p;
				} else {
					start = i;	
					break;
				}
			}
			if (start != -1) {
				int meta = n-k;
				int con = get_const(start);


				int myval = 0;
				int lval = 1;
				int tlok_nr = 0;
				
				int ile_moge = (meta-(start*BND))/BLOCK_SIZE;
				myval = (ile_moge == 0) ? 0 : tloki[start][ile_moge-1];
				int startuj = start*BND + 1 + BLOCK_SIZE*ile_moge;
				lval = (ile_moge == 0) ? 1 : p_tlok[start][ile_moge-1];

				for (int i = startuj; i < meta+1; i++) {
						lval = (lval * 1LL * (k-1+i)) % p;
						lval = (lval * 1LL * inv(i)) % p;
						if (lval % 2 == 0) lval = lval/2;
						else {
							lval = (lval + p) / 2;
						}
						myval += lval;
						if (myval >= p) myval -= p;
				}
				result = (result + (con * 1LL * myval))%p;
			}
			result = (result * 1LL * C) % p;
			result = (result + powmod(2, n-k))%p;
			int p2 = powmod(2, n);
			int newton = (silnia(n) * 1LL * inv(silnia(n-k)))%p;
			newton = (newton * 1LL * inv(silnia(k)))%p;
			
			int uff = p2 + newton - result;
			if (uff < 0) uff += p;
			if (uff >= p) uff -=p;
			printf("%d\n", uff);
			return 0;
	}
}