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
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>

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

using namespace std;
using ll = long long;

// futsoko42.in
/*
int GetN() { return 833713965; }
int GetK() { return 424203011; }
int GetP() { return 890139749; }
*/

//
// helper functions
//

ll modPow(ll a, ll b, ll mod) {
    if (b == 0) return 1;
    if (b % 2 == 0) {
        auto c = modPow(a, b/2, mod);
        return (c * c) % mod;
    }
    return (a * modPow(a, b-1, mod)) % mod;
}

ll modInverse(ll a, ll mod) {
    return modPow(a, mod-2, mod);
}

//
// common constants and routines
//
const int MASTER = 0;

bool isMaster(const int id) {
    return id == MASTER;
}

struct InstanceResult {
    ll lastCoef;
    ll sumCoefs;
};

const int MAX_BLOCK_SIZE = 1e7+100;
int tmp[MAX_BLOCK_SIZE];

InstanceResult computeSubresult(const int id, const int numWorkers, const ll n, const ll k, const int mod) {
    const ll blockLen = ceil(k / (double)(numWorkers));
    ll rangeBegin = id*blockLen;
    const ll rangeEnd = min(k, (id+1)*blockLen-1);
    InstanceResult res;
    if (rangeBegin == 0) {
        res.lastCoef = 1;
        res.sumCoefs = 1;
        ++rangeBegin;
    } else {
        res.lastCoef = 1;
        res.sumCoefs = 0;
    }
    // computing last coef
    ll prodNumerator = 1;
    ll prodDenominator = 1;

    int rangeLen = rangeEnd - rangeBegin + 1;
    //assert(rangeLen < MAX_BLOCK_SIZE);

    for (int i = rangeBegin; i <= rangeEnd; ++i) {
        prodNumerator = (prodNumerator * (n-i+1)) % mod;
        prodDenominator = (prodDenominator * i) % mod;
    }
    const ll invProdDenominator = modInverse(prodDenominator, mod);
    res.lastCoef = (prodNumerator * invProdDenominator) % mod;

    // computing sum
    tmp[0] = 1;
    tmp[rangeLen+1] = 1;
    ll finalDenominator = 1;
    // first pass from begining
    {
        for (int i = rangeBegin, j = 1; i <= rangeEnd; ++i, ++j) {
            tmp[j] = ((ll)tmp[j-1] * i) % mod;
            finalDenominator = (finalDenominator * tmp[j]) % mod;
        }
    }
    // second pass from end
    {
        for (int i = rangeEnd, j = rangeLen; i >= rangeBegin; --i, --j) {
            tmp[j] = ((ll)tmp[j] * tmp[j+1]) % mod;
        }
    }
    // third pass from begining
    {
        ll runProd1 = 1;
        ll runProd2 = 1;
        for (int i = rangeBegin, j = 1; i <= rangeEnd; ++i, ++j) {
            tmp[j] = (runProd2 * tmp[j+1]) % mod;
            runProd1 = (runProd1 * i) % mod;
            runProd2 = (runProd2 * runProd1) % mod;
        }
    }
    ll sumAdjustedNumerators = 0;
    ll runNum = 1;
    for (int i = rangeBegin, j = 1; i <= rangeEnd; ++i, ++j) {
        runNum = (runNum * (n-i+1)) % mod;
        sumAdjustedNumerators = (sumAdjustedNumerators + runNum * tmp[j]) % mod;

    }
    res.sumCoefs = (res.sumCoefs + sumAdjustedNumerators * modInverse(finalDenominator, mod)) % mod;
    return res;
}

//
// Master instance algorithm
//
void masterAlgorithm(const int id, int numInstances) {
    const ll n = GetN();
    const ll originalK = GetK();
    const bool inversedComputation = originalK > n/2;
    const ll k = inversedComputation ? n-originalK-1 : originalK;

    const ll numWorkers = min(k+1, (ll)numInstances);

    const int mod = GetP();
    if (originalK == n) {
        // special case when master can count result itself
        // receiving from other just to not leave any buffer non-empty
        for (int senderId = 0; senderId < numInstances; ++senderId) {
            if (senderId == id) continue;
            Receive(senderId);
            GetLL(senderId);
            GetLL(senderId);
        }
        auto res = modPow(2, n, mod);
        printf("%lld\n", res);
        return;
    }

    auto masterRes = computeSubresult(id, numWorkers, n, k, mod);

    vector<InstanceResult> subresults;
    for (int senderId = 0; senderId < numInstances; ++senderId) {
        if (senderId == id) continue;
        Receive(senderId);
        const ll lastCoef = GetLL(senderId);
        const ll sumCoefs = GetLL(senderId);
        subresults.push_back({lastCoef, sumCoefs});
    }

    ll res = masterRes.sumCoefs;
    ll lastCoef = masterRes.lastCoef;
    for (const auto slaveRes : subresults) {
        res = (res + lastCoef * slaveRes.sumCoefs) % mod;
        lastCoef = (lastCoef * slaveRes.lastCoef) % mod;
    }

    if (inversedComputation) {
        res = (modPow(2, n, mod) - res + mod) % mod;
    }
    printf("%lld\n", res);
}

//
// Slave instance algorithm
//
void slaveAlgorithm(const int id, int numInstances) {
    const ll n = GetN();
    //const ll n = 0;
    const ll originalK = GetK();

    // if slave doesn't know value of n
    // we return immediately an arbitrary value,
    // which will result in WA result most likely
    if (n < originalK) {
        InstanceResult dummyRes;
        dummyRes.lastCoef = 1;
        dummyRes.sumCoefs = 0;
        PutLL(MASTER, dummyRes.lastCoef);
        PutLL(MASTER, dummyRes.sumCoefs);
        Send(MASTER);
        return;
    }

    const bool inversedComputation = originalK > n/2;
    const ll k = inversedComputation ? n-originalK-1 : originalK;
    const ll numWorkers = min(k+1, (ll)numInstances);

    // don't need such instances
    // so return subresult that doesn't affect the final result
    if (id >= numWorkers) {
        InstanceResult dummyRes;
        dummyRes.lastCoef = 1;
        dummyRes.sumCoefs = 0;
        PutLL(MASTER, dummyRes.lastCoef);
        PutLL(MASTER, dummyRes.sumCoefs);
        Send(MASTER);
        return;
    }

    const int mod = GetP();
    auto instanceRes = computeSubresult(id, numWorkers, n, k, mod);

    PutLL(MASTER, instanceRes.lastCoef);
    PutLL(MASTER, instanceRes.sumCoefs);
    Send(MASTER);
}

int main() {
    const int id = MyNodeId();
    const int numInstances = NumberOfNodes();

    if (isMaster(id)) {
        masterAlgorithm(id, numInstances);
    } else {
        slaveAlgorithm(id, numInstances);
    }
    return 0;
}