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

#define FOR(i,a,n) for (auto i ## __ = (n), i = (a); i <= i ## __; ++i)
#define FORD(i,a,n) for (auto i ## __ = (n), i = (a); i >= i ## __; --i)
#define REP(i,n) FOR(i,0, n - 1)
#define ALL(h) begin(h), end(h)
#define EB emplace_back
#define X first
#define Y second
#define V vector
#define tpv typedef V<

typedef long long LL;
typedef pair<int, int> PII;
tpv int> VI;
tpv VI> VVI;
tpv PII> VPII;
tpv LL> VLL;

constexpr char nl = '\n';
#define endl nl
#define ris return *this
#define tem template<class t

tem, class u> inline void mini(t& a, u&& b) { if (b < a) a = b; }
tem, class u> inline void maxi(t& a, u&& b) { if (b > a) a = b; }
int ceil2(int h) { return h < 2 ? 1 : 1 << (sizeof(h) * 8 - __builtin_clz(h - 1)); }

tem> struct Dump { t a, b; };
tem> auto dump(t&& h) -> Dump<decltype(begin(h))> { return {ALL(h)}; }
tem> auto stub(t* h) -> decltype(cerr << *h, 0);
tem> char stub(...);
#define enif(o) tem> typename enable_if<sizeof stub<t>(0) o 1, debug&>::type operator<<(t h)
#define dor > debug& operator<<
struct debug {
#ifdef DEBUG
#define deb debug()
	~debug() { cerr << nl; }
	enif(!=) { cerr << boolalpha << h; ris; }
	enif(==) {
		*this << '{';
		for (auto a = begin(h), b = end(h); a != b;)
			*this << *a++ << &" "[a == b];
		ris << '}';
	}
	tem, class u dor(pair<t, u> p) { ris << '(' << p.X << ", " << p.Y << ')'; }
	tem dor(Dump<t> d) {
		*this << "{\n";
		for (t a = d.a, c = a; a != d.b; ++a)
			*this << "  " << distance(c, a) << ": " << *a << nl;
		ris << '}';
	}
#else
	operator int() { return 0; }
#define deb 0 and debug()
	tem dor(t&&) { ris; }
#endif
};

#define imie(h...) #h ": " << (h) << " "
#define LOG(h...) deb << imie(h)
#define DOG(h...) deb << #h ": " << dump(h) << " "
// End of templates

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

long long inv(long long a, long long m){
	return 1<a ? m - inv(m%a,a)*m/a : 1;
}

LL power(LL a, int k, int mod) {
	LL res = 1;
	while (k) {
		if (k & 1)
			res = res * a % mod;
		a = a * a % mod;
		k >>= 1;
	}

	return res;
}

void brut(int n, int k, int mod) {
	bool rev = false;
	if (k > n / 2) {
		k = n - k - 1;
		rev = true;
	}

	// WARNING: k may be equal to -1 for which the result should be equal to 0

	LL nfac = 1;
	FOR (i, 2, n) {
		nfac = nfac * i % mod;
		LOG(i) imie(nfac) imie(inv(nfac, mod));
	}

	LL inv_nfac = inv(nfac, mod);
	LOG(inv_nfac);
	LL res = 0;

	const int NEXT = 1 << 16;
	VI ifac_cache(NEXT);
	LL next_ifac = 1;

	for (int ii = 0; ii <= k; ii += NEXT) {
		FOR (j, ii + 1, min(ii + NEXT - 1, mod - 1))
			next_ifac = next_ifac * j % mod;

		LL x = inv(next_ifac, mod);
		next_ifac = next_ifac * min(ii + NEXT, mod) % mod;
		FORD (j, min(NEXT - 1, mod - ii - 1), 0) {
			ifac_cache[j] = x;
			x = x * (ii + j) % mod;
		}

		DOG(ifac_cache);
		LOG(next_ifac);

		FOR (i, ii, min(ii + NEXT - 1, k)) {
			LOG(i) imie(ifac_cache[i - ii]);
			res += ifac_cache[i - ii] * inv_nfac % mod;
			inv_nfac = inv_nfac * (n - i) % mod;
		}
	}

	res %= mod;
	res = res * nfac % mod;
	if (rev)
		res = (power(2, n, mod) - res + mod) % mod;

	cout << res << nl;
}

PII segment_interval(int segment_id, int segments_no, int n) {
	int beg = LL(segment_id) * n / segments_no;
	int end = LL(segment_id + 1) * n / segments_no;
	return {beg, end};
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

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

	// 160000000 seems safe for time limit == 3.0
	if (n + 4ll * min(k, n - k) <= 160000000) {
		if (MyNodeId() == 0)
			brut(n, k, mod);
		return 0;
	}

	int M = NumberOfNodes();
	int pid = MyNodeId();

	int a, b;
	tie(a, b) = segment_interval(pid, M, n + 1);
	LOG(pid) imie(a) imie(b);

	LL afac = 1; // (a + 1) * ... * (b - 1)
	FOR (i, a + 1, b - 1)
		afac = afac * i % mod;

	VI inv_afac_cache(b - a, 1);
	{
		LL inv_afac = inv(afac, mod);
		FORD (i, b - 1, a + 1) {
			inv_afac_cache[i - a] = inv_afac;
			inv_afac = inv_afac * i % mod;
		}
	}

	// Computes a! / (a + i)! for i: [a, b - 1]
	auto inv_afac = [&](int i) { return inv_afac_cache[i - a]; };

	LL na_fac = 1; // (n - a) * (n - a - 1) * ... * (n - b + 2)
	FOR (i, a, b - 2)
		na_fac = na_fac * (n - i) % mod;

	VI inv_na_fac_cache(b - a, 1);
	{
		LL inv_na_fac = inv(na_fac, mod);
		FOR (i, a, b - 2) {
			inv_na_fac_cache[i - a] = inv_na_fac;
			inv_na_fac = inv_na_fac * (n - i) % mod;
		}
	}

	// Computes (n - b + 1)! / (n - i)! for i: [a, b - 1]
	auto inv_na_fac = [&](int i) { return inv_na_fac_cache[i - a]; };

	// LOG(pid) imie(inv_na_fac_cache);
	// LOG(pid) imie(inv_afac(a)) imie(inv(inv_afac(b - 1), mod));
	// LOG(pid) imie(inv(inv_na_fac(a), mod)) imie(inv_na_fac(b - 1));
	deb;

	LL local_sum = 0;
	FOR (i, a, min(b - 1, k)) {
		// LOG(pid) imie(inv(inv_afac(i), mod));
		// LOG(pid) imie(inv(inv_na_fac(i), mod));
		local_sum += LL(inv_afac(i)) * inv_na_fac(i) % mod;
	}
	local_sum %= mod;
	LOG(pid) imie(local_sum);

	LL b1fac = afac;
	if (pid > 0) {
		Receive(pid - 1);

		afac = GetInt(pid - 1); // a!
		LOG(pid) imie(afac);
		local_sum = local_sum * inv(afac, mod) % mod;
		b1fac = b1fac * afac % mod;

		LL prev_sum = GetInt(pid - 1); // previous sum
		prev_sum = prev_sum * inv(na_fac * (n - a + 1) % mod, mod) % mod;
		local_sum = (local_sum + prev_sum) % mod;
	}

	if (pid < M - 1) {
		PutInt(pid + 1, b1fac * b % mod); // send b!
		PutInt(pid + 1, local_sum); // send local sum
		Send(pid + 1);
		return 0;
	}

	LOG(b1fac);

	// pid == M - 1
	cout << b1fac * local_sum % mod << nl;
	LOG(inv(29070, mod) * 1549 % mod);

	return 0;
}