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

#include <algorithm>
#include <cassert>
#include <iostream>

using namespace std;

const unsigned int kModulus = 3037000493U;
const unsigned int kI = 315439574U;
const unsigned int kOneFifth = 1822200296U;

class Hash {
 public:
  Hash() : Hash(1, 0, 0, 1) {}

  static void Init() {
    Hash one_fifth = Hash(kOneFifth, 0, 0, kOneFifth);
    // 1-bit.
      bytes[0] = Hash(1         , 2, kModulus - 2,                     1);
    unbytes[0] = Hash(1         , kModulus - 2, 2,                     1);
      bytes[1] = Hash(       1 + 2 * kI    , 0, 0, kModulus - 2 * kI + 1);
    unbytes[1] = Hash(kModulus - 2 * kI + 1, 0, 0,        1 + 2 * kI    );
    unbytes[0].RightMultiplyBy(one_fifth);
    unbytes[1].RightMultiplyBy(one_fifth);
    /*
    bytes[0].RightMultiplyBy(unbytes[0]);
    bytes[0].Print();
    bytes[1].RightMultiplyBy(unbytes[1]);
    bytes[1].Print();
    assert(false);
    */
    // 2-bit.
    for (int i = 3; i >= 0; --i) {
      bytes[i] = bytes[i & 1];
      bytes[i].RightMultiplyBy(bytes[i >> 1]);
      unbytes[i] = unbytes[i & 1];
      unbytes[i].LeftMultiplyBy(unbytes[i >> 1]);
    }
    // 4-bit.
    for (int i = 0xf; i >= 0; --i) {
      bytes[i] = bytes[i & 3];
      bytes[i].RightMultiplyBy(bytes[i >> 2]);
      unbytes[i] = unbytes[i & 3];
      unbytes[i].LeftMultiplyBy(unbytes[i >> 2]);
    }
    // 8-bit.
    for (int i = 0xff; i >= 0; --i) {
      bytes[i] = bytes[i & 0xf];
      bytes[i].RightMultiplyBy(bytes[i >> 4]);
      unbytes[i] = unbytes[i & 0xf];
      unbytes[i].LeftMultiplyBy(unbytes[i >> 4]);
    }
  }

  void Get(const int source) {
    a_ = GetInt(source);
    b_ = GetInt(source);
    c_ = GetInt(source);
    d_ = GetInt(source);
  }

  void RightMultiplyBy(const Hash& rhs) {
    const unsigned long long a = (unsigned long long)(a_) * rhs.a_ + (unsigned long long)(b_) * rhs.c_;
    const unsigned long long b = (unsigned long long)(a_) * rhs.b_ + (unsigned long long)(b_) * rhs.d_;
    const unsigned long long c = (unsigned long long)(c_) * rhs.a_ + (unsigned long long)(d_) * rhs.c_;
    const unsigned long long d = (unsigned long long)(c_) * rhs.b_ + (unsigned long long)(d_) * rhs.d_;
    a_ = a % kModulus;
    b_ = b % kModulus;
    c_ = c % kModulus;
    d_ = d % kModulus;
  }

  void Put(const int target) const {
    PutInt(target, a_);
    PutInt(target, b_);
    PutInt(target, c_);
    PutInt(target, d_);
  }

  void LeftUnUpdate32(const int a) {
    LeftUnUpdate16(a & 0xffff);
    LeftUnUpdate16(a >> 16);
  }

  void RightUnUpdate32(const int a) {
    RightUnUpdate16(a >> 16);
    RightUnUpdate16(a & 0xffff);
  }

  void RightUpdate32(const int a) {
    RightUpdate16(a & 0xffff);
    RightUpdate16(a >> 16);
  }

  void Print() const { cerr << a_ << ' ' << b_ << ' ' << c_ << ' ' << d_ << endl; }

  bool operator==(const Hash& rhs) const { return a_ == rhs.a_ && b_ == rhs.b_ && c_ == rhs.c_ && d_ == rhs.d_; }

 private:
  Hash(const unsigned int a, const unsigned int b, const unsigned int c, const unsigned int d) : a_(a), b_(b), c_(c), d_(d) {}

  void LeftMultiplyBy(const Hash& lhs) {
    const unsigned long long a = (unsigned long long)(lhs.a_) * a_ + (unsigned long long)(lhs.b_) * c_;
    const unsigned long long b = (unsigned long long)(lhs.a_) * b_ + (unsigned long long)(lhs.b_) * d_;
    const unsigned long long c = (unsigned long long)(lhs.c_) * a_ + (unsigned long long)(lhs.d_) * c_;
    const unsigned long long d = (unsigned long long)(lhs.c_) * b_ + (unsigned long long)(lhs.d_) * d_;
    a_ = a % kModulus;
    b_ = b % kModulus;
    c_ = c % kModulus;
    d_ = d % kModulus;
  }

  void LeftUnUpdate8(const int a) {
    LeftMultiplyBy(unbytes[a]);
  }

  void RightUnUpdate8(const int a) {
    RightMultiplyBy(unbytes[a]);
  }

  void RightUpdate8(const int a) {
    RightMultiplyBy(bytes[a]);
  }

  void LeftUnUpdate16(const int a) {
    LeftUnUpdate8(a & 0xff);
    LeftUnUpdate8(a >> 8);
  }

  void RightUnUpdate16(const int a) {
    RightUnUpdate8(a >> 8);
    RightUnUpdate8(a & 0xff);
  }

  void RightUpdate16(const int a) {
    RightUpdate8(a & 0xff);
    RightUpdate8(a >> 8);
  }

  static Hash bytes[1 << 8];
  static Hash unbytes[1 << 8];

  unsigned int a_, b_, c_, d_;
};

Hash Hash::bytes[1 << 8];
Hash Hash::unbytes[1 << 8];

void PosInit() {
  Hash::Init();
}

vector<int> Split(const long long len, const long long parts) {
  vector<int> ret(parts + 1);
  for (int i = 0; i <= parts; ++i) ret[i] = 1 + (len * i) / parts;
  return ret;
}

int main() {
  PosInit();

  long long S = SignalLength();
  long long M = SeqLength();

  const int node = MyNodeId();
  const int nodes = NumberOfNodes();
  vector<int> signal_bounds = Split(S, nodes);
  vector<int> seq_bounds = Split(M, nodes);

  {
    Hash signal_hash;
    Hash seq_hash;

    for (int i = signal_bounds[node]; i < signal_bounds[node + 1]; ++i) signal_hash.RightUpdate32(SignalAt(i));
    for (int i = seq_bounds[node]; i < seq_bounds[node + 1]; ++i) seq_hash.RightUpdate32(SeqAt(i));

    signal_hash.Put(0);
    seq_hash.Put(0);
    Send(0);
  }

  if (node == 0) {
    Hash signal_hash;
    for (int i = 0; i < nodes; ++i) {
      Receive(i);
      Hash hash;
      hash.Get(i);
      signal_hash.RightMultiplyBy(hash);
      hash.Get(i);
      for (int j = 0; j < nodes; ++j) hash.Put(j);
    }
    for (int j = 0; j < nodes; ++j) {
      signal_hash.Put(j);
      Send(j);
    }
    /*
    cerr << "Signal hash: ";
    signal_hash.Print();
    */
  }

  Receive(0);
  vector<Hash> seq_hash(nodes);
  for (int j = 0; j < nodes; ++j) seq_hash[j].Get(0);
  Hash signal_hash;
  signal_hash.Get(0);

  vector<int> start_bounds = Split(M - S, nodes);
  int begin = start_bounds[node];
  int end = begin + S;

  Hash hash;
  for (int i = 0; i < nodes; ++i) {
    const int trunc_begin = max(begin, seq_bounds[i]);
    const int trunc_end = min(end, seq_bounds[i + 1]);
    if (trunc_begin < trunc_end) {
      Hash trunc_hash;
      const int trunc_len = trunc_end - trunc_begin;
      if (trunc_len > (seq_bounds[i + 1] - seq_bounds[i]) / 2) {
        trunc_hash = seq_hash[i];
        for (int j = seq_bounds[i]; j < trunc_begin; ++j) trunc_hash.LeftUnUpdate32(SeqAt(j));
        for (int j = seq_bounds[i + 1] - 1; j >= trunc_end; --j) trunc_hash.RightUnUpdate32(SeqAt(j));
      } else {
        for (int j = trunc_begin; j < trunc_end; ++j) trunc_hash.RightUpdate32(SeqAt(j));
      }
      hash.RightMultiplyBy(trunc_hash);
    }
  }

  {
    long long res = 0;
    while (begin < start_bounds[node + 1]) {
      /*
      cerr << begin << ' ';
      hash.Print();
      */
      if (hash == signal_hash) {
        // cerr << "Occurrence at " << begin << endl;
        ++res;
      }
      hash.LeftUnUpdate32(SeqAt(begin++));
      hash.RightUpdate32(SeqAt(end++));
    }
    PutLL(0, res);
    Send(0);
  }

  if (node == 0) {
    long long res = 0;
    for (int i = 0; i < nodes; ++i) {
      Receive(i);
      res += GetLL(i);
    }
    cout << res << endl;
  }

  return 0;
}