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

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
template < typename _T > inline void _DBG(const char *s, _T x) { cerr << s << " = " << x << "\n"; }
template < typename _T, typename... args > void _DBG(const char *s, _T x, args... a) { while(*s != ',') cerr << *s++; cerr << " = " << x << ','; _DBG(s + 1, a...); }

#ifdef LOCAL
#define DBG(...) DBG(#__VA_ARGS_, _VA_ARGS_)
#define __gcd gcd
#else
#define DBG(...) (_VA_ARGS_)
#define cerr if(0) cout
#endif

// ******************** CODE ******************** //

int id;
LL k, p, n, mul1, sm, a, b, c1, c2;

LL fast(LL x, LL y)
{
    if(y == 0) return 1;
    if(y & 1) return x * fast(x, y - 1) % p;
    return fast(x * x % p, y / 2);
}

const int N = 1e7 + 7;

LL memo[N];
LL revs[N];

int main()
{
    id = MyNodeId() - 1;
    k = GetK();
    p = GetP();
    if(id != -1)
    {
        LL d = (p + NumberOfNodes() - 1) / NumberOfNodes();
        a = k + id * d, b = k + (id + 1) * d;
        LL inv = fast(2, p - 2), base = fast(inv, a);
        c2 = 1, c1 = base;
        memo[0] = c1;
        revs[b - a] = 1;
        for(LL i = a + 1; i <= b; i++)
        {
        	revs[b - a] = (revs[b - a] * (i - k)) % p;
        }
        revs[b - a] = fast(revs[b - a], p - 2);
        for(LL i = b - 1; i >= a + 1; i--)
        {
        	revs[i - a] = revs[i + 1 - a] * (i - k + 1) % p;
        }
        //assert(revs[1] == fast(a + 1 - k, p - 2));
        for(LL i = a + 1; i < b; i++)
        {
            c2 = c2 * i % p;
            base = base * inv % p;
            c1 = (c1 + c2 * base % p * revs[i - a]) % p;
            memo[i - a] = c1;
        }
        c2 = c2 * b % p;
        c2 = c2 * revs[b - a] % p;
    }
    else
    {
        n = GetN();
        PutLL(1, n);
        PutLL(1, 1);
        PutLL(1, 0);
        Send(1);
    }
    int non = NumberOfNodes();
    for(int ins = 1; ins < non; ins++)
    {
        if(id + 1 == ins)
        {
            int rec = Receive(ins - 1);
            int des = (ins + 1) % non;
            n = GetLL(rec);
            mul1 = GetLL(rec);
            sm = GetLL(rec);
            if(a <= n - 1 && n - 1 < b)
            {
                PutLL(des, n);
                PutLL(des, mul1 * c2 % p);
                LL add = memo[n - 1 - a] * mul1 % p;
                sm = (sm + add) % p;
                PutLL(des, sm);
                Send(des);
            }
            else if(n - 1 < a)
            {
                PutLL(des, n);
                PutLL(des, mul1 * c2 % p);
                PutLL(des, sm);
                Send(des);
            }
            else
            {
                PutLL(des, n);
                PutLL(des, mul1 * c2 % p);
                LL add = mul1 * c1 % p;
                sm = (sm + add) % p;
                PutLL(des, sm);
                Send(des);
            }
        }
    }
    if(id == -1)
    {
        int rec = Receive(non - 1);
        LL ans = GetLL(rec);
        ans = GetLL(rec);
        ans = GetLL(rec);

        LL res = fast(2, n - 1);
        res = (res * 2 - res * ans) % p;
        res = (res % p + p) % p;
        cout << res << "\n";
    }
	return 0;
}