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
#include "message.h"
#include "futbol.h"
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

#define PB push_back
#define ST first
#define ND second

//~ int GetN() {if(MyNodeId() == 0) return 739999430; return 0;}
//~ int GetK() {return 23;}
//~ int GetP() {return 999999197; }

ll po(ll a, ll k, ll p)
{
	ll res=1;
	while(k)
	{
		if(k%2)
			res = (res*a)%p;
		a = (a*a)%p;
		k/=2;
	}
	return (res%p+p)%p;
}

ll inv(ll a, int p)
{
	return po(a, p-2, p);
}

void lastPart(ll _s, ll _t, ll _res, ll _fact, int d, ll n, ll k, int p)
{
	vector<ll> s, t, res, fact, b;
	s.PB(_s);
	t.PB(_t);
	res.PB(_res);
	fact.PB(_fact);
	b.PB(0);
	for(int i=1; i < d; i++)
	{
		Receive(i);
		s.PB(GetLL(i));
		t.PB(GetLL(i));
		res.PB(GetLL(i));
		fact.PB(GetLL(i));
		b.PB((n/d + 1)*i);
	}
	ll nmk=1;
	for(int i=1; i < d; i++)
	{
		s[i] = (s[i]*s[i-1])%p;
		fact[i] = (fact[i]*fact[i-1])%p;
	}
	for(int i=d-2; i >= 0; i--)
	{
		t[i] = (t[i]*t[i+1])%p;
	}
	ll fin = 0;
	for(int i=0; i < d; i++)
	{
		ll x = res[i];
		if(i > 0)
			x = (x*s[i-1])%p;
		if(i < d-1)
			x = (x*t[i+1])%p;
		//~ cerr << "\t\t" << (x*fact.back())%p << "\n";
		fin = (fin+x)%p;
	}
	int j;
	for(j = 0; j+1 < d; j++)
	{
		if(b[j+1] > n-k-1)
			break;
	}
	if(j > 0)
		nmk = fact[j-1];
	for(ll x = max(1LL, b[j]); x <= n-k-1; x++)
	{
		nmk = (nmk*x)%p;
	}
	//~ cerr << "almost fin: " << b[j+1] << " " << n-k-1 << " " << nmk << endl;
	fin = (fin*fact.back())%p;
	fin = ((fin*inv(nmk, p))%p+p)%p;
	cout << fin << "\n";
}

void withN(ll n, ll k, int p)
{
	int id = MyNodeId();
	int d = NumberOfNodes();
	d--;
	ll b, e;
	b = ((k+1)/d+1)*id;
	e = ((k+1)/d+1)*(id+1);
	e = min(k+1, e);
	ll m = e-b-1;
	//~ cerr << b << " " << b+m << "\n";
	ll s=1, t=1;
	for(ll i=0; i <= m; i++)
	{
		if(b+i > 0)
			s = (s*(b+i))%p;
		if(n-b-i > 0)
			t = (t*(n-b-i))%p;
	}
	s = (s+p)%p;
	t = (t+p)%p;
	s = inv(s, p);
	t = inv(t, p);
	vector<int> v, u;
	v.PB(s);
	u.PB(t);
	for(ll i=m; i > 0; i--)
	{
		ll x=1;
		if(b+i > 0)
			x = (ll(v.back())*(b+i))%p;
		v.PB(x);
	}
	//ll res = (u.back()*v[m])%p;
	for(ll i=0; i < m; i++)
	{
		ll x = 1;
		if(n-b-i > 0)
			x = (ll(u.back())*(n-b-i))%p;
		//res = (res+x*v[m-i-1])%p;
		//u[0] = x;
		u.PB(x);
	}
	reverse(v.begin(), v.end());
	reverse(u.begin(), u.end());
	ll res=0;
	for(ll i=0; i <= m; i++)
	{
		res = (res+ll(u[m-i])*ll(v[i]))%p;
		//~ cerr << "a: " << i << " " << inv(u[m-i], p) << " " << inv(v[i], p) << "\n";
	}
	//compute part of n!
	ll x = (n/d + 1)*id, y = (n/d + 1)*(id+1);
	x = max(1LL, x);
	y = min(y, n+1);
	//~ cerr << "f: " << x << " " << y << "\n";
	ll fact=1;
	for(ll i = x; i < y; i++)
	{
		fact = (fact*i)%p;
	}
	//~ cerr << b << " " << b+m << endl;
	//~ cerr << inv(s, p) << " " << inv(t, p) << " " << res << " " << fact << endl;
	if(id == 0)
	{
		lastPart(s, t, res, fact, d, n, k, p);
	}
	else
	{
		PutLL(0, s);
		PutLL(0, t);
		PutLL(0, res);
		PutLL(0, fact);
		Send(0);
		return;
	}
}

void withoutN(ll n, ll k, int p)
{
	int id = MyNodeId();
	if(id != 0)
		return;
	ll f = 1;
	vector<int> invf;
	for(ll i=1; i <= n; i++)
	{
		f = (f*i)%p;
	}
	invf.PB(inv(f, p));
	for(ll i=n; i > 0; i--)
	{
		invf.PB((ll(invf.back())*i)%p);
	}
	reverse(invf.begin(), invf.end());
	ll res=0;
	for(ll i=0; i <= k; i++)
	{
		res = (res + ll(invf[i])*ll(invf[n-i]))%p;
	}
	res = ((res*f)%p+p)%p;
	cout << res << "\n";
}

int main()
{
	int id = MyNodeId();
	int d = NumberOfNodes();
	ll n = GetN();
	ll k = GetK();
	int p = GetP();
	//~ cerr << n << " " << k << " " << p << endl;
	if(id == d-1)
	{
		PutLL(0, n);
		Send(0);
		return 0;
	}
	bool kn=true;
	if(id == 0)
	{
		Receive(d-1);
		ll np = GetLL(d-1);
		if(np == 0)
			kn = false;
	}
	else
	{
		if(n == 0)
			kn = false;
	}
	if(kn)
		withN(n, k, p);
	else
		withoutN(n, k, p);
}