#include "message.h" #include "futbol.h" #include "bits/stdc++.h" using namespace std; const int Nodes = 100; const int N = 4e7 + 7; int MOD; int n, k, id; int val[N]; int res[Nodes + 10]; int sLeft[Nodes + 10], sRight[Nodes + 10]; int fast(int a, int b){ if(b == 0) return 1; int x = fast(a, b / 2); if(b & 1) return (((1LL * x * x)%MOD) * a)%MOD; return (1LL * x * x)%MOD; } int get(int from, int to){ int ret = 1; for(int i = from; i <= to; ++i) ret = (1LL * i * ret)%MOD; return ret; } void brut(){ int sil = 1; for(int i = 2; i <= n; ++i) sil = (1LL * sil * i)%MOD; int ans = 0; int odw = fast(sil, MOD - 2); val[k] = fast(get(2, k), MOD - 2); for(int i = k - 1; i >= 0; --i) val[i] = (1LL * val[i + 1] * (i + 1))%MOD; int Right = odw; for(int i = 0; i <= k; ++i){ ans = (ans + 1LL * val[i] * Right)%MOD; Right = (1LL * Right * (n - i))%MOD; } ans = (1LL * ans * sil)%MOD; printf("%d\n", ans); } int main(){ n = GetN(); k = GetK(); MOD = GetP(); id = MyNodeId(); if(n == 0){ PutInt(0, -1); Send(0); return 0; } if(n <= 3000){ if(id != 0) return 0; brut(); return 0; } int sz = n / Nodes; int from = max(sz * id, 1), to = sz * (id + 1) - 1; if(id == Nodes - 1) to = max(to, n); int sil = get(from, to); int rv = get(max(n - to, 1), n - from); val[to - from] = fast(sil, MOD - 2); for(int i = to - from - 1; i >= 0; --i) val[i] = (1LL * val[i + 1] * (i + from + 1))%MOD; int Right = fast(rv, MOD - 2); int ans = 0; for(int i = 0; i <= to - from; ++i){ if(i + from > k) break; ans = (ans + 1LL * val[i] * Right)%MOD; Right = (1LL * Right * (n - from - i))%MOD; } if(id != 0){ PutInt(0, sil); PutInt(0, rv); PutInt(0, ans); Send(0); return 0; } res[0] = ans; sLeft[0] = sil; sRight[0] = rv; for(int i = 1; i < Nodes; ++i){ Receive(i); sLeft[i] = GetInt(i); if(sLeft[i] == -1){ brut(); return 0; } sRight[i] = GetInt(i); res[i] = GetInt(i); } int cur = 1; for(int i = 0; i < Nodes; ++i){ res[i] = (1LL * res[i] * fast(cur, MOD - 2))%MOD; cur = (1LL * cur * sLeft[i])%MOD; } cur = 1; for(int i = Nodes - 1; i >= 0; --i){ res[i] = (1LL * res[i] * fast(cur, MOD - 2))%MOD; cur = (1LL * cur * sRight[i])%MOD; } ans = 0; for(int i = 0; i < Nodes; ++i) ans = (ans + res[i])%MOD; int all = 1; for(int i = 0; i < Nodes; ++i) all = (1LL * all * sLeft[i])%MOD; printf("%lld\n", (1LL * ans * all + 1)%MOD); 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 137 138 139 140 141 142 143 144 145 | #include "message.h" #include "futbol.h" #include "bits/stdc++.h" using namespace std; const int Nodes = 100; const int N = 4e7 + 7; int MOD; int n, k, id; int val[N]; int res[Nodes + 10]; int sLeft[Nodes + 10], sRight[Nodes + 10]; int fast(int a, int b){ if(b == 0) return 1; int x = fast(a, b / 2); if(b & 1) return (((1LL * x * x)%MOD) * a)%MOD; return (1LL * x * x)%MOD; } int get(int from, int to){ int ret = 1; for(int i = from; i <= to; ++i) ret = (1LL * i * ret)%MOD; return ret; } void brut(){ int sil = 1; for(int i = 2; i <= n; ++i) sil = (1LL * sil * i)%MOD; int ans = 0; int odw = fast(sil, MOD - 2); val[k] = fast(get(2, k), MOD - 2); for(int i = k - 1; i >= 0; --i) val[i] = (1LL * val[i + 1] * (i + 1))%MOD; int Right = odw; for(int i = 0; i <= k; ++i){ ans = (ans + 1LL * val[i] * Right)%MOD; Right = (1LL * Right * (n - i))%MOD; } ans = (1LL * ans * sil)%MOD; printf("%d\n", ans); } int main(){ n = GetN(); k = GetK(); MOD = GetP(); id = MyNodeId(); if(n == 0){ PutInt(0, -1); Send(0); return 0; } if(n <= 3000){ if(id != 0) return 0; brut(); return 0; } int sz = n / Nodes; int from = max(sz * id, 1), to = sz * (id + 1) - 1; if(id == Nodes - 1) to = max(to, n); int sil = get(from, to); int rv = get(max(n - to, 1), n - from); val[to - from] = fast(sil, MOD - 2); for(int i = to - from - 1; i >= 0; --i) val[i] = (1LL * val[i + 1] * (i + from + 1))%MOD; int Right = fast(rv, MOD - 2); int ans = 0; for(int i = 0; i <= to - from; ++i){ if(i + from > k) break; ans = (ans + 1LL * val[i] * Right)%MOD; Right = (1LL * Right * (n - from - i))%MOD; } if(id != 0){ PutInt(0, sil); PutInt(0, rv); PutInt(0, ans); Send(0); return 0; } res[0] = ans; sLeft[0] = sil; sRight[0] = rv; for(int i = 1; i < Nodes; ++i){ Receive(i); sLeft[i] = GetInt(i); if(sLeft[i] == -1){ brut(); return 0; } sRight[i] = GetInt(i); res[i] = GetInt(i); } int cur = 1; for(int i = 0; i < Nodes; ++i){ res[i] = (1LL * res[i] * fast(cur, MOD - 2))%MOD; cur = (1LL * cur * sLeft[i])%MOD; } cur = 1; for(int i = Nodes - 1; i >= 0; --i){ res[i] = (1LL * res[i] * fast(cur, MOD - 2))%MOD; cur = (1LL * cur * sRight[i])%MOD; } ans = 0; for(int i = 0; i < Nodes; ++i) ans = (ans + res[i])%MOD; int all = 1; for(int i = 0; i < Nodes; ++i) all = (1LL * all * sLeft[i])%MOD; printf("%lld\n", (1LL * ans * all + 1)%MOD); return 0; } |