#include <bits/stdc++.h> #include "message.h" #include "futbol.h" using namespace std; typedef long long int lli; const int MAXN = 205; const int MAXD = 10000100; lli MOD; // lli pot(lli a, lli n) { // if (n == 0) // return 1; // // if (n%2 == 0) // return pot((a*a)%MOD, n/2); // else // return (a*pot(a, n-1))%MOD; // } lli pot(lli a, lli n) { lli res = 1; while (n > 0) { if (n&1) res = (res*a)%MOD; n >>= 1; a = (a*a)%MOD; } return res; } lli rev(lli a) { return pot(a, MOD-2); } lli bins[MAXN]; lli sums[MAXN]; lli prods[MAXD][2]; lli glRew; lli st, nd; lli rev_fast(lli a) { return (((glRew*prods[a-st][0])%MOD)*prods[nd-a-1][1])%MOD; } int main() { int num = MyNodeId(); int allcnt = NumberOfNodes(); lli n = GetN(); lli k = GetK(); MOD = GetP(); // lli n = 650400662, k = 572680295; // MOD = 769490011; st = ((k+1)*num)/allcnt; nd = ((k+1)*(num+1))/allcnt; lli bin = 1; lli s = 0; glRew = 1; for (int i=st; i<nd; i++) { if (i != 0) glRew = (glRew*i)%MOD; } glRew = rev(glRew); prods[0][0] = 1; prods[0][1] = 1; for (int i=0; i<nd-st; i++) { if (st+i == 0) prods[i+1][0] = 1; else prods[i+1][0] = (prods[i][0]*(st+i))%MOD; if (nd-i-1 == 0) prods[i+1][1] = prods[i][1]; else prods[i+1][1] = (prods[i][1]*(nd-i-1))%MOD; } for (lli x=st; x<nd; x++) { if (x == 0) { s += 1; continue; } bin = (bin*(n-x+1))%MOD; bin = (bin*rev_fast(x))%MOD; s = (s+bin)%MOD; } if (num != 0) { PutLL(0, bin); PutLL(0, s); Send(0); return 0; } bins[0] = bin; sums[0] = s; for (int i=1; i<allcnt; i++) { int fr = Receive(-1); bins[fr] = GetLL(fr); sums[fr] = GetLL(fr); } // for (int i=0; i<allcnt; i++) { // printf("%d: %lld %lld\n", i, bins[i], sums[i]); // } bin = 1; s = 0; for (int i=0; i<allcnt; i++) { s = (s+sums[i]*bin)%MOD; bin = (bin*bins[i])%MOD; } printf("%lld\n", s); 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 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 | #include <bits/stdc++.h> #include "message.h" #include "futbol.h" using namespace std; typedef long long int lli; const int MAXN = 205; const int MAXD = 10000100; lli MOD; // lli pot(lli a, lli n) { // if (n == 0) // return 1; // // if (n%2 == 0) // return pot((a*a)%MOD, n/2); // else // return (a*pot(a, n-1))%MOD; // } lli pot(lli a, lli n) { lli res = 1; while (n > 0) { if (n&1) res = (res*a)%MOD; n >>= 1; a = (a*a)%MOD; } return res; } lli rev(lli a) { return pot(a, MOD-2); } lli bins[MAXN]; lli sums[MAXN]; lli prods[MAXD][2]; lli glRew; lli st, nd; lli rev_fast(lli a) { return (((glRew*prods[a-st][0])%MOD)*prods[nd-a-1][1])%MOD; } int main() { int num = MyNodeId(); int allcnt = NumberOfNodes(); lli n = GetN(); lli k = GetK(); MOD = GetP(); // lli n = 650400662, k = 572680295; // MOD = 769490011; st = ((k+1)*num)/allcnt; nd = ((k+1)*(num+1))/allcnt; lli bin = 1; lli s = 0; glRew = 1; for (int i=st; i<nd; i++) { if (i != 0) glRew = (glRew*i)%MOD; } glRew = rev(glRew); prods[0][0] = 1; prods[0][1] = 1; for (int i=0; i<nd-st; i++) { if (st+i == 0) prods[i+1][0] = 1; else prods[i+1][0] = (prods[i][0]*(st+i))%MOD; if (nd-i-1 == 0) prods[i+1][1] = prods[i][1]; else prods[i+1][1] = (prods[i][1]*(nd-i-1))%MOD; } for (lli x=st; x<nd; x++) { if (x == 0) { s += 1; continue; } bin = (bin*(n-x+1))%MOD; bin = (bin*rev_fast(x))%MOD; s = (s+bin)%MOD; } if (num != 0) { PutLL(0, bin); PutLL(0, s); Send(0); return 0; } bins[0] = bin; sums[0] = s; for (int i=1; i<allcnt; i++) { int fr = Receive(-1); bins[fr] = GetLL(fr); sums[fr] = GetLL(fr); } // for (int i=0; i<allcnt; i++) { // printf("%d: %lld %lld\n", i, bins[i], sums[i]); // } bin = 1; s = 0; for (int i=0; i<allcnt; i++) { s = (s+sums[i]*bin)%MOD; bin = (bin*bins[i])%MOD; } printf("%lld\n", s); return 0; } |