#include "bits/stdc++.h" #include "futbol.h" #include "message.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif int topdiv(int n, int k){ return (n + k - 1) / k; } int NWDR(int a, int b, LL &xp, LL &yp) { if (a==0) { xp=0; yp=1; return b; } LL x,y; int d = NWDR(b%a, a, x, y); yp = x; xp = y-(b/a)*x; return d; } int RevMod(int a, int p){ LL x, y; NWDR(a, p, x, y); if (x < 0LL) return (p - ((-x) % (LL)p)) % p; return x % (LL)p; } const int n = GetN(); const int p = GetP(); const int k = GetK(); void tup(int & x){ if(x >= p) x -= p; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int nodes = NumberOfNodes(); int id = MyNodeId(); if(k <= 1e7){ nodes = 1; if(id != 0){ return 0; } } int block_size = topdiv(k + 1, nodes); int curSum = 0; int curEl = 1; int block_start = block_size * id; int block_end = block_size * (id + 1); mini(block_end, k + 1); VI revFact; revFact.reserve(block_size + 1); int fact = 1; for(int i = block_start + 1; i < block_end + 1; i++){ fact *= i; fact %= p; } fact = RevMod(fact, p); for(int i = block_end; i >= block_start + 1; i--){ revFact.PB(fact); fact *= i; fact %= p; } revFact.PB(fact); reverse(ALL(revFact)); /*for(int a : revFact){ cerr<<RevMod(a,p)<<" "; } cerr<<endl; debug(revFact);*/ for(int i = block_start; i < block_end; i++){ debug(curEl); curSum += curEl * revFact[i - block_start]; curSum %= p; debug("mnoze", n - (i + 1) + 1); curEl *= n - (i + 1) + 1; curEl %= p; debug(curSum); } curEl *= revFact[block_end - block_start]; curEl %= p; if(id != 0){ Receive(id - 1); int prevEl = GetLL(id - 1); int prevSum = GetLL(id - 1); curEl *= prevEl; curEl %= p; curSum *= prevEl; curSum %= p; curSum += prevSum; tup(curSum); } if(id != nodes - 1){ PutLL(id + 1, curEl); PutLL(id + 1, curSum); Send(id + 1); } else{ cout<<curSum<<"\n"; } }
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 147 148 149 150 151 152 153 154 | #include "bits/stdc++.h" #include "futbol.h" #include "message.h" using namespace std; #define PB push_back #define LL long long #define int LL #define FOR(i,a,b) for (int i = (a); i <= (b); i++) #define FORD(i,a,b) for (int i = (a); i >= (b); i--) #define REP(i,n) FOR(i,0,(int)(n)-1) #define st first #define nd second #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((int)(x).size()) #define VI vector<int> #define PII pair<int,int> #define LD long double template<class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template<class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";} template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...); } template<class T> ostream &operator<<(ostream &os, vector<T> V){ os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]"; } template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) { return os << "(" << P.st << "," << P.nd << ")"; } #ifdef LOCAL #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define debug(...) (__VA_ARGS__) #define cerr if(0)cout #endif int topdiv(int n, int k){ return (n + k - 1) / k; } int NWDR(int a, int b, LL &xp, LL &yp) { if (a==0) { xp=0; yp=1; return b; } LL x,y; int d = NWDR(b%a, a, x, y); yp = x; xp = y-(b/a)*x; return d; } int RevMod(int a, int p){ LL x, y; NWDR(a, p, x, y); if (x < 0LL) return (p - ((-x) % (LL)p)) % p; return x % (LL)p; } const int n = GetN(); const int p = GetP(); const int k = GetK(); void tup(int & x){ if(x >= p) x -= p; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int nodes = NumberOfNodes(); int id = MyNodeId(); if(k <= 1e7){ nodes = 1; if(id != 0){ return 0; } } int block_size = topdiv(k + 1, nodes); int curSum = 0; int curEl = 1; int block_start = block_size * id; int block_end = block_size * (id + 1); mini(block_end, k + 1); VI revFact; revFact.reserve(block_size + 1); int fact = 1; for(int i = block_start + 1; i < block_end + 1; i++){ fact *= i; fact %= p; } fact = RevMod(fact, p); for(int i = block_end; i >= block_start + 1; i--){ revFact.PB(fact); fact *= i; fact %= p; } revFact.PB(fact); reverse(ALL(revFact)); /*for(int a : revFact){ cerr<<RevMod(a,p)<<" "; } cerr<<endl; debug(revFact);*/ for(int i = block_start; i < block_end; i++){ debug(curEl); curSum += curEl * revFact[i - block_start]; curSum %= p; debug("mnoze", n - (i + 1) + 1); curEl *= n - (i + 1) + 1; curEl %= p; debug(curSum); } curEl *= revFact[block_end - block_start]; curEl %= p; if(id != 0){ Receive(id - 1); int prevEl = GetLL(id - 1); int prevSum = GetLL(id - 1); curEl *= prevEl; curEl %= p; curSum *= prevEl; curSum %= p; curSum += prevSum; tup(curSum); } if(id != nodes - 1){ PutLL(id + 1, curEl); PutLL(id + 1, curSum); Send(id + 1); } else{ cout<<curSum<<"\n"; } } |