// Artur Kraska, II UWr #include "message.h" #include "futbol.h" #include "bits/stdc++.h" /* int GetN() { return int(1e8); } int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; } */ #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #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 MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 #define EPS 1e-12 #define wekt(a, b, p) ((b.x-a.x)*(p.y-a.y)-(b.y-a.y)*(p.x-a.x)) using namespace std; int ID, N; long long n, k, p, pocz, kon; int check; pair <long long, long long> euklid(long long a, long long b) { if(b == 0) return make_pair(1, 0); pair <long long, long long> p = euklid(b, a%b); return make_pair(p.second, p.first - p.second * (a/b)); } long long odwr(long long a, long long mod) { long long e = euklid(a, mod).first; return (e < 0 ? mod-((-e) % mod) : (e % mod)); } int main() { ID = MyNodeId(); N = NumberOfNodes(); N--; n = GetN(); k = GetK(); p = GetP(); pocz = ID*(k+1)/N; kon = (ID+1)*(k+1)/N-1; // wywalic /*{ if(ID != 0) n = 0; }*/ if(ID == N) { PutLL(0, n); Send(0); return 0; } if(ID == 0) { Receive(N); check = GetLL(N); if(check == 0) { pocz = 0; kon = k; } } else { if(n == 0) return 0; } long long res = 0; if(pocz <= kon) { long long dw = 1, l = 1, m = 1; FOR(i, 1, pocz) { l = l*(n+1-i)%p; m = m*i%p; } dw = l*odwr(m, p) % p; // cout << " dodaje " << dw << endl; res += dw; FOR(i, pocz+1, kon) { dw = dw*(n+1-i) % p * odwr(i, p) % p; // cout << " dodaje " << dw << endl; res += dw; } } if(ID != 0) { // cout << " wysylam " << res << endl; PutLL(0, res); Send(0); } else { if(check != 0) { FOR(i, 1, N-1) { Receive(i); res += GetLL(i); } } cout << res%p << endl; } 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 | // Artur Kraska, II UWr #include "message.h" #include "futbol.h" #include "bits/stdc++.h" /* int GetN() { return int(1e8); } int GetElement(int i) { return (i * 1ll * i) % int(1e6) + 1; } */ #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #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 MP make_pair #define PB push_back #define deb(X) X; #define M 1000000007 #define INF 1000000007 #define EPS 1e-12 #define wekt(a, b, p) ((b.x-a.x)*(p.y-a.y)-(b.y-a.y)*(p.x-a.x)) using namespace std; int ID, N; long long n, k, p, pocz, kon; int check; pair <long long, long long> euklid(long long a, long long b) { if(b == 0) return make_pair(1, 0); pair <long long, long long> p = euklid(b, a%b); return make_pair(p.second, p.first - p.second * (a/b)); } long long odwr(long long a, long long mod) { long long e = euklid(a, mod).first; return (e < 0 ? mod-((-e) % mod) : (e % mod)); } int main() { ID = MyNodeId(); N = NumberOfNodes(); N--; n = GetN(); k = GetK(); p = GetP(); pocz = ID*(k+1)/N; kon = (ID+1)*(k+1)/N-1; // wywalic /*{ if(ID != 0) n = 0; }*/ if(ID == N) { PutLL(0, n); Send(0); return 0; } if(ID == 0) { Receive(N); check = GetLL(N); if(check == 0) { pocz = 0; kon = k; } } else { if(n == 0) return 0; } long long res = 0; if(pocz <= kon) { long long dw = 1, l = 1, m = 1; FOR(i, 1, pocz) { l = l*(n+1-i)%p; m = m*i%p; } dw = l*odwr(m, p) % p; // cout << " dodaje " << dw << endl; res += dw; FOR(i, pocz+1, kon) { dw = dw*(n+1-i) % p * odwr(i, p) % p; // cout << " dodaje " << dw << endl; res += dw; } } if(ID != 0) { // cout << " wysylam " << res << endl; PutLL(0, res); Send(0); } else { if(check != 0) { FOR(i, 1, N-1) { Receive(i); res += GetLL(i); } } cout << res%p << endl; } return 0; } |