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
392
393
#include "message.h"
#include "poszukiwania.h"

#include <cassert>
#include <iostream>
#include <limits>
#include <vector>
using std::numeric_limits;
using std::vector;

typedef long long LL;
typedef unsigned long long ULL;

// Mersenne prime.
constexpr int primeExponent = 61;
constexpr ULL prime = (1ULL << primeExponent) - 1ULL;

// Modulo prime.
class Mod {
 public:
  // x must be < p
  explicit constexpr Mod(ULL x) : val{x} {}

  Mod operator+(Mod b) const {
    ULL res = val + b.val;
    if (res >= prime) res -= prime;
    return Mod{res};
  }

  Mod operator-(Mod b) const {
    ULL res = val;
    if (res < b.val) res += prime;
    res -= b.val;
    return Mod{res};
  }

  Mod operator*(unsigned b) const {
    unsigned a0 = unsigned(val);
    unsigned a1 = unsigned(val >> 32);
    ULL res0 = ULL(a0) * ULL(b);
    ULL res1 = ULL(a1) * ULL(b);
    ULL res =
      (res0 & prime) + (res0 >> primeExponent) +
      ((res1 << 32) & prime) + (res1 >> (primeExponent - 32));
    return Mod{res % prime};
  }

  Mod operator*(Mod b) const {
    unsigned a0 = unsigned(val);
    unsigned a1 = unsigned(val >> 32);
    unsigned b0 = unsigned(b.val);
    unsigned b1 = unsigned(b.val >> 32);
    ULL res0 = ULL(a0) * ULL(b0);
    ULL res1 = ULL(a0) * ULL(b1) + ULL(a1) * ULL(b0);
    ULL res2 = ULL(a1) * ULL(b1);
    ULL res =
      (res0 & prime) + (res0 >> primeExponent) +
      ((res1 << 32) & prime) + (res1 >> (primeExponent - 32)) +
      ((res2 << (64 - primeExponent)) & prime) +
      (res2 >> (2 * primeExponent - 64));
    return Mod{res % prime};
  }

  bool operator==(Mod b) const { return val == b.val; }

  void operator+=(Mod b) { *this = *this + b; }
  void operator-=(Mod b) { *this = *this - b; }
  void operator*=(unsigned b) { *this = *this * b; }
  void operator*=(Mod b) { *this = *this * b; }

  ULL Extract() const { return val; }

 private:
  ULL val;
};

Mod Power(Mod a, ULL n) {
  Mod res{1};
  ULL two_b = 1u;
  Mod a_two_b = a;
  while(two_b <= n) {
    if (n & two_b) res *= a_two_b;
    two_b <<= 1;
    a_two_b *= a_two_b;
  }
  return res;
}

template<ULL baseULL>
class Hash {
  static_assert(baseULL < prime, "base < prime");
  static constexpr Mod base = Mod{baseULL};

 public:
  Hash() : val{0}, powBaseLen{0} {}
  Hash(Mod v) : val{v}, powBaseLen{0} {}

  void Append(unsigned c) {
    val = val * base + Mod{c};
  }

  void AppendSuffix(const Hash &h, ULL len) {
    val = val * Power(base, len) + h.val;
  }

  void SetLength(ULL len) {
    powBaseLen = Power(base, len);
  }

  void DeleteChar(unsigned c) {
    assert(powBaseLen.Extract());
    val -= powBaseLen * c;
  }

  void DeletePrefix(const Hash &h) {
    assert(powBaseLen.Extract());
    val -= powBaseLen * h.val;
  }

  bool operator==(const Hash &b) const {
    return val == b.val;
  }

  Mod Extract() const { return val; }

 private:
  Mod val;
  Mod powBaseLen;
};

template<ULL baseULL> constexpr Mod Hash<baseULL>::base;

class StrongHash {
 public:
  StrongHash() : hash1{}, hash2{} {}
  StrongHash(Mod h1, Mod h2) : hash1{h1}, hash2{h2} {}

  void Append(unsigned c) {
    hash1.Append(c);
    hash2.Append(c);
  }

  void AppendSuffix(const StrongHash &sh, ULL len) {
    hash1.AppendSuffix(sh.hash1, len);
    hash2.AppendSuffix(sh.hash2, len);
  }

  void SetLength(ULL len) {
    hash1.SetLength(len);
    hash2.SetLength(len);
  }

  void DeleteChar(unsigned c) {
    hash1.DeleteChar(c);
    hash2.DeleteChar(c);
  }

  void DeletePrefix(const StrongHash &sh) {
    hash1.DeletePrefix(sh.hash1);
    hash2.DeletePrefix(sh.hash2);
  }

  bool operator==(const StrongHash &b) const {
    return hash1 == b.hash1 && hash2 == b.hash2;
  }

  Mod Extract1() const { return hash1.Extract(); }
  Mod Extract2() const { return hash2.Extract(); }

 private:
  Hash<1919222792405563639ULL> hash1;
  Hash<1940348388277362841ULL> hash2;
};

int numNodes;
int myNode;
LL signalLength;
LL seqLength;

inline LL SplitSignal(int i) {
  return signalLength * LL(i) / numNodes;
}

inline LL SplitSeq(int i) {
  return seqLength * LL(i) / numNodes;
}

inline LL SplitWindowBegin(int i) {
  return (seqLength - signalLength + 1) * LL(i) / numNodes;
}

inline LL SplitWindowEnd(int i) {
  return SplitWindowBegin(i) + signalLength;
}

StrongHash PartHashSignal() {
  const LL alpha = SplitSignal(myNode);
  const LL beta = SplitSignal(myNode+1);

  StrongHash sh;
  for (LL i = alpha; i < beta; ++i) {
    sh.Append(SignalAt(i+1));
  }
  return sh;
}

StrongHash PartHashSeq(vector<StrongHash> &hashWindowBegins,
                       vector<StrongHash> &hashWindowEnds) {
  const LL alpha = SplitSeq(myNode);
  const LL beta = SplitSeq(myNode+1);

  int nextWindowBeginIdx = 0;
  LL nextWindowBegin;
  for (;;) {
    nextWindowBegin = nextWindowBeginIdx >= numNodes ?
      numeric_limits<LL>::max() :
      SplitWindowBegin(nextWindowBeginIdx);
    if (nextWindowBegin >= alpha) break;
    ++nextWindowBeginIdx;
  }

  int nextWindowEndIdx = 0;
  LL nextWindowEnd;
  for (;;) {
    nextWindowEnd = nextWindowEndIdx >= numNodes ?
      numeric_limits<LL>::max() :
      SplitWindowEnd(nextWindowEndIdx);
    if (nextWindowEnd >= alpha) break;
    ++nextWindowEndIdx;
  }

  StrongHash sh;
  for (LL i = alpha; i < beta; ++i) {
    while (i == nextWindowBegin) {
      hashWindowBegins.push_back(sh);
      ++nextWindowBeginIdx;
      nextWindowBegin = nextWindowBeginIdx >= numNodes ?
        numeric_limits<LL>::max() :
        SplitWindowBegin(nextWindowBeginIdx);
    }

    while (i == nextWindowEnd) {
      hashWindowEnds.push_back(sh);
      ++nextWindowEndIdx;
      nextWindowEnd = nextWindowEndIdx >= numNodes ?
        numeric_limits<LL>::max() :
        SplitWindowEnd(nextWindowEndIdx);
    }

    sh.Append(SeqAt(i+1));
  }

  if (myNode == numNodes - 1) {
    while (nextWindowBeginIdx < numNodes) {
      assert(SplitWindowBegin(nextWindowBeginIdx) == beta);
      hashWindowBegins.push_back(sh);
      ++nextWindowBeginIdx;
    }
    while (nextWindowEndIdx < numNodes) {
      assert(SplitWindowEnd(nextWindowEndIdx) == beta);
      hashWindowEnds.push_back(sh);
      ++nextWindowEndIdx;
    }
  }

  return sh;
}

LL PartWindowScan(StrongHash hashSignal, StrongHash hashWindow) {
  const LL alpha = SplitWindowBegin(myNode);
  const LL beta = SplitWindowBegin(myNode+1);

  LL res = 0;
  for (LL i = alpha; i < beta; ++i) {
    if (hashWindow == hashSignal) ++res;
    if (i+1 >= beta) break;
    hashWindow.Append(SeqAt(i+1 + signalLength));
    hashWindow.DeleteChar(SeqAt(i+1));
  }

  return res;
}

void PutStrongHash(int node, const StrongHash &sh) {
  PutLL(node, sh.Extract1().Extract());
  PutLL(node, sh.Extract2().Extract());
}

StrongHash GetStrongHash(int node) {
  LL m1 = GetLL(node);
  LL m2 = GetLL(node);
  return StrongHash{Mod{m1}, Mod{m2}};
}

int main() {
  numNodes = NumberOfNodes();
  myNode = MyNodeId();
  signalLength = SignalLength();
  seqLength = SeqLength();
  assert(signalLength <= seqLength);

  StrongHash hashSignal = PartHashSignal();

  vector<StrongHash> hashWindowBegins;
  vector<StrongHash> hashWindowEnds;
  hashWindowBegins.reserve(numNodes);
  hashWindowEnds.reserve(numNodes);
  StrongHash hashSeq = PartHashSeq(hashWindowBegins,
                                   hashWindowEnds);

  StrongHash hashWindow;

  if (myNode > 0) {
    PutStrongHash(0, hashSignal);
    PutInt(0, hashWindowBegins.size());
    for (const StrongHash &sh : hashWindowBegins) {
      PutStrongHash(0, sh);
    }
    PutInt(0, hashWindowEnds.size());
    for (const StrongHash &sh : hashWindowEnds) {
      PutStrongHash(0, sh);
    }
    PutStrongHash(0, hashSeq);
    Send(0);

    Receive(0);
    hashSignal = GetStrongHash(0);
    hashWindow = GetStrongHash(0);
    hashWindow.SetLength(signalLength);
  } else {

    for (int node = 1; node < numNodes; ++node) {
      Receive(node);

      StrongHash hashSignal2 = GetStrongHash(node);
      hashSignal.AppendSuffix(hashSignal2,
                              SplitSignal(node+1) - SplitSignal(node));

      int numWindowBegins = GetInt(node);
      for (int i=0; i < numWindowBegins; ++i) {
        StrongHash windowBegin = hashSeq;
        StrongHash sh = GetStrongHash(node);
        int w = hashWindowBegins.size();
        LL len = SplitWindowBegin(w) - SplitSeq(node);
        assert(len>=0);
        windowBegin.AppendSuffix(sh, len);
        hashWindowBegins.push_back(windowBegin);
      }

      int numWindowEnds = GetInt(node);
      for (int i=0; i < numWindowEnds; ++i) {
        StrongHash windowEnd = hashSeq;
        StrongHash sh = GetStrongHash(node);
        int w = hashWindowEnds.size();
        LL len = SplitWindowEnd(w) - SplitSeq(node);
        assert(len>=0);
        windowEnd.AppendSuffix(sh, len);
        hashWindowEnds.push_back(windowEnd);
      }

      StrongHash hashSeq2 = GetStrongHash(node);
      hashSeq.AppendSuffix(hashSeq2, SplitSeq(node+1) - SplitSeq(node));
    }

    assert(int(hashWindowBegins.size()) == numNodes);
    assert(int(hashWindowEnds.size()) == numNodes);

    for (int node=0; node < numNodes; ++node) {
      hashWindowEnds[node].SetLength(signalLength);
      hashWindowEnds[node].DeletePrefix(hashWindowBegins[node]);
      if (node > 0) {
        PutStrongHash(node, hashSignal);
        PutStrongHash(node, hashWindowEnds[node]);
        Send(node);
      }
    }
    hashWindow = hashWindowEnds[0];
  }

  LL res = PartWindowScan(hashSignal, hashWindow);

  if (myNode > 0) {
    PutLL(0, res);
    Send(0);
  } else {
    for (int node = 1; node < numNodes; ++node) {
      Receive(node);
      LL res2 = GetLL(node);
      res += res2;
    }
    std::cout << res << '\n';
  }
}