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
// just brute on instance 0
#include "message.h"
#include "futbol.h"
#include "bits/stdc++.h"

using namespace std;

int pow(int x, unsigned int y, int p) {
    int res = 1;
    x = x % p;
    while (y > 0) {
        if (y & 1)
            res = res * int64_t(x) % p;
        y /= 2;
        x = x * int64_t(x) % p;
    }
    return res;
}

uint32_t ps_ncr(uint32_t n, uint32_t k, uint32_t p) {
    if (k == n) {
        return pow(2, n, p);
    }
    if (2 * k + 1 == n) {
        return pow(2, n - 1, p);
    }
    if (2 * k > n) {
        return (pow(2, n, p) - ps_ncr(n, n - k - 1, p) + p) % p;
    }
    uint64_t num = 1;
    uint64_t den = 1;
    uint64_t res = 1;
    for (int i = 0; i < k; i++) {
        num = (num * (n - i)) % p;
        den = (den * (i + 1)) % p;
        res = (res + num * pow(den, p - 2, p)) % p;
    }
    return res;
}

int main() {
    if(MyNodeId() != 0) return 0;
    int n = GetN(), k = GetK(), p = GetP();
    //int n = 440581, k = 133004, p = 144495917;
    //int n = 10, k=3, p=13;
    cout << ps_ncr(n, k, p) << endl;
}