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
#include<bits/stdc++.h>
#include "message.h"
#include "futbol.h"
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (ll)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 1e18L + 1;
const int IINF = 1e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

//#define LOCAL
#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...) (__VA_ARGS__)
#define cerr if(0)cout
#endif

const ll N = 1000000000, C = 40, FI = 10000;

ll mod = GetP(), k = GetK(), is = MyNodeId(), nds = NumberOfNodes();

//ll mod = 258589211, k = 173308313, is = MyNodeId(), nds = NumberOfNodes();

ll int_pow(ll base, ll exp){
	if(exp == 0){
		return 1ll;
	} else if(exp == 1){
		return base;
	} else if(exp & 1){
		return (base * int_pow((base * base) % mod, exp >> 1)) % mod;
	} else{
		return int_pow((base * base) % mod, exp >> 1);
	}
}

vector<ll> cand_n;
vector<ll> fact_ref = {1, 1};

ll chks = 0;

ll get_fact(ll x){
	ll res =  fact_ref[x / FI];
	rep(i, x / FI * FI + 1, x + 1){
		res = (res * i) % mod;
	}
	/*
	if(chks < 100){
		ll check_res = 1ll;
		rep(i, 1, x + 1){
			check_res = (check_res * i) % mod;
		}
		if(res != check_res){
			DBG(x, res, check_res, fact_ref[x / FI]);
			assert(res == check_res);
		}
	}
	*/
	return res;
}

ll newt(ll n, ll k){ return get_fact(n) * int_pow(get_fact(k), mod - 2) % mod * int_pow(get_fact(n - k), mod - 2) % mod; }


ll cor_res(ll cand_res, ll st_n, ll true_n){
	DBG(cand_res, st_n, true_n, k);
	ll temp_k = (k > st_n) ? st_n : k;
	ll nk = get_fact(st_n) * int_pow(get_fact(temp_k), mod - 2) % mod * int_pow(get_fact(st_n - temp_k), mod - 2) % mod;
	DBG(nk);
	rep(i, st_n, true_n){
		cand_res = (2 * cand_res + mod - nk) % mod;
		//DBG(i, nk, cand_res);
		nk = nk * (i + 1) % mod * int_pow(i + 1 - temp_k, mod - 2) % mod;
	}
	rep(i, temp_k + 1, k + 1){
		nk = nk * (true_n - (i - 1)) % mod * int_pow(i, mod - 2) % mod;
		cand_res = cand_res + nk;
	}
	return cand_res % mod;
}

int main()
{
#ifndef LOCAL
	ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
	ll st_fact = is * (N / nds), fn_fact = (is == nds - 1) ? N : (is + 1) * (N / nds) - 1;
	ll c_fact = 1ll;
	vector<ll> to_send;
	rep(i, max(1ll, st_fact), fn_fact + 1){
		c_fact = (c_fact * i) % mod;
		if(i % FI == 0){
			to_send.pb(c_fact);
			c_fact = 1ll;
		}
	}
	to_send.pb(c_fact);
	PutLL(0, SZ(to_send));
	for(auto& i : to_send){
		PutLL(0, i);
	}

	rep(i, 1, C){
		cand_n.pb(i * (N / C));
	}
	ll st = is * (k / nds), fn = (is == nds - 1) ? k : (is + 1) * (k / nds) - 1;
	vector<ll>invs(fn - st + 1);
	rep(i, st, fn + 1){
		invs[i - st] = int_pow(i, mod - 2);
	}
	for(auto& cnd : cand_n){
		ll cnd_res = 0, cur = 1;
		rep(i, st, fn + 1){
			if(i <= cnd){
				cnd_res = cnd_res + cur;
				cur = cur * (cnd - i) % mod * invs[i + 1 - st] % mod;
			}
		}
		cnd_res %= mod;
		DBG(cnd, cnd_res);
		PutLL(0, st);
		PutLL(0, cnd_res);
	}
	Send(0);
	if(is == 0){
		rep(i, 0, nds){
			Receive(i);
			ll cnt_fact = GetLL(i);
			if(cnt_fact > 0){
				ll f_fact = GetLL(i);
				fact_ref.back() = (fact_ref.back() * f_fact) % mod;
				cnt_fact--;
			}
			rep(_, 0, cnt_fact){
				ll seg_pr = GetLL(i);
				fact_ref.pb((fact_ref.back() * seg_pr) % mod);
			}
		}

		ll true_n = GetN(), best_cnd = 0, cand_res = 1;
		//ll true_n = 187694266, best_cnd = 0, cand_res = 0;
		for(auto& cnd : cand_n){
			if(cnd <= true_n){
				best_cnd = cnd;
				cand_res = 0;
			}
		}
		rep(i, 0, nds){
			for(auto& cnd : cand_n){
				ll seg_st = GetLL(i), seg_sm = GetLL(i);
				if(cnd == best_cnd and best_cnd >= seg_st){
					ll nwt = newt(best_cnd, seg_st);
					DBG(cand_res, nwt, seg_sm);
					cand_res = (cand_res + newt(best_cnd, seg_st) * seg_sm % mod) % mod;
				}
			}
		}
		cout << cor_res(cand_res, best_cnd, true_n) << "\n";
	}
    return 0;
}