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
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define REP(i, a) FOR(i, 0, a - 1)
#define ST first
#define ND second
#define V vector
#define RS resize
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
#define S(a) (int)a.size()

template<class T> void db(T a) { cerr << a; }
template<class L, class R> void db(pair<L, L> a) { cerr << "(" << a.ST << ", " << a.ND << ")"; }
template<class T> void db(V<T> v) { cerr << "{"; REP(i, S(v)) cerr << (i != 0 ? ", " : ""), db(v[i]); cerr << "}"; }
template<class T> void dump(const char *s, T a) { cerr << s << ": "; db(a); cerr << "\n"; }
template<class T, class... TS> void dump(const char *s, T a, TS... x)
{ while(*s != ',') cerr<< *s++; cerr << ": "; db(a); dump(s + 1, x...); }

#ifdef DEBUG
#define DB(...) dump(#__VA_ARGS__, __VA_ARGS__); 
#else
#define DB(...)
#endif

using LL = long long;
using PII = pair<int, int>;
using VI = V<int>;
using VLL = V<LL>;
using VVI = V<VI>;
using VPII = V<PII>;

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

LL mod;
LL p(LL base, LL n)
{
    if(n == 0)
        return 1;
    if(n % 2 == 1)
        return (base * p(base, n - 1)) % mod;
    LL x = p(base, n / 2);
    return (x * x) % mod;
}
LL reverse(LL a) { return p(a, mod - 2); }

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

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

    if(id == NumberOfNodes() - 1)
    {
        PutInt(0, n);
        Send(0);
        return 0;
    }

    if(id == 0)
    {
        Receive(NumberOfNodes() - 1);
        int tn = GetInt(NumberOfNodes() - 1);
        if(tn == 0)
        {
            LL S = 1;
            FOR(i, 1, n)
                S = (S * i) % mod;

            VLL F(n + 1, 1);
            F[n] = reverse(S);
            for(int i = n - 1; i >= 1; i--)
                F[i] = F[i + 1] * i;

            LL ans = 0;
            for(int i = 0; i <= k; i++)
                ans += F[i] * F[n - i];

            ans = (ans * S) % mod;
            cout << ans << "\n";
            
            return 0;
        }
    }

    if(n == 0)
    {
        return 0;
    }

    int node_count = NumberOfNodes() - 1;
    int d = n / node_count;
    int c = n - d * node_count;

    int a = 0, b = 0;
    REP(i, id + 1)
    {
        a = b + 1;
        b = a + d - (c <= 0);
        c--;
    }

    int nexta = b + 1;
    int nextb = nexta + d - (c <= 0);

    int s = b - a + 1;
    if(s == 0)
        return 0;

    LL F = 1;
    LL R = 1;

    FOR(i, a, b)
        F = (F * i) % mod;
    R = reverse(F); 

    LL g = -1;
    LL cpy = R;
    for(int i = b; i >= a; i--)
    {
        if(i == n - k)
            g = cpy;
        cpy = (cpy * i) % mod;
    }

    LL sum = 0;
    int kb = min(k, b);
    int knextb = min(k, nextb);

    LL x = 1;
    FOR(i, n - kb + 1, n - a)
        x = (x * i) % mod;
    x = reverse(x);

    if(a <= kb)
    {
        VLL M(kb - a + 1, 1);
        int pos = 0;
        for(int i = kb; i >= a; i--)
        {
            if(pos != 0) M[pos] = M[pos - 1];
            M[pos] = (M[pos] * i) % mod;
            pos++;
        }

        pos--;
        LL rev = reverse(M[pos]);

        FOR(i, a, kb)
        {
            pos--;
            LL m = (pos == -1 ? 1 : M[pos]);
            LL reev = (rev * m) % mod;
            rev = (rev * i) % mod;
            x = (x * reev) % mod;
            sum = (sum + x) % mod;
            x = (x * max(1, n - i)) % mod;
        }
    }

    LL xd = 1;
    REP(i, max(0, knextb - nexta + 1))
        xd = (xd * (n - kb - i)) % mod;
    xd = reverse(xd);

    if(id != 0)
    {
        Receive(id - 1);
        LL f = GetLL(id - 1);
        LL r = GetLL(id - 1);
        LL s = GetLL(id - 1);
        LL _g = GetLL(id - 1);

        F = (F * f) % mod;
        R = (R * r) % mod;
        sum = (sum * r) % mod;
        sum = (sum + s) % mod;
        if(g != -1) g = (g * r) % mod;
        else g = _g;
    }

    sum = (sum * xd) % mod;

    int last = min(n, node_count) - 1;
    if(id != last)
    {
        PutLL(id + 1, F);
        PutLL(id + 1, R);
        PutLL(id + 1, sum);
        PutLL(id + 1, g);
        Send(id + 1);
    }

    if(id == last)
    {
        sum = (sum * g) % mod;
        cout << (sum * F + 1) % mod << "\n";
    }
}