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
// Jenda instancja wypisuje (n * k) mod p.

#include "message.h"
#include "futbol.h"
#include "bits/stdc++.h"

using namespace std;


#define I long long

//#define MAX
I p,n,k,i2;

I pow(I a, I b){
    I x=1, y=a;
    while (b > 0) {
        if (b%2 == 1) x = (x*y) % p;
        y = (y*y) % p;
        b /= 2;
    }
    return x;
}

I gcd(I a, I b, I &x, I &y) {
    if(b == 0) {
       x = 1;
       y = 0;
       return a;
    }
    I x1, y1;
    I r = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - (a / b) * y1;
    return r;
}

I inv(I a) {
    I x, y;
    int g = gcd(a, p, x, y);
    return (x%p + p) % p;
}

#define MAX 20000000
//#define MAX 20 000 000
#define D(x)

int gora[MAX+10];
int dol[MAX+10];
int suma[MAX+10];

void print(const char * name, int *v,I x = 1){
    cout << name << ":";
    for(int i=0;i<MAX;i++) {
        cout << ((v[i]*x)%p) << " ";
    }
    cout << "\n";
}


int main() {
    int we = NumberOfNodes();
    int me = MyNodeId();
    n = GetN(), k = GetK(), p = GetP();

    int blok = (p-k-1+we-1)/we;
    if(blok>=MAX) blok = (n-k+we-1)/we;
    int a = k+1+blok*me;
    int b = k+1+blok*(me+1);

D(    cout << "blok:" << blok << " a:" << a << " b:" << b << "\n");

    dol[0]=1;//b-k-1;//+me*blok;
    gora[0]=k+1 + me*blok;
    for(int i=1;i<blok;i++)   gora[i]=(gora[i-1]*(a+i))%p;
    for(int i=1;i<blok+1;i++) dol[i] =(dol [i-1]*(b-i-k))%p;
//    return 0;
    I i2 = inv(2);
    I m = inv(dol[blok]);
    I s,sm=0;
    I  n_2_k_2=pow(2,n-k-2);

    for(int i=0;i<blok;i++) {
        s=(gora[i]*(m*dol[blok-i-1])%p)%p;
        D(cout << i << " s: " << ((s*n_2_k_2)%p) << " b:" << gora[i] << "*"<<  dol[blok-i-1] << "/" <<dol[blok] << "\n");//((s*n_2_k_2)%p) << "\n";
        m=(m*i2)%p;
        sm= (s+sm)%p;
        suma[i]=sm;
    }

    D(
        print("gora",gora);
        print("dol ",dol);
        print("suma",suma,4);//n_2_k_2);
    )

    int pos, idx=blok-1;
    if(me == 0)  {
        pos = n-2-k;
        if (pos < blok) idx = pos;
        sm = suma[idx];
    }
    m=(gora[blok-1]*(m)%p)%p;
    D(cout << "m:" << (m*2)%p << "\n");
//    m=(gora[blok-1]*m)%p;

    if(we > 1) {
        if (me == 0) {
            PutLL(1, pos-blok);
            PutLL(1, sm);
            PutLL(1, m);
            Send(1);

            Receive(we-1);
            pos = GetLL(we-1);
            sm = GetLL(we-1);
            m = GetLL(we-1);
        } else {
            Receive(me-1);
            pos = GetLL(me-1);
            sm = GetLL(me-1);
            I m2 = GetLL(me-1);

            if (pos < blok) idx = pos;
            if (idx>=0) sm=(sm+(m2*suma[idx])%p)%p;
            D(cout << "mul:" << m2 << "\n");
            D(print("nowa suma",suma,n_2_k_2*m2));

            PutLL((me+1)%we, pos-blok);
            PutLL((me+1)%we, sm);
            PutLL((me+1)%we, (m*m2)%p);
            Send((me+1)%we);
        }
    }

    if(me == 0) {
        I  n_2=pow(2,n);
        sm = (sm*n_2_k_2)%p;
        sm = (sm+n_2_k_2*2)%p;
        sm = (((n_2 - sm) %p)+p)%p;
        if(n-k == 1) {
            sm--;
        }
        cout << sm << "\n";
    }
}