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
#include "message.h"
#include "futbol.h"

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

int n,k,p;
int nodes, my_id;

//int GetN() { return my_id==0 ? 997365040 : 0; }
//int GetK() { return 496917823; }
//volatile int GetP() { return 999287789; }

int qpow(int a, int m) {
	if (m==0) return 1;
	int b = qpow(a,m/2);
	b = 1LL*b*b%p;
	if (m&1) b = 1LL*a*b%p;
	return b;
}

inline int inv(int x) {
	int q=p-2, res=1;
	/*FOR(i,30) {
		if ((q>>i)&1) res=1LL*res*x%p;
		x=1LL*x*x%p;
	}
	return res;*/
	if ((q>>0)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>1)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>2)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>3)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>4)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>5)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>6)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>7)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>8)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>9)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>10)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>11)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>12)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>13)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>14)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>15)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>16)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>17)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>18)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>19)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>20)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>21)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>22)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>23)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>24)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>25)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>26)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>27)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>28)&1) res=1LL*res*x%p; x=1LL*x*x%p;
	if ((q>>29)&1) res=1LL*res*x%p;
	return res;
	return qpow(x, p-2);
}

const int M = 11100100;
int a[M+10];
pair<ll,ll> populate(int lo, int hi) {
	ll cur = 1, res = 0;
	a[hi-lo]=1;
	for (int j = hi; j > lo; j--) {
		a[j-lo-1] = 1LL * a[j-lo] * j % p;
	}
	for (int j = lo; j < hi; j++) {
		//printf("adding %d*%d/%d = %d/%d\n", cur, a[j-lo], a[1], cur * a[j-lo], a[1]);
		res = (cur * a[j-lo] + res) % p;
		if (res >= p) res -= p;
		cur = cur * (n-j) % p;
	}
	int ai = inv(a[0]);
	cur = cur * ai % p;
	res = res * ai % p;
	//printf("%d %d -> %lld %lld\n", lo, hi, cur, res);
	return mp(cur, res);
}

int main() {
	my_id = MyNodeId();
	nodes = NumberOfNodes();
	n = GetN();
	k = GetK();
	p = GetP();
	if (my_id == nodes-1) {
		PutInt(0, n);
		Send(0);
		return 0;
	}
	if (my_id == 0) {
		Receive(nodes-1);
		int n0 = GetInt(nodes-1);
		if (n0 == 0) {
			nodes = 2;
			PutInt(1, n);
			Send(1);
		} else {
			nodes--;
		}
	} else {
		if (n == 0) {
			if (my_id == 1) {
				Receive(0);
				n = GetInt(0);
				nodes = 2;
			} else {
				return 0;
			}
		} else {
			nodes--;
		}
	}
	//printf("nodes = %d, n = %d\n", nodes, n);
	bool flip = 0;
	if (k*2 >= n) {
		flip = 1;
		k = n-k-1;
	}
	int lo = 1LL * my_id * (k+1) / nodes, hi = 1LL * (my_id+1) * (k+1) / nodes;
	ll cur = 1, res = 0;
	for (int i = lo; i < hi; i+=M) {
		pair<ll,ll> tmp = populate(i, min(i+M,hi));
		res = (cur * tmp.se + res) % p;
		cur = cur * tmp.fi % p;
	}
	//printf("range = %d %d\tsum = base * %d\tbase mutiplier = %d\n", lo, hi, res, cur);
	if (my_id > 0) {
		PutLL(0, cur);
		PutLL(0, res);
		Send(0);
		//printf("sent from %d to 0\n", my_id, 0);
	}
	if (my_id == 0) {
		int ret = res, val = cur;
		for (int i = 1; i < nodes; i++) {
			//printf("getting from %d\n", i);
			Receive(i);
			cur = GetLL(i);
			res = GetLL(i);
			ret = (1LL * val * res + ret) % p;
			val = 1LL * val * cur % p;
		}
		if (flip) {
			ret = (qpow(2,n) + p - ret) % p;
		}
		printf("%d\n", ret);
	}
	return 0;
}