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
#include <bits/stdc++.h>
using namespace std;

#include "message.h"
#include "futbol.h"

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mp make_pair

int read() { int n; scanf("%d", &n); return n; }

int p;

ll powMod(ll a, int n) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = (res * a) % p;
		a = (a * a) % p;
		n >>= 1;
	}
	return res;
}

pll add(pll a, pll b) { return mp((a.first * b.second + a.second * b.first) % p, (a.second * b.second) % p); }
pll mul(pll a, pll b) { return mp((a.first * b.first) % p, (a.second * b.second) % p); }
void mul2(pll &a) { a.first <<= 1; if (a.first >= p) a.first -= p; }

pll sum(int x, int k, int s) {
	pll res = mp(1, 1);
	pll cur = mp(x+1, x+1-k);
	for (int i=1; i<s; ++i) {
		mul2(res);
		res = add(res, cur);
		cur = mul(cur, mp(x+i+1, x+i+1-k));
	}
	return res;
}

pair<pll, pll> sum2(int x, int k, int s) {
//	fprintf(stderr, "sum2(%d, %d, %d);\n", x, k, s);
	pll res = mp(1, 1);
	pll cur = mp(x+1, x+1-k);
	for (int i=1; i<s; ++i) {
		mul2(res);
		res = add(res, cur);
		cur = mul(cur, mp(x+i+1, x+i+1-k));
	}
	return mp(cur, res);
}

ll adjust(pll a) { return (a.first * powMod(a.second, p-2)) % p; }

void show(pll a, bool nl = true) {
	printf("(%lld, %lld)", a.first, a.second);
}

void show(pair<pll, pll> a, bool nl = true) {
	printf("( ");
	show(a.first, false);
	printf(" , ");
	show(a.second, false);
	printf(" )    ");
	printf("(%lld , %lld)%s", adjust(a.first), adjust(a.second), nl ? "\n" : "");
}

pair<pll, pll> merge(pair<pll, pll> st, pair<pll, pll> nd, int step) {
	ll mn = powMod(2, step);
	st.second = add(
		mul(st.second, mp(mn, 1)),
		mul(nd.second, st.first)
	);
	st.first = mul(st.first, nd.first);
	return st;
}

pair<pll, pll> aggregate(const vector<pair<pll, pll> > &v, int step, int lastStep) {
	pair<pll, pll> res = v[0];
	ll mn = powMod(2, step);
	ll mn2 = powMod(2, lastStep);
	for (int i=1; i<v.size(); ++i) {
		res.second = add(
			mul(res.second, mp(i+1<v.size() ? mn : mn2, 1)),
			mul(v[i].second, res.first)
		);
		res.first = mul(res.first, v[i].first);
	}
	return res;
}

pair<pll, pll> sum3(int x, int k, int s, int cnt) {
	int step = s / cnt;

	vector<pair<pll, pll> > v(cnt);
	--cnt;
	int lastStep = s - cnt * step;
	for (int i=0; i<cnt; ++i) {
		int xx = x + i * step;
		v[i] = sum2(xx, k, step);
	}
	v[cnt] = sum2(x + cnt * step, k, lastStep);

	return aggregate(v, step, lastStep);
}



ll solveAlone(int n, int k) {
	ll res = powMod(2, n);
	if (n == k) return res;
	res -= adjust(sum2(k, k, n-k).second);
	if (res < 0) res += p;
	return res;
}


int main() {
	int mni = MyNodeId();
	int non = NumberOfNodes();

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

	if (p-k < 1000000) {
		if (mni == 0)
			printf("%lld\n", solveAlone(n, k));
		return 0;
	}

	int step = (p - k) / non;

	int first = k + mni * step;
	int last = k + (mni + 1) * step; if (mni == non - 1) last = p;

	pair<pll, pll> state = sum2(first, k, last - first);
	pair<pll, pll> prevState = mp(mp(1, 1), mp(0, 1));


	int src = mni - 1;
	int dst = mni + 1;

	if (mni > 0) {
		Receive(src);
		ll cmd = GetLL(src);
		if (cmd == 1) {
			if (dst < non) {
				PutLL(dst, 1);
				Send(dst);
			}
			return 0;
		}
		n = GetLL(src);
		prevState.first.first = GetLL(src);
		prevState.first.second = GetLL(src);
		prevState.second.first = GetLL(src);
		prevState.second.second = GetLL(src);
	}

	if (n > last) {
		pair<pll, pll> res = merge(prevState, state, last - first);
		PutLL(dst, 2);
		PutLL(dst, n);
		PutLL(dst, res.first.first);
		PutLL(dst, res.first.second);
		PutLL(dst, res.second.first);
		PutLL(dst, res.second.second);
		Send(dst);
		return 0;
	}

	if (dst < non) {
		PutLL(dst, 1);
		Send(dst);
	}

	if (n < last) {
		last = n;
		state = sum2(first, k, last - first);
	}

	pair<pll, pll> res = merge(prevState, state, last - first);
	ll s = adjust(res.second);
	ll resv = powMod(2, n);
	if (k < n) {
		resv -= s;
		if (resv < 0) resv += p;
	}
	printf("%lld\n", resv);

	return 0;
}