#include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second #include "message.h" #include "poszukiwania.h" typedef int Int21; typedef ULL Int42; const Int42 LOWER_21 = (1LL << 21) - 1; int ME, NODES; int M, S; int CHUNK, SMALL_CHUNK; Int42 SIGNAL_HASH; template<Int21 Multiplier, Int42 MOD> struct GHash { Int42 value; GHash(Int42 _value): value(_value) {} static Int42 mul(Int42 a, Int42 b) { Int42 a0 = a & LOWER_21, a1 = a >> 21; Int42 b0 = b & LOWER_21, b1 = b >> 21; Int42 res = (a * b0 + a0 * b - a0 * b0) % MOD; res += (((((a1 * b1) % MOD) << 21) % MOD) << 21) % MOD; return res % MOD; } static Int42 shifter(int len) { Int42 res = 1, M = Multiplier; while (len) { if (len&1) { res = mul(res, M); } len >>= 1; M = mul(M, M); } return res; } void shift() { value = (value * Multiplier) % MOD; } void shift(Int42 by) { value = mul(value, by); } void add(Int42 x) { value = (value + x) % MOD; } void drop(Int42 x, Int42 by) { value = (value + MOD - mul(x, by)) % MOD; } }; typedef GHash<48271, ((1LL<<42) - 1)> Hash; int signalPosition[1000]; ULL getSignalHash() { REP(i, NODES) signalPosition[i] = S / NODES * i; signalPosition[NODES] = S; Hash partial(0); FOR(i, signalPosition[ME], signalPosition[ME + 1]) { partial.shift(); partial.add(SignalAt(i+1)); } PutLL(0, partial.value); Send(0); if (ME == 0) { Hash full(0); REP(i, NODES) { Receive(i); Int42 x = GetLL(i); Int42 shift_by = Hash::shifter(signalPosition[i+1] - signalPosition[i]); full.shift(shift_by); full.add(x); } REP(i, NODES) { PutLL(i, full.value); Send(i); } } Receive(0); return GetLL(0); } int getSeqAt(int p) { return p < M ? SeqAt(p+1) : 1000000001; } int scanAndSend(Hash h, int start, int end) { int result = 0; Int42 shift_by = Hash::shifter(S); FOR(i, start, end) { if (i > start) { h.shift(); h.drop(getSeqAt(i-S), shift_by); h.add(getSeqAt(i)); } if (h.value == SIGNAL_HASH) ++result; } PutInt(0, result); Send(0); } void receiveAndPrint() { if (ME != 0) return; int total = 0; REP(i, NODES) { Receive(i); total += GetInt(i); } printf("%d\n", total); } void doSmall() { int chunk_start = max(CHUNK * ME, S - 1), chunk_end = CHUNK * (ME + 1); Hash h(0); FOR(i, chunk_start - S + 1, chunk_start + 1) { h.shift(); h.add(getSeqAt(i)); } scanAndSend(h, chunk_start, chunk_end); receiveAndPrint(); } void doBig() { Hash h(0); FOR(i, SMALL_CHUNK * ME, SMALL_CHUNK * (ME + 1)) { h.shift(); h.add(getSeqAt(i)); } PutLL(0, h.value); Send(0); int chunks_num = S / SMALL_CHUNK; int R = S - chunks_num * SMALL_CHUNK; if (ME == 0) { Int42 chunk_shift = Hash::shifter(SMALL_CHUNK); Int42 chunks_shift = Hash::shifter(SMALL_CHUNK * chunks_num); Int42 chunk_hashes[NODES]; REP(i, NODES) { Receive(i); chunk_hashes[i] = GetLL(i); } Hash chunks_hash(0); REP(i, NODES) { chunks_hash.shift(chunk_shift); chunks_hash.add(chunk_hashes[i]); if (i >= chunks_num) { chunks_hash.drop(chunk_hashes[i - chunks_num], chunks_shift); } PutLL(i, chunks_hash.value); Send(i); } } Receive(0); Hash chunks_hash(GetLL(0)); int chunk_start = SMALL_CHUNK * (ME + 1) + R - 1; if (chunk_start + 1 >= S) { FOR(i, SMALL_CHUNK * (ME + 1), chunk_start + 1) { chunks_hash.shift(); chunks_hash.add(getSeqAt(i)); } scanAndSend(chunks_hash, chunk_start, chunk_start + SMALL_CHUNK); } else { PutInt(0, 0); Send(0); } receiveAndPrint(); } int main() { ME = MyNodeId(); NODES = NumberOfNodes(); S = SignalLength(); M = SeqLength(); SIGNAL_HASH = getSignalHash(); CHUNK = (M + NODES - 1) / NODES; SMALL_CHUNK = (M + NODES) / (NODES + 1); int result; if (S <= CHUNK) { doSmall(); } else { doBig(); } }
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 235 236 237 238 239 240 | #include <list> #include <map> #include <queue> #include <set> #include <stack> #include <vector> #include <cmath> #include <cstring> #include <string> #include <iostream> #include <complex> #include <sstream> #include <cassert> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef long double LD; typedef vector<int> VI; typedef pair<int,int> PII; #define REP(i,n) for(int i=0;i<(n);++i) #define SIZE(c) ((int)((c).size())) #define FOR(i,a,b) for (int i=(a); i<(b); ++i) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i) #define ALL(v) (v).begin(), (v).end() #define pb push_back #define mp make_pair #define st first #define nd second #include "message.h" #include "poszukiwania.h" typedef int Int21; typedef ULL Int42; const Int42 LOWER_21 = (1LL << 21) - 1; int ME, NODES; int M, S; int CHUNK, SMALL_CHUNK; Int42 SIGNAL_HASH; template<Int21 Multiplier, Int42 MOD> struct GHash { Int42 value; GHash(Int42 _value): value(_value) {} static Int42 mul(Int42 a, Int42 b) { Int42 a0 = a & LOWER_21, a1 = a >> 21; Int42 b0 = b & LOWER_21, b1 = b >> 21; Int42 res = (a * b0 + a0 * b - a0 * b0) % MOD; res += (((((a1 * b1) % MOD) << 21) % MOD) << 21) % MOD; return res % MOD; } static Int42 shifter(int len) { Int42 res = 1, M = Multiplier; while (len) { if (len&1) { res = mul(res, M); } len >>= 1; M = mul(M, M); } return res; } void shift() { value = (value * Multiplier) % MOD; } void shift(Int42 by) { value = mul(value, by); } void add(Int42 x) { value = (value + x) % MOD; } void drop(Int42 x, Int42 by) { value = (value + MOD - mul(x, by)) % MOD; } }; typedef GHash<48271, ((1LL<<42) - 1)> Hash; int signalPosition[1000]; ULL getSignalHash() { REP(i, NODES) signalPosition[i] = S / NODES * i; signalPosition[NODES] = S; Hash partial(0); FOR(i, signalPosition[ME], signalPosition[ME + 1]) { partial.shift(); partial.add(SignalAt(i+1)); } PutLL(0, partial.value); Send(0); if (ME == 0) { Hash full(0); REP(i, NODES) { Receive(i); Int42 x = GetLL(i); Int42 shift_by = Hash::shifter(signalPosition[i+1] - signalPosition[i]); full.shift(shift_by); full.add(x); } REP(i, NODES) { PutLL(i, full.value); Send(i); } } Receive(0); return GetLL(0); } int getSeqAt(int p) { return p < M ? SeqAt(p+1) : 1000000001; } int scanAndSend(Hash h, int start, int end) { int result = 0; Int42 shift_by = Hash::shifter(S); FOR(i, start, end) { if (i > start) { h.shift(); h.drop(getSeqAt(i-S), shift_by); h.add(getSeqAt(i)); } if (h.value == SIGNAL_HASH) ++result; } PutInt(0, result); Send(0); } void receiveAndPrint() { if (ME != 0) return; int total = 0; REP(i, NODES) { Receive(i); total += GetInt(i); } printf("%d\n", total); } void doSmall() { int chunk_start = max(CHUNK * ME, S - 1), chunk_end = CHUNK * (ME + 1); Hash h(0); FOR(i, chunk_start - S + 1, chunk_start + 1) { h.shift(); h.add(getSeqAt(i)); } scanAndSend(h, chunk_start, chunk_end); receiveAndPrint(); } void doBig() { Hash h(0); FOR(i, SMALL_CHUNK * ME, SMALL_CHUNK * (ME + 1)) { h.shift(); h.add(getSeqAt(i)); } PutLL(0, h.value); Send(0); int chunks_num = S / SMALL_CHUNK; int R = S - chunks_num * SMALL_CHUNK; if (ME == 0) { Int42 chunk_shift = Hash::shifter(SMALL_CHUNK); Int42 chunks_shift = Hash::shifter(SMALL_CHUNK * chunks_num); Int42 chunk_hashes[NODES]; REP(i, NODES) { Receive(i); chunk_hashes[i] = GetLL(i); } Hash chunks_hash(0); REP(i, NODES) { chunks_hash.shift(chunk_shift); chunks_hash.add(chunk_hashes[i]); if (i >= chunks_num) { chunks_hash.drop(chunk_hashes[i - chunks_num], chunks_shift); } PutLL(i, chunks_hash.value); Send(i); } } Receive(0); Hash chunks_hash(GetLL(0)); int chunk_start = SMALL_CHUNK * (ME + 1) + R - 1; if (chunk_start + 1 >= S) { FOR(i, SMALL_CHUNK * (ME + 1), chunk_start + 1) { chunks_hash.shift(); chunks_hash.add(getSeqAt(i)); } scanAndSend(chunks_hash, chunk_start, chunk_start + SMALL_CHUNK); } else { PutInt(0, 0); Send(0); } receiveAndPrint(); } int main() { ME = MyNodeId(); NODES = NumberOfNodes(); S = SignalLength(); M = SeqLength(); SIGNAL_HASH = getSignalHash(); CHUNK = (M + NODES - 1) / NODES; SMALL_CHUNK = (M + NODES) / (NODES + 1); int result; if (S <= CHUNK) { doSmall(); } else { doBig(); } } |