#include <iostream>
#include <cstdio>
#include "message.h"
#include "futbol.h"
using namespace std;
const int L = 1e7;
int n, k, p;
long long pot(long long a, long long b)
{
if(b==0) return 1;
long long w = pot(a,b/2);
w = w*w%p;
if(b%2) w=w*a%p;
return w;
}
long long odw(long long a)
{
return pot(a, p - 2);
}
int main()
{
n = GetN();
k = GetK();
p = GetP();
int nr = MyNodeId();
int l = nr * L, r = nr * L + L - 1;
r = min(k, r);
long long ans = 1;
long long sum = 0;
if(l==0)
{
l++;
sum = 1;
}
for(int i=l;i<=r;i++)
{
ans *= (n - i + 1);
ans %= p;
ans *= odw(i);
ans %= p;
sum += ans;
sum %= p;
}
if(nr!=0)
{
Receive(nr - 1);
long long mn = GetLL(nr - 1);
ans *= mn;
sum *= mn;
sum += GetLL(nr - 1);
ans%=p;
sum%=p;
}
int next = (nr + 1) % 100;
PutLL(next, ans);
PutLL(next, sum);
Send(next);
if(nr==0)
{
Receive(99);
GetLL(99);
printf("%lld", GetLL(99));
}
return 0;
}
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 | #include <iostream> #include <cstdio> #include "message.h" #include "futbol.h" using namespace std; const int L = 1e7; int n, k, p; long long pot(long long a, long long b) { if(b==0) return 1; long long w = pot(a,b/2); w = w*w%p; if(b%2) w=w*a%p; return w; } long long odw(long long a) { return pot(a, p - 2); } int main() { n = GetN(); k = GetK(); p = GetP(); int nr = MyNodeId(); int l = nr * L, r = nr * L + L - 1; r = min(k, r); long long ans = 1; long long sum = 0; if(l==0) { l++; sum = 1; } for(int i=l;i<=r;i++) { ans *= (n - i + 1); ans %= p; ans *= odw(i); ans %= p; sum += ans; sum %= p; } if(nr!=0) { Receive(nr - 1); long long mn = GetLL(nr - 1); ans *= mn; sum *= mn; sum += GetLL(nr - 1); ans%=p; sum%=p; } int next = (nr + 1) % 100; PutLL(next, ans); PutLL(next, sum); Send(next); if(nr==0) { Receive(99); GetLL(99); printf("%lld", GetLL(99)); } return 0; } |
English