#include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdint> #include <queue> #include <vector> #include "poszukiwania.h" #include "message.h" static const uint64_t MODULO_HALF = 1000 * 1000 * 1000 + 9; static const uint64_t MODULO_BASE = MODULO_HALF * MODULO_HALF; static const uint64_t X_BASE = 1200000041; static const uint64_t X_INV = 957501881187053277LL; // static const uint64_t X_BASE = 10; // static const uint64_t X_INV = 900000016200000073LL; struct miniQuery_t { uint64_t content; uint64_t recipient; }; struct query_t { uint64_t content; uint64_t recipient; uint64_t response; }; namespace { int nodesCount, nodeID; uint64_t signalLength, sequenceLength; uint64_t computedSignalStart, computedSignalEnd; uint64_t computedSequenceStart, computedSequenceEnd; std::vector<query_t> sequenceQueries; uint64_t prefixSum = 0; uint64_t sequenceStartHash = 0; uint64_t completeSignalHash = 0; } static inline long long SeqAtProxy(long long i) { return SeqAt(i + 1); } static inline long long SignalAtProxy(long long i) { return SignalAt(i + 1); } static inline uint64_t safeAdd(uint64_t a, uint64_t b) { return (a + b) % MODULO_BASE; } static inline uint64_t safeSubtract(uint64_t a, uint64_t b) { return ((a + MODULO_BASE) - b) % MODULO_BASE; } static inline uint64_t safeMultiply(uint64_t a, uint64_t b) { const uint64_t lowA = a % MODULO_HALF; const uint64_t hiA = a / MODULO_HALF; const uint64_t lowB = b % MODULO_HALF; const uint64_t hiB = b / MODULO_HALF; uint64_t acc = lowA * lowB; uint64_t lowRet = acc % MODULO_HALF; uint64_t hiRet = acc / MODULO_HALF; acc = lowA * hiB; hiRet += acc % MODULO_HALF; acc = lowB * hiA; hiRet += acc % MODULO_HALF; return lowRet + (hiRet % MODULO_HALF) * MODULO_HALF; } uint64_t moduloPow(uint64_t t, uint64_t x) { uint64_t ret = 1; uint64_t bitty = 1; while (x != 0) { if (x & bitty) ret = safeMultiply(ret, t); x &= ~bitty; bitty <<= 1; t = safeMultiply(t, t); } return ret; } void initializeVariables() { nodesCount = NumberOfNodes(); nodeID = MyNodeId(); signalLength = SignalLength(); sequenceLength = SeqLength(); computedSignalStart = nodeID * signalLength / nodesCount; computedSignalEnd = (nodeID + 1) * signalLength / nodesCount; computedSequenceStart = nodeID * sequenceLength / nodesCount; computedSequenceEnd = (nodeID + 1) * sequenceLength / nodesCount; } void prepareQueries() { std::vector<miniQuery_t> allQueries; // Determine starting positions to compute for (int64_t i = 0; i < (int64_t)nodesCount; i++) { int64_t queryPos = (i * sequenceLength / nodesCount) - signalLength; if (queryPos > 0) allQueries.push_back({ (uint64_t)queryPos, (uint64_t)i }); } // Dispatch queries with recipients uint64_t numQuery = 0; for (uint64_t i = 0; i < nodesCount; i++) { uint64_t bound = (i + 1) * sequenceLength / nodesCount; uint64_t j; // fprintf(stderr, "(%lld)\n", bound); for (j = numQuery; j < allQueries.size() && allQueries[j].content < bound; j++) { // Nothing } PutInt(i, j - numQuery); while (numQuery < j) { // fprintf(stderr, "Sending %lld %lld\n", allQueries[numQuery].content, allQueries[numQuery].recipient); PutLL(i, allQueries[numQuery].content); // Query content PutLL(i, allQueries[numQuery].recipient); // Recipient numQuery++; } // fprintf(stderr, "SEND\n"); Send(i); } } void receiveQueries() { Receive(0); int numQueries = GetInt(0); for (int i = 0; i < numQueries; i++) { query_t q; q.content = GetLL(0); q.recipient = GetLL(0); q.response = 0; sequenceQueries.push_back(q); } } void computeHashForSequence() { uint64_t hash = 0; uint64_t x = moduloPow(X_BASE, computedSequenceStart); for (uint64_t i = computedSequenceStart; i < computedSequenceEnd; i++) { // There won't be many queries (around 1-2) for (query_t & q : sequenceQueries) { if (i == q.content) q.response = hash; } hash = safeAdd(hash, safeMultiply(x, SeqAtProxy(i))); x = safeMultiply(x, X_BASE); } PutLL(0, hash); Send(0); } void computePrefixSequences() { uint64_t hash = 0; for (uint64_t i = 0; i < nodesCount; i++) { Receive(i); uint64_t hashComponent = GetLL(i); // Send prefix PutLL(i, hash); Send(i); // Adjust prefix hash hash = safeAdd(hash, hashComponent); } } void receivePrefixSequences() { Receive(0); prefixSum = GetLL(0); } void handleQueries() { for (const query_t & q : sequenceQueries) { // fprintf(stderr, "QUERY for %lld\n", q.recipient); PutLL(q.recipient, safeAdd(q.response, prefixSum)); Send(q.recipient); } sequenceQueries.clear(); } void receiveAnswers() { // Do we get a response? int64_t queryPos = computedSequenceStart - signalLength; if (queryPos > 0) { auto r = Receive(-1); sequenceStartHash = GetLL(r); } } void computeHashForSignal() { uint64_t hash = 0; uint64_t x = moduloPow(X_BASE, computedSignalStart); for (uint64_t i = computedSignalStart; i < computedSignalEnd; i++) { hash = safeAdd(hash, safeMultiply(x, SignalAtProxy(i))); x = safeMultiply(x, X_BASE); } PutLL(0, hash); Send(0); } void mergeSignalHashes() { uint64_t hash = 0; for (uint64_t i = 0; i < nodesCount; i++) { Receive(i); hash = safeAdd(hash, GetLL(i)); } for (uint64_t i = 0; i < nodesCount; i++) { PutLL(i, hash); Send(i); } } void receiveCompleteSignalHash() { Receive(0); completeSignalHash = GetLL(0); } void findOccurrences() { if (nodeID == 0 && false) { fprintf(stderr, "Signal:\n"); for (uint64_t i = 0; i < signalLength; i++) fprintf(stderr, " %lld\n", SignalAtProxy(i)); fprintf(stderr, "Sequence:\n"); for (uint64_t i = 0; i < sequenceLength; i++) fprintf(stderr, " %lld\n", SeqAtProxy(i)); } int64_t queryPos = computedSequenceStart - signalLength; uint64_t hash = safeSubtract(prefixSum, sequenceStartHash); uint64_t distanceToZero = (uint64_t)std::max((int64_t)0LL, queryPos); // fprintf(stderr, "Base hash: %lld\n", hash); hash = safeMultiply(hash, moduloPow(X_INV, distanceToZero)); // fprintf(stderr, "computedSequenceStart: %lld\n", computedSequenceStart); // fprintf(stderr, "distanceToZero: %lld\n", distanceToZero); uint64_t hits = 0; uint64_t xMax = moduloPow(X_BASE, std::min(signalLength, computedSequenceStart)); // uint64_t i = std::max(signalLength, computedSequenceStart); uint64_t i = computedSequenceStart; // fprintf(stderr, "(%lld)\n", i); // fprintf(stderr, "POS %lld HASH %lld %lld\n", i, hash, completeSignalHash); // if (hash == completeSignalHash) // hits++; for (i; i < computedSequenceEnd; i++) { if (i >= signalLength) hash = safeSubtract(hash, SeqAtProxy(i - signalLength)); hash = safeAdd(hash, safeMultiply(xMax, SeqAtProxy(i))); if (i < signalLength) xMax = safeMultiply(xMax, X_BASE); else hash = safeMultiply(hash, X_INV); // fprintf(stderr, "POS %lld HASH %lld %lld\n", i + 1, hash, completeSignalHash); if (i >= signalLength - 1 && hash == completeSignalHash) { // fprintf(stderr, "HIT! %lld\n", i); hits++; } } PutLL(0, hits); Send(0); } void gatherOccurrences() { uint64_t sum = 0; for (uint64_t i = 0; i < nodesCount; i++) { Receive(i); sum += GetLL(i); } printf("%lld\n", sum); } void solve() { // Check for special case if (signalLength > sequenceLength) { if (nodeID == 0) puts("0"); exit(0); } if (nodeID == 0) prepareQueries(); receiveQueries(); computeHashForSequence(); // puts("FOO"); if (nodeID == 0) computePrefixSequences(); receivePrefixSequences(); // puts("FOO"); handleQueries(); receiveAnswers(); // puts("FOO"); computeHashForSignal(); if (nodeID == 0) mergeSignalHashes(); receiveCompleteSignalHash(); // puts("FOO"); findOccurrences(); if (nodeID == 0) gatherOccurrences(); } int main() { initializeVariables(); solve(); 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 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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | #include <algorithm> #include <cassert> #include <cmath> #include <cstdio> #include <cstdint> #include <queue> #include <vector> #include "poszukiwania.h" #include "message.h" static const uint64_t MODULO_HALF = 1000 * 1000 * 1000 + 9; static const uint64_t MODULO_BASE = MODULO_HALF * MODULO_HALF; static const uint64_t X_BASE = 1200000041; static const uint64_t X_INV = 957501881187053277LL; // static const uint64_t X_BASE = 10; // static const uint64_t X_INV = 900000016200000073LL; struct miniQuery_t { uint64_t content; uint64_t recipient; }; struct query_t { uint64_t content; uint64_t recipient; uint64_t response; }; namespace { int nodesCount, nodeID; uint64_t signalLength, sequenceLength; uint64_t computedSignalStart, computedSignalEnd; uint64_t computedSequenceStart, computedSequenceEnd; std::vector<query_t> sequenceQueries; uint64_t prefixSum = 0; uint64_t sequenceStartHash = 0; uint64_t completeSignalHash = 0; } static inline long long SeqAtProxy(long long i) { return SeqAt(i + 1); } static inline long long SignalAtProxy(long long i) { return SignalAt(i + 1); } static inline uint64_t safeAdd(uint64_t a, uint64_t b) { return (a + b) % MODULO_BASE; } static inline uint64_t safeSubtract(uint64_t a, uint64_t b) { return ((a + MODULO_BASE) - b) % MODULO_BASE; } static inline uint64_t safeMultiply(uint64_t a, uint64_t b) { const uint64_t lowA = a % MODULO_HALF; const uint64_t hiA = a / MODULO_HALF; const uint64_t lowB = b % MODULO_HALF; const uint64_t hiB = b / MODULO_HALF; uint64_t acc = lowA * lowB; uint64_t lowRet = acc % MODULO_HALF; uint64_t hiRet = acc / MODULO_HALF; acc = lowA * hiB; hiRet += acc % MODULO_HALF; acc = lowB * hiA; hiRet += acc % MODULO_HALF; return lowRet + (hiRet % MODULO_HALF) * MODULO_HALF; } uint64_t moduloPow(uint64_t t, uint64_t x) { uint64_t ret = 1; uint64_t bitty = 1; while (x != 0) { if (x & bitty) ret = safeMultiply(ret, t); x &= ~bitty; bitty <<= 1; t = safeMultiply(t, t); } return ret; } void initializeVariables() { nodesCount = NumberOfNodes(); nodeID = MyNodeId(); signalLength = SignalLength(); sequenceLength = SeqLength(); computedSignalStart = nodeID * signalLength / nodesCount; computedSignalEnd = (nodeID + 1) * signalLength / nodesCount; computedSequenceStart = nodeID * sequenceLength / nodesCount; computedSequenceEnd = (nodeID + 1) * sequenceLength / nodesCount; } void prepareQueries() { std::vector<miniQuery_t> allQueries; // Determine starting positions to compute for (int64_t i = 0; i < (int64_t)nodesCount; i++) { int64_t queryPos = (i * sequenceLength / nodesCount) - signalLength; if (queryPos > 0) allQueries.push_back({ (uint64_t)queryPos, (uint64_t)i }); } // Dispatch queries with recipients uint64_t numQuery = 0; for (uint64_t i = 0; i < nodesCount; i++) { uint64_t bound = (i + 1) * sequenceLength / nodesCount; uint64_t j; // fprintf(stderr, "(%lld)\n", bound); for (j = numQuery; j < allQueries.size() && allQueries[j].content < bound; j++) { // Nothing } PutInt(i, j - numQuery); while (numQuery < j) { // fprintf(stderr, "Sending %lld %lld\n", allQueries[numQuery].content, allQueries[numQuery].recipient); PutLL(i, allQueries[numQuery].content); // Query content PutLL(i, allQueries[numQuery].recipient); // Recipient numQuery++; } // fprintf(stderr, "SEND\n"); Send(i); } } void receiveQueries() { Receive(0); int numQueries = GetInt(0); for (int i = 0; i < numQueries; i++) { query_t q; q.content = GetLL(0); q.recipient = GetLL(0); q.response = 0; sequenceQueries.push_back(q); } } void computeHashForSequence() { uint64_t hash = 0; uint64_t x = moduloPow(X_BASE, computedSequenceStart); for (uint64_t i = computedSequenceStart; i < computedSequenceEnd; i++) { // There won't be many queries (around 1-2) for (query_t & q : sequenceQueries) { if (i == q.content) q.response = hash; } hash = safeAdd(hash, safeMultiply(x, SeqAtProxy(i))); x = safeMultiply(x, X_BASE); } PutLL(0, hash); Send(0); } void computePrefixSequences() { uint64_t hash = 0; for (uint64_t i = 0; i < nodesCount; i++) { Receive(i); uint64_t hashComponent = GetLL(i); // Send prefix PutLL(i, hash); Send(i); // Adjust prefix hash hash = safeAdd(hash, hashComponent); } } void receivePrefixSequences() { Receive(0); prefixSum = GetLL(0); } void handleQueries() { for (const query_t & q : sequenceQueries) { // fprintf(stderr, "QUERY for %lld\n", q.recipient); PutLL(q.recipient, safeAdd(q.response, prefixSum)); Send(q.recipient); } sequenceQueries.clear(); } void receiveAnswers() { // Do we get a response? int64_t queryPos = computedSequenceStart - signalLength; if (queryPos > 0) { auto r = Receive(-1); sequenceStartHash = GetLL(r); } } void computeHashForSignal() { uint64_t hash = 0; uint64_t x = moduloPow(X_BASE, computedSignalStart); for (uint64_t i = computedSignalStart; i < computedSignalEnd; i++) { hash = safeAdd(hash, safeMultiply(x, SignalAtProxy(i))); x = safeMultiply(x, X_BASE); } PutLL(0, hash); Send(0); } void mergeSignalHashes() { uint64_t hash = 0; for (uint64_t i = 0; i < nodesCount; i++) { Receive(i); hash = safeAdd(hash, GetLL(i)); } for (uint64_t i = 0; i < nodesCount; i++) { PutLL(i, hash); Send(i); } } void receiveCompleteSignalHash() { Receive(0); completeSignalHash = GetLL(0); } void findOccurrences() { if (nodeID == 0 && false) { fprintf(stderr, "Signal:\n"); for (uint64_t i = 0; i < signalLength; i++) fprintf(stderr, " %lld\n", SignalAtProxy(i)); fprintf(stderr, "Sequence:\n"); for (uint64_t i = 0; i < sequenceLength; i++) fprintf(stderr, " %lld\n", SeqAtProxy(i)); } int64_t queryPos = computedSequenceStart - signalLength; uint64_t hash = safeSubtract(prefixSum, sequenceStartHash); uint64_t distanceToZero = (uint64_t)std::max((int64_t)0LL, queryPos); // fprintf(stderr, "Base hash: %lld\n", hash); hash = safeMultiply(hash, moduloPow(X_INV, distanceToZero)); // fprintf(stderr, "computedSequenceStart: %lld\n", computedSequenceStart); // fprintf(stderr, "distanceToZero: %lld\n", distanceToZero); uint64_t hits = 0; uint64_t xMax = moduloPow(X_BASE, std::min(signalLength, computedSequenceStart)); // uint64_t i = std::max(signalLength, computedSequenceStart); uint64_t i = computedSequenceStart; // fprintf(stderr, "(%lld)\n", i); // fprintf(stderr, "POS %lld HASH %lld %lld\n", i, hash, completeSignalHash); // if (hash == completeSignalHash) // hits++; for (i; i < computedSequenceEnd; i++) { if (i >= signalLength) hash = safeSubtract(hash, SeqAtProxy(i - signalLength)); hash = safeAdd(hash, safeMultiply(xMax, SeqAtProxy(i))); if (i < signalLength) xMax = safeMultiply(xMax, X_BASE); else hash = safeMultiply(hash, X_INV); // fprintf(stderr, "POS %lld HASH %lld %lld\n", i + 1, hash, completeSignalHash); if (i >= signalLength - 1 && hash == completeSignalHash) { // fprintf(stderr, "HIT! %lld\n", i); hits++; } } PutLL(0, hits); Send(0); } void gatherOccurrences() { uint64_t sum = 0; for (uint64_t i = 0; i < nodesCount; i++) { Receive(i); sum += GetLL(i); } printf("%lld\n", sum); } void solve() { // Check for special case if (signalLength > sequenceLength) { if (nodeID == 0) puts("0"); exit(0); } if (nodeID == 0) prepareQueries(); receiveQueries(); computeHashForSequence(); // puts("FOO"); if (nodeID == 0) computePrefixSequences(); receivePrefixSequences(); // puts("FOO"); handleQueries(); receiveAnswers(); // puts("FOO"); computeHashForSignal(); if (nodeID == 0) mergeSignalHashes(); receiveCompleteSignalHash(); // puts("FOO"); findOccurrences(); if (nodeID == 0) gatherOccurrences(); } int main() { initializeVariables(); solve(); return 0; } |