#ifndef MW_HEADER #define MW_HEADER #include <bits/stdc++.h> #include "message.h" using namespace std; #define FOR(i,a,b) for (LL i = (a); i < (b); ++i) #define FORD(i,b,a) for (LL i = (LL)(b)-1; i >= (a); --i) #define REP(i,N) FOR(i,0,N) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define st first #define nd second #define pb push_back typedef pair<int, int> PII; typedef long long LL; typedef unsigned long long ULL; #endif LL NODES = NumberOfNodes(), ME = MyNodeId(); LL __N, START, END, NEXT, PREV; LL get_start(int node) { return __N/NODES * node + min(__N%NODES, (LL)node); } LL get_end(int node) { return get_start(node+1); } /** Every node has at least factor items to process */ void reduce_nodes(LL N, int factor = 1) { NODES = min(NODES, max(N/factor, 1LL)); if (ME >= NODES) exit(0); NEXT = (ME + 1)%NODES, PREV=(ME+NODES-1)%NODES; __N = N; START = get_start(ME), END = get_end(ME); } template <typename Container> struct is_container : false_type { }; template <typename... Ts> struct is_container<list<Ts...> > : true_type { }; template <typename... Ts> struct is_container<vector<Ts...> > : true_type { }; // template <typename... Ts> struct is_container<set<Ts...> > : true_type { }; // template <typename... Ts> struct is_container<map<Ts...> > : true_type { }; template <typename Container> struct is_pair : false_type { }; template <typename... Ts> struct is_pair<pair<Ts...>> : true_type { }; template <typename Container> struct is_triple : false_type { }; template <typename T1, typename T2, typename T3> struct is_triple<tuple<T1,T2,T3>> : true_type { }; template<typename T> void Put(int target, typename enable_if<!is_class<T>::value, const T&>::type value) { T::not_implemented; } template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec); template<> void Put<bool>(int target, const bool& value) { PutChar(target, value); } template<> void Put<char>(int target, const char& value) { PutChar(target, value); } template<> void Put<int>(int target, const int& value) { PutInt(target, value); } template<> void Put<unsigned int>(int target, const unsigned int& value) { PutInt(target, value); } template<> void Put<long long>(int target, const long long& value) { PutLL(target, value); } template<> void Put<unsigned long long>(int target, const unsigned long long& value) { PutLL(target, value); } // template<typename T> void Put(int target, typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, const T&>::type value){ // char data[sizeof(T)]; memcpy(data, &value, sizeof(T)); // REP(i, (int)sizeof(T)) PutChar(target, data[i]); // } template<typename T> void Put(int target, typename enable_if<is_pair<T>::value, const T&>::type pair) { Put<typename T::first_type>(target, pair.first); Put<typename T::second_type>(target, pair.second); } template<typename T> void Put(int target, typename enable_if<is_triple<T>::value, const T&>::type triple) { Put<typename tuple_element<0, T>::type>(target, get<0>(triple)); Put<typename tuple_element<1, T>::type>(target, get<1>(triple)); Put<typename tuple_element<2, T>::type>(target, get<2>(triple)); } template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec) { Put<int>(target, vec.size()); for (auto v: vec) { Put<typename T::value_type>(target, v); } } template<typename T> typename enable_if<!is_class<T>::value, T>::type Get(int source) { T::not_implemented; } template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source); template<> bool Get<bool>(int source) { return GetChar(source); } template<> char Get<char>(int source) { return GetChar(source); } template<> int Get<int>(int source) { return GetInt(source); } template<> unsigned int Get<unsigned int>(int source) { return GetInt(source); } template<> long long Get<long long>(int source) { return GetLL(source); } template<> unsigned long long Get<unsigned long long>(int source) { return GetLL(source); } // template<typename T> typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, T>::type Get(int source) { // char data[sizeof(T)]; REP(i, (int)sizeof(T)) data[i] = GetChar(source); // T value; memcpy(&value, data, sizeof(T)); // return value; // } template<typename T> typename enable_if<is_pair<T>::value, T>::type Get(int source) { auto f = Get<typename T::first_type>(source); auto s = Get<typename T::second_type>(source); return T(f, s); } template<typename T> typename enable_if<is_triple<T>::value, T>::type Get(int source) { auto f = Get<typename tuple_element<0, T>::type>(source); auto s = Get<typename tuple_element<1, T>::type>(source); auto t = Get<typename tuple_element<2, T>::type>(source); return T(f, s, t); } template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source) { int size = GetInt(source); vector<typename T::value_type> result; result.reserve(size); REP(i,size) result.push_back(Get<typename T::value_type>(source)); return T(result.begin(), result.end()); } template<typename T> void Broadcast(int source, T& value) { if (ME == source) REP(i,NODES) { Put<T>(i, value); Send(i); } Receive(source); value = Get<T>(source); } template<typename T> void BroadcastTree(int source, T& value) { int relative = (ME - source + NODES) % NODES; if (relative) { int from = (source + (relative-1) / 2) % NODES; Receive(from); value = Get<T>(from); } FOR(i,1,3) if (2*relative + i < NODES) { int to = ((source + 2*relative + i) % NODES); Put<T>(to, value); Send(to); } } template<typename T, typename Fn> void Accumulate(int target, const T& value, Fn fn) { Put<T>(target, value); Send(target); if (ME == target) REP(i, NODES) { Receive(i); fn(Get<T>(i)); } } /** ~5ms for adding ints */ template<typename T, typename Fn> T AccumulateTree(int target, T value, Fn fn) { int relative = (ME - target + NODES) % NODES; for (int b = 1; b < NODES; b <<= 1) { if (relative&b) { int to = (target + relative - b + NODES) % NODES; Put<T>(to, value); Send(to); break; } else if (relative + b < NODES) { int from = (target + relative + b) % NODES; Receive(from); value = fn(value, Get<T>(from)); } } return value; } template<typename T, typename Compute, typename Acc> T AccumulateValues(int target, Compute fn, Acc acc) { T result = fn(START); FOR(i,START+1,END) result = acc(result, fn(i)); return AccumulateTree(target, result, acc); } template<typename T, typename Compute> vector<T> AccumulateVector(int target, Compute fn) { vector<T> local; FOR(i,START, END) local.pb(fn(i)); vector<T> acc; Accumulate(target, local, [&](const vector<T>& vec) {acc.insert(acc.end(), vec.begin(), vec.end());}); return acc; } template<typename T> vector<T> Collect(int target, const T& value) { vector<T> acc; Accumulate(target, value, [&](const T& val) { acc.pb(val); }); return acc; } // ^^^ CUT HERE ^^^ #include "futbol.h" const int MOD = GetP(); const int K = GetK(); LL powe(LL a, int b) { LL res = 1; while (b) { if (b&1) (res *= a) %= MOD; (a *= a) %= MOD; b >>= 1; } return res; } LL inv(LL a) { return powe(a, MOD - 2); } LL _data[11111111]; int main() { if (MOD == 2) { if (ME != 0) return 0; printf("%d\n", K == 0 ? 1 : 0); return 0; } reduce_nodes(MOD); LL* data = _data - START; // data[i] = powe(2, M-1-i) * SN[K+i][K] int INV2 = (MOD + 1) / 2; data[START] = 1; LL divi = 1; FOR(i,START+1,END) { data[i] = data[i-1] * (K + i) % MOD * INV2 % MOD; // /i (divi *= i) %= MOD; } divi = inv(divi); FORD(i,END,START+1) { (data[i] *= divi) %= MOD; (divi *= i) %= MOD; } LL next = data[END-1] * (K+END) % MOD * INV2 % MOD * inv(END) % MOD; FOR(i,START+1,END) { (data[i] += data[i-1]) %= MOD; } LL acc = 0; int M = GetN() - K; LL multiplier = M > 0 ? powe(2,M-1) : 1; if (ME != 0) { Receive(PREV); acc = GetLL(PREV); multiplier = GetLL(PREV); M = GetInt(PREV); } if (M >= START + 1) { (acc += data[min(M-1,(int)END-1)] * multiplier) %= MOD; } (multiplier *= next) %= MOD; PutLL(NEXT, acc); PutLL(NEXT, multiplier); PutInt(NEXT, M); Send(NEXT); if (ME == 0) { Receive(PREV); acc = GetLL(PREV); multiplier = GetLL(PREV); M = GetInt(PREV); LL result = (powe(2, K+M) - acc) % MOD; result += MOD; result %= MOD; printf("%lld\n", result); } }
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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | #ifndef MW_HEADER #define MW_HEADER #include <bits/stdc++.h> #include "message.h" using namespace std; #define FOR(i,a,b) for (LL i = (a); i < (b); ++i) #define FORD(i,b,a) for (LL i = (LL)(b)-1; i >= (a); --i) #define REP(i,N) FOR(i,0,N) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define st first #define nd second #define pb push_back typedef pair<int, int> PII; typedef long long LL; typedef unsigned long long ULL; #endif LL NODES = NumberOfNodes(), ME = MyNodeId(); LL __N, START, END, NEXT, PREV; LL get_start(int node) { return __N/NODES * node + min(__N%NODES, (LL)node); } LL get_end(int node) { return get_start(node+1); } /** Every node has at least factor items to process */ void reduce_nodes(LL N, int factor = 1) { NODES = min(NODES, max(N/factor, 1LL)); if (ME >= NODES) exit(0); NEXT = (ME + 1)%NODES, PREV=(ME+NODES-1)%NODES; __N = N; START = get_start(ME), END = get_end(ME); } template <typename Container> struct is_container : false_type { }; template <typename... Ts> struct is_container<list<Ts...> > : true_type { }; template <typename... Ts> struct is_container<vector<Ts...> > : true_type { }; // template <typename... Ts> struct is_container<set<Ts...> > : true_type { }; // template <typename... Ts> struct is_container<map<Ts...> > : true_type { }; template <typename Container> struct is_pair : false_type { }; template <typename... Ts> struct is_pair<pair<Ts...>> : true_type { }; template <typename Container> struct is_triple : false_type { }; template <typename T1, typename T2, typename T3> struct is_triple<tuple<T1,T2,T3>> : true_type { }; template<typename T> void Put(int target, typename enable_if<!is_class<T>::value, const T&>::type value) { T::not_implemented; } template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec); template<> void Put<bool>(int target, const bool& value) { PutChar(target, value); } template<> void Put<char>(int target, const char& value) { PutChar(target, value); } template<> void Put<int>(int target, const int& value) { PutInt(target, value); } template<> void Put<unsigned int>(int target, const unsigned int& value) { PutInt(target, value); } template<> void Put<long long>(int target, const long long& value) { PutLL(target, value); } template<> void Put<unsigned long long>(int target, const unsigned long long& value) { PutLL(target, value); } // template<typename T> void Put(int target, typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, const T&>::type value){ // char data[sizeof(T)]; memcpy(data, &value, sizeof(T)); // REP(i, (int)sizeof(T)) PutChar(target, data[i]); // } template<typename T> void Put(int target, typename enable_if<is_pair<T>::value, const T&>::type pair) { Put<typename T::first_type>(target, pair.first); Put<typename T::second_type>(target, pair.second); } template<typename T> void Put(int target, typename enable_if<is_triple<T>::value, const T&>::type triple) { Put<typename tuple_element<0, T>::type>(target, get<0>(triple)); Put<typename tuple_element<1, T>::type>(target, get<1>(triple)); Put<typename tuple_element<2, T>::type>(target, get<2>(triple)); } template<typename T> void Put(int target, typename enable_if<is_container<T>::value, const T&>::type vec) { Put<int>(target, vec.size()); for (auto v: vec) { Put<typename T::value_type>(target, v); } } template<typename T> typename enable_if<!is_class<T>::value, T>::type Get(int source) { T::not_implemented; } template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source); template<> bool Get<bool>(int source) { return GetChar(source); } template<> char Get<char>(int source) { return GetChar(source); } template<> int Get<int>(int source) { return GetInt(source); } template<> unsigned int Get<unsigned int>(int source) { return GetInt(source); } template<> long long Get<long long>(int source) { return GetLL(source); } template<> unsigned long long Get<unsigned long long>(int source) { return GetLL(source); } // template<typename T> typename enable_if<is_class<T>::value && !is_container<T>::value && !is_pair<T>::value, T>::type Get(int source) { // char data[sizeof(T)]; REP(i, (int)sizeof(T)) data[i] = GetChar(source); // T value; memcpy(&value, data, sizeof(T)); // return value; // } template<typename T> typename enable_if<is_pair<T>::value, T>::type Get(int source) { auto f = Get<typename T::first_type>(source); auto s = Get<typename T::second_type>(source); return T(f, s); } template<typename T> typename enable_if<is_triple<T>::value, T>::type Get(int source) { auto f = Get<typename tuple_element<0, T>::type>(source); auto s = Get<typename tuple_element<1, T>::type>(source); auto t = Get<typename tuple_element<2, T>::type>(source); return T(f, s, t); } template<typename T> typename enable_if<is_container<T>::value, T>::type Get(int source) { int size = GetInt(source); vector<typename T::value_type> result; result.reserve(size); REP(i,size) result.push_back(Get<typename T::value_type>(source)); return T(result.begin(), result.end()); } template<typename T> void Broadcast(int source, T& value) { if (ME == source) REP(i,NODES) { Put<T>(i, value); Send(i); } Receive(source); value = Get<T>(source); } template<typename T> void BroadcastTree(int source, T& value) { int relative = (ME - source + NODES) % NODES; if (relative) { int from = (source + (relative-1) / 2) % NODES; Receive(from); value = Get<T>(from); } FOR(i,1,3) if (2*relative + i < NODES) { int to = ((source + 2*relative + i) % NODES); Put<T>(to, value); Send(to); } } template<typename T, typename Fn> void Accumulate(int target, const T& value, Fn fn) { Put<T>(target, value); Send(target); if (ME == target) REP(i, NODES) { Receive(i); fn(Get<T>(i)); } } /** ~5ms for adding ints */ template<typename T, typename Fn> T AccumulateTree(int target, T value, Fn fn) { int relative = (ME - target + NODES) % NODES; for (int b = 1; b < NODES; b <<= 1) { if (relative&b) { int to = (target + relative - b + NODES) % NODES; Put<T>(to, value); Send(to); break; } else if (relative + b < NODES) { int from = (target + relative + b) % NODES; Receive(from); value = fn(value, Get<T>(from)); } } return value; } template<typename T, typename Compute, typename Acc> T AccumulateValues(int target, Compute fn, Acc acc) { T result = fn(START); FOR(i,START+1,END) result = acc(result, fn(i)); return AccumulateTree(target, result, acc); } template<typename T, typename Compute> vector<T> AccumulateVector(int target, Compute fn) { vector<T> local; FOR(i,START, END) local.pb(fn(i)); vector<T> acc; Accumulate(target, local, [&](const vector<T>& vec) {acc.insert(acc.end(), vec.begin(), vec.end());}); return acc; } template<typename T> vector<T> Collect(int target, const T& value) { vector<T> acc; Accumulate(target, value, [&](const T& val) { acc.pb(val); }); return acc; } // ^^^ CUT HERE ^^^ #include "futbol.h" const int MOD = GetP(); const int K = GetK(); LL powe(LL a, int b) { LL res = 1; while (b) { if (b&1) (res *= a) %= MOD; (a *= a) %= MOD; b >>= 1; } return res; } LL inv(LL a) { return powe(a, MOD - 2); } LL _data[11111111]; int main() { if (MOD == 2) { if (ME != 0) return 0; printf("%d\n", K == 0 ? 1 : 0); return 0; } reduce_nodes(MOD); LL* data = _data - START; // data[i] = powe(2, M-1-i) * SN[K+i][K] int INV2 = (MOD + 1) / 2; data[START] = 1; LL divi = 1; FOR(i,START+1,END) { data[i] = data[i-1] * (K + i) % MOD * INV2 % MOD; // /i (divi *= i) %= MOD; } divi = inv(divi); FORD(i,END,START+1) { (data[i] *= divi) %= MOD; (divi *= i) %= MOD; } LL next = data[END-1] * (K+END) % MOD * INV2 % MOD * inv(END) % MOD; FOR(i,START+1,END) { (data[i] += data[i-1]) %= MOD; } LL acc = 0; int M = GetN() - K; LL multiplier = M > 0 ? powe(2,M-1) : 1; if (ME != 0) { Receive(PREV); acc = GetLL(PREV); multiplier = GetLL(PREV); M = GetInt(PREV); } if (M >= START + 1) { (acc += data[min(M-1,(int)END-1)] * multiplier) %= MOD; } (multiplier *= next) %= MOD; PutLL(NEXT, acc); PutLL(NEXT, multiplier); PutInt(NEXT, M); Send(NEXT); if (ME == 0) { Receive(PREV); acc = GetLL(PREV); multiplier = GetLL(PREV); M = GetInt(PREV); LL result = (powe(2, K+M) - acc) % MOD; result += MOD; result %= MOD; printf("%lld\n", result); } } |