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

#include <bits/stdc++.h>
using namespace std;

const long long D1 = 1e9 + 7;
const long long P1 = 1e9 + 9;
const long long D2 = 999009019;
const long long P2 = 1003162753;

long long Pow(long long a, long long d, long long p) {
  long long q = 1;
  while (d > 0) {
    if (d % 2 == 1)
      q = (q * a) % p;
    a = (a * a) % p;
    d /= 2;
  }
  return q;
}

typedef pair<long long, long long> Hash;

ostream& operator<<(ostream& o, Hash hash) {
  return o << hash.first << " " << hash.second;
}

void PutHash(int i, Hash hash) {
  PutLL(i, hash.first);
  PutLL(i, hash.second);
}

Hash GetHash(int i) {
  Hash hash;
  hash.first = GetLL(i);
  hash.second = GetLL(i);
  return hash;
}

Hash AddToHash(Hash hash, long long symbol) {
  hash.first = (hash.first * D1 + symbol) % P1;
  hash.second = (hash.second * D2 + symbol) % P2;
  return hash;
}

Hash MergeHash(Hash left, long long width, Hash right) {
  left.first = (left.first * Pow(D1, width, P1) + right.first) % P1;
  left.second = (left.second * Pow(D2, width, P2) + right.second) % P2;
  return left;
}

Hash MoveHash(Hash hash, Hash power, long long prev, long long next) {
  // cerr << "move hash " << hash << " with power " << power << "and prev next " << prev << " " << next << " = ";
  hash.first = (hash.first * D1 - power.first * prev + next) % P1;
  hash.second = (hash.second * D2 - power.second * prev + next) % P2;
  // cerr << hash << endl;
  return hash;
}

Hash Normalize(Hash hash) {
  if (hash.first < 0) hash.first += P1;
  if (hash.second < 0) hash.second += P2;
  return hash;
}

int main() {
  long long N = SeqLength();
  long long M = SignalLength();
 
  int sig_from = MyNodeId() * M / NumberOfNodes();
  int sig_to = (1 + MyNodeId()) * M / NumberOfNodes();

  Hash hash(0, 0);
  for (int i = sig_from; i < sig_to; ++i)
    hash = AddToHash(hash, SignalAt(i + 1));

  if (MyNodeId() > 0) {
    PutHash(0, hash);
    Send(0);
  } else {
    for (int i = 1; i < NumberOfNodes(); ++i) {
      int width = ((1 + i) * M / NumberOfNodes()) - (i * M / NumberOfNodes());
      Receive(i);
      hash = MergeHash(hash, width, GetHash(i));
    }
  }

  if (MyNodeId() == 0) {
    for (int i = 1; i < NumberOfNodes(); ++i) {
      PutHash(i, hash);
      Send(i);
    }
  } else {
    Receive(0);
    hash = GetHash(0);
  }

  // cerr << "Hash = " << hash << endl;

  int seq_from = MyNodeId() * N / NumberOfNodes();
  int seq_to = (1 + MyNodeId()) * N / NumberOfNodes();

  Hash seq_hash(0, 0);
  for (int i = seq_from; i < seq_to; ++i)
    seq_hash = AddToHash(seq_hash, SeqAt(i + 1));
  
  for (int i = 0; i < NumberOfNodes(); ++i) {
    PutHash(i, seq_hash);
    Send(i);
  }

  Hash seq_hashes[100];
  long long seq_lefts[100], seq_rights[100];

  for (int i = 0; i < NumberOfNodes(); ++i) {
    Receive(i);
    seq_hashes[i] = GetHash(i);
    seq_lefts[i] = i * N / NumberOfNodes();
    seq_rights[i] = (1 + i) * N / NumberOfNodes();
  }

  long long CAND = N - M + 1;

  int cand_from = MyNodeId() * CAND / NumberOfNodes();
  int cand_to = (1 + MyNodeId()) * CAND / NumberOfNodes();

  long long result = 0;

  if (cand_from < cand_to) {
    long long first_cand_first = cand_from;
    long long first_cand_last = cand_from + M - 1;
    int chunk_first = -1, chunk_last = -1;
    for (int i = 0; i < NumberOfNodes(); ++i) {
      if (seq_lefts[i] <= first_cand_first && first_cand_first < seq_rights[i])
        chunk_first = i;
      if (seq_lefts[i] <= first_cand_last && first_cand_last < seq_rights[i])
        chunk_last = i;
    }
    assert(chunk_first != -1 && chunk_last != -1 && chunk_first <= chunk_last);

    Hash cand_hash(0, 0);
    if (chunk_first == chunk_last) {
      for (int i = first_cand_first; i <= first_cand_last; ++i)
        cand_hash = AddToHash(cand_hash, SeqAt(1 + i));
    } else {
      for (int i = first_cand_first; i < seq_rights[chunk_first]; ++i)
        cand_hash = AddToHash(cand_hash, SeqAt(1 + i));
      for (int i = chunk_first + 1; i < chunk_last; ++i)
        cand_hash = MergeHash(cand_hash, seq_rights[i] - seq_lefts[i], seq_hashes[i]);
      for (int i = seq_lefts[chunk_last]; i <= first_cand_last; ++i)
        cand_hash = AddToHash(cand_hash, SeqAt(1 + i));
    }
    cand_hash = Normalize(cand_hash);
     
    Hash power(Pow(D1, M, P1), Pow(D2, M, P2));
    if (cand_hash == hash)
      ++result;
    // cerr << "Cand = " << cand_hash << " (first)" << endl;
    for (int i = cand_from; i + 1 < cand_to; ++i) {  // verify range
      cand_hash = MoveHash(cand_hash, power, SeqAt(1 + i), SeqAt(1 + i + M));
      // cerr << "Cand = " << cand_hash << endl;
      cand_hash = Normalize(cand_hash);
      if (cand_hash == hash)
        ++result;
    }
  }

  if (MyNodeId() > 0) {
    PutLL(0, result);
    Send(0);
  } else {
    for (int i = 1; i < NumberOfNodes(); ++i) {
      Receive(i);
      result += GetLL(i);
    }
    cout << result << endl;
  }

  return 0;
}