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


using namespace std;

int expo(int a, int n, int mod) {
    int ans = 1;

    while (n) {
        if (n & 1) {
            ans = ((long long) ans * a) % mod;
        }

        n >>= 1;
        a = ((long long) a * a) % mod;
    }

    return ans;
}

int revMod(int a, int mod) {
    return expo(a, mod - 2, mod);
}

int allignUp(int pos, int blockLen) {
    assert(pos >= 0);
    return pos / blockLen * blockLen + blockLen - 1;
}

int solveBlock(int myBlockBeg, int myBlockEnd, int blockLen, int k, int p, bool allign) {
    int inv2 = revMod(2, p);
    int sum = 0;

    vector <int> suffProd(myBlockEnd - myBlockBeg + 2, 1);
    for (int i = myBlockEnd; i >= myBlockBeg; i--) {
        int index = i - myBlockBeg;
        suffProd[index] = ((long long) (i - k) * suffProd[index + 1]) % p;
    }

    int here = 1;
    for (int i = myBlockBeg; i <= myBlockEnd; i++) {
        if (i > 0) {
            here = ((long long) here * i) % p;
            here = ((long long) here * inv2) % p;
        }

        if (i >= k) {
            sum = (sum + (long long) here * suffProd[i - myBlockBeg + 1]) % p;
        }
    }

    if (allign) {
        int pos = allignUp(myBlockEnd - k, blockLen);
        for (int x = myBlockEnd - k + 1; x <= pos; x++) {
            sum = ((long long) sum * x) % p;
        }
    }

    if (myBlockBeg > 0) {
        sum = ((long long) sum * expo(inv2, myBlockBeg - 1, p)) % p;
    }

    return sum;
}

int getFact(int k, int blockLen, vector <int> &factBlocks, int p) {
    int div = (k + 1) / blockLen;
    int prod = factBlocks[div];

    for (int i = max(1, div * blockLen); i <= k; i++) {
        prod = ((long long) prod * i) % p;
    }

    return prod;
}

int main() {
    ios_base::sync_with_stdio(0);

    int myId = MyNodeId();

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

    int numNodes = min(p, NumberOfNodes());
    if (myId >= numNodes) {
        return 0;
    }

    int blockLen = (p + numNodes - 1) / numNodes;
    int myBlockBeg = blockLen * myId;
    int myBlockEnd = min(myBlockBeg + blockLen, p) - 1;

    int sum = 0;

    if (myBlockEnd >= k) {
        sum = solveBlock(myBlockBeg, myBlockEnd, blockLen, k, p, true);
    }

    int prodFull = 1;
    for (int i = max(1, myBlockBeg); i <= myBlockEnd; i++) {
        prodFull = ((long long) prodFull * i) % p;
    }

    PutInt(0, sum);
    PutInt(0, prodFull);

    Send(0);

    if (myId == 0) {
        int n = GetN();

        int ans = 0;

        vector <int> factBlocks = {1};
        vector <int> partialValues(numNodes);

        for (int i = 0; i < numNodes; i++) {
            Receive(i);

            partialValues[i] = GetInt(i);
            factBlocks.push_back(GetInt(i));
        }

        for (int i = 1; i < (int) factBlocks.size(); i++) {
            factBlocks[i] = ((long long) factBlocks[i] * factBlocks[i-1]) % p;
        }

        for (int i = 0; i < numNodes; i++) {
            partialValues[i] = ((long long) partialValues[i] * factBlocks[i]) % p;

            int blockEnd = min(blockLen * (i + 1), p) - 1;
            
            if (blockEnd >= k) {
                int pos = allignUp(blockEnd - k, blockLen);

                int rv = revMod(getFact(pos, blockLen, factBlocks, p), p);
                partialValues[i] = ((long long) partialValues[i] * rv) % p;
            }

            if (blockEnd < n) {
                ans = (ans + partialValues[i]) % p;
            } else {
                break;
            }
        }

        int inv2 = revMod(2, p);

        int nRest = n / blockLen * blockLen;
        if (nRest < n) {
            int here = solveBlock(nRest, n-1, blockLen, k, p, false);
            here = ((long long) here * getFact(nRest - 1, blockLen, factBlocks, p)) % p;

            int rv = revMod(getFact(n - 1 - k, blockLen, factBlocks, p), p);
            here = ((long long) here * rv) % p;

            ans += here;
            if (ans >= p) ans -= p;
        }

        ans = ((long long) ans * revMod(getFact(k, blockLen, factBlocks, p), p)) % p;
        ans = (2 + p - ans) % p;
        ans = ((long long) ans * expo(2, n-1, p)) % p;

        cout << ans;
    }

    return 0;
}