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
#include <iomanip>
#include <iostream>
#include <utility>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <set>
#include <map>

#include "message.h"
#include "poszukiwania.h"

using namespace std;

#define ALL(x) x.begin(), x.end()
#define VAR(a,b) __typeof (b) a = b
#define IN(a) int a; cin >> a
#define IN2(a,b) int a, b; cin >> a >> b
#define REP(i,n) for (int _n=(n), i=0; i<_n; ++i)
#define FOR(i,a,b) for (int _b=(b), i=(a); i<=_b; ++i)
#define FORD(i,a,b) for (int _b=(b), i=(a); i>=_b; --i)
#define FORE(i,a) for (VAR(i,a.begin ()); i!=a.end (); ++i) 
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

typedef vector<int> VI;
typedef long long LL;
typedef pair<int,int> PII;
typedef double LD;

ostream& operator<<(ostream &o, const PII& p) {
  return o << "( " << p.first << ", " << p.second << ")";
}

int S, M;

int piece_len(int len, int machines_num, int my_id) {
  int fair_share = len / machines_num;
  int remaining = len - machines_num * fair_share;
  if (my_id < remaining)
    return fair_share + 1;
  return fair_share;
}

PII my_piece(int len, int machines_num, int my_id) {
  int beg = 0;
  for (int id = 0; id < my_id; ++id)
    beg += piece_len(len, machines_num, id);
  int end = beg + piece_len(len, machines_num, my_id);
  return MP(beg, end);
}

void test_my_piece() {
  assert(my_piece(1, 1, 0) == MP(0, 1));
  assert(my_piece(2, 1, 0) == MP(0, 2));

  assert(my_piece(1, 100, 0) == MP(0, 1));
  assert(my_piece(1, 100, 1) == MP(1, 1));
  assert(my_piece(1, 100, 2) == MP(1, 1));
  assert(my_piece(1, 100, 99) == MP(1, 1));

  assert(my_piece(2, 3, 0) == MP(0, 1));
  assert(my_piece(2, 3, 1) == MP(1, 2));
  assert(my_piece(2, 3, 2) == MP(2, 2));

  assert(my_piece(13, 4, 0) == MP(0, 4));
  assert(my_piece(13, 4, 1) == MP(4, 7));
  assert(my_piece(13, 4, 2) == MP(7, 10));
  assert(my_piece(13, 4, 3) == MP(10, 13));

  assert(my_piece(6, 3, 0) == MP(0, 2));
  assert(my_piece(6, 3, 1) == MP(2, 4));
  assert(my_piece(6, 3, 2) == MP(4, 6));
}

int machines_num, my_id;

LL x = int(1e9) + 3;
LL P = 2000000033;

map<int, LL> pow_x;

LL calc_pow_x(int k) {
  assert(x < P);
  if (k == 0) return 1;
  if (k == 1) return x;
  if (pow_x.count(k)) return pow_x[k];
  LL res;
  if (k & 1)
    res = (calc_pow_x(k - 1) * x) % P;
  else {
    res = calc_pow_x(k / 2);
    res = (res * res) % P;
  }
  pow_x[k] = res;
  return res;
}

inline int signal_at(int i) { return SignalAt(i + 1); }
inline int seq_at(int i) { return SeqAt(i + 1); }

LL calc_hash(PII subword, int is_signal) {
  LL res = 0;
  for (int i = subword.first; i < subword.second; ++i) {
    int c = is_signal == 1 ? signal_at(i) : seq_at(i);
    res = ((res * x) % P + c) % P;
  }
  return res;
}

struct hashed_range {
  PII range;
  LL hash;
  hashed_range() {}
  hashed_range(PII range, LL hash) : range(range), hash(hash) {}
  void put(int target) const {
    PutInt(target, range.first);
    PutInt(target, range.second);
    PutLL(target, hash);
  }
  void merge_with(const hashed_range& other) {
    assert(range.second == other.range.first);
    int other_len = other.range.second - other.range.first;
    LL shifted_hash = (hash * calc_pow_x(other_len)) % P;
    hash = (other.hash + shifted_hash) % P;
    range.second = other.range.second;
  }
  bool is_inside(PII other) const {
    return other.first <= range.first && range.second <= other.second;
  }
  void advance_seq() {
    int len = range.second - range.first;
    LL sub = (seq_at(range.first) * calc_pow_x(len - 1)) % P;
    hash = (hash + P - sub) % P;
    hash = (hash * x) % P;
    hash = (hash + seq_at(range.second)) % P;
    ++range.first;
    ++range.second;
  }
};

void send_one(int target, const hashed_range& range) {
  range.put(target);
  Send(target);
}

void send_all(int target, const vector<hashed_range>& ranges) {
  assert(ranges.size() == machines_num);
  PutInt(target, ranges.size());
  FORE(it, ranges) it->put(target);
  Send(target);
}

hashed_range get_one(int source) {
  int beg = GetInt(source);
  int end = GetInt(source);
  LL hash = GetLL(source);
  return hashed_range(MP(beg, end), hash);
}

hashed_range receive_one(int source) {
  Receive(source);
  return get_one(source);
}

void get_all(int source, vector<hashed_range>& res) {
  Receive(source);
  int cnt = GetInt(source);
  REP(i, cnt)
    res.PB(get_one(source));
}

LL signal_hash;
vector<hashed_range> seq_ranges;

void calculate_ranges(int is_signal, vector<hashed_range>& res) {
  int len = is_signal == 1 ? S : M;
  hashed_range range;
  range.range = my_piece(len, machines_num, my_id);
  range.hash = calc_hash(range.range, is_signal);
  if (my_id == 0) {
    res.PB(range);
    for (int id = 1; id < machines_num; ++id)
      res.PB(receive_one(id));
    for (int id = 1; id < machines_num; ++id)
      send_all(id, res);
  }
  else {
    send_one(0, range);
    get_all(0, res);
  }
}

void calculate_signal_hash() {
  vector<hashed_range> ranges;
  calculate_ranges(1, ranges);
  assert(ranges.size() == machines_num);
  for (int i = 1; i < machines_num; ++i)
    ranges[0].merge_with(ranges[i]);
  signal_hash = ranges[0].hash;
}

void calculate_seq_ranges() {
  calculate_ranges(0, seq_ranges);
}

hashed_range construct_seq_hash(PII range) {
  int k = seq_ranges.size();
  int first_inside = 0;
  while (first_inside < k && !seq_ranges[first_inside].is_inside(range)) {
    ++first_inside;
  }
  hashed_range res;
  if (first_inside == k) {
    res.range = range;
    res.hash = calc_hash(range, 0);
  }
  else {
    res.range = MP(range.first, seq_ranges[first_inside].range.first);
    res.hash = calc_hash(res.range, 0);
    while (first_inside < k && seq_ranges[first_inside].is_inside(range)) {
      res.merge_with(seq_ranges[first_inside]);
      ++first_inside;
    }
    hashed_range remainder;
    remainder.range = MP(res.range.second, range.second);
    remainder.hash = calc_hash(remainder.range, 0);
    res.merge_with(remainder);
    assert(res.range == range);
  }
  return res;
}

int local_res = 0;

const int to_check = 5;
const int random_check[] = {0, 17, 423, 4378324, 3339234};

bool sanity_check(hashed_range window) {
  REP(i, to_check) {
    if (random_check[i] >= S)
      break;
    if (signal_at(random_check[i]) != seq_at(window.range.first + random_check[i]))
      return false;
  }
  return true;
}

void sanity_check_and_maybe_inc_res(hashed_range window) {
  if (sanity_check(window)) ++local_res;
}

void gather_results() {
  if (my_id == 0) {
    for (int id = 1; id < machines_num; ++id) {
      Receive(id);
      local_res += GetInt(id);
    }
    cout << local_res << endl;
  }
  else {
    PutInt(0, local_res);
    Send(0);
  }
}

int main() {
   ios_base::sync_with_stdio(0);
   cout.setf(ios::fixed);
   machines_num = NumberOfNodes();
   my_id = MyNodeId();
   test_my_piece();
   S = SignalLength();
   M = SeqLength();
   calculate_signal_hash();
   calculate_seq_ranges();
   int positions_to_check = M - S + 1;
   PII my_positions = my_piece(positions_to_check, machines_num, my_id);
   if (my_positions.first == my_positions.second) {
     gather_results();
     return 0;
   }
   hashed_range window = construct_seq_hash(MP(my_positions.first, my_positions.first + S));
   while (window.range.first < my_positions.second) {
     if (window.hash == signal_hash)
       sanity_check_and_maybe_inc_res(window);
     if (window.range.first + 1 == my_positions.second) break;
     window.advance_seq();
   }
   gather_results();
   return 0;
}