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
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <vector>
#include <utility>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include "message.h"
#include "poszukiwania.h"

#define ST first
#define ND second
#define MP make_pair
#define PB push_back
#define QI queue <int>
#define VI vector <int>
#define PII pair <int,int>
#define SZ(x) (int)(x).size()
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
#define SYNC ios_base::sync_with_stdio(0)
#define MAX(a,b) ((a) > (b)) ? a : b

using namespace std;

typedef long long LL;
typedef long double LD;
typedef unsigned int UI;
typedef unsigned long long ULL;

const LL MOD = 1000000000000037LL;
const LL PRIME = 1e9 + 7;

int a, b, c, d, e, f;
LL sumHash, signalHash, wynik, ilezgodnych, hashh, poczatek, koniec, firstSeq;
LL primePow, primePow2, primePow3, n, m, maszyny, podzial;

vector <LL> Start;

LL Multiply(LL,LL,LL);

int main()
{
  n = (int)SignalLength();
  m = (int)SeqLength();
  maszyny = NumberOfNodes();
  primePow = primePow2 = primePow3 = 1;
  podzial = (n + maszyny - 1) / maszyny;
  
  for(int i = 1; i <= podzial; i++)
  {
    primePow = Multiply(primePow, PRIME, MOD);
  }
  for(int i = 1; i <= n; i++)
  {
    primePow2 = Multiply(primePow2, PRIME, MOD);
  }
  for(int i = 1; i <= n - podzial; i++)
  {
    primePow3 = Multiply(primePow3, PRIME, MOD);
  }
  
  //obliczamy hasza dla wzorca
  poczatek = podzial * MyNodeId() + 1;
  koniec = min(poczatek + podzial - 1, n);
  
  for(int i = poczatek; i <= koniec; i++)
  {
    sumHash = Multiply(sumHash, PRIME, MOD);
    sumHash = (sumHash + SignalAt(i)) % MOD;  
  }
  PutLL(0, sumHash);
  Send(0);
  if(MyNodeId() == 0)
  {
    for(int i = 0; i < maszyny; i++)
    {
      signalHash = Multiply(signalHash, primePow, MOD);
      Receive(i);
      signalHash = (signalHash + GetLL(i)) % MOD;
    }
    for(int i = 0; i < maszyny; i++)
    {
      PutLL(i, signalHash);
      Send(i);
    }
  }
  Receive(0);
  signalHash = GetLL(0);
  
  
  //obliczamy pierwszego hasza dla ciagu
  sumHash = 0;
  for(int i = poczatek; i <= koniec; i++)
  {
    sumHash = Multiply(sumHash, PRIME, MOD);
    sumHash = (sumHash + SeqAt(i)) % MOD;  
  }
  PutLL(0, sumHash);
  Send(0);
  if(MyNodeId() == 0)
  {
    for(int i = 0; i < maszyny; i++)
    {
      firstSeq = Multiply(firstSeq, primePow, MOD);
      Receive(i);
      firstSeq = (signalHash + GetLL(i)) % MOD;
    }
    Start.PB(firstSeq);
  }
  
  //obliczamy kolejne hasze startowe
  if(n > podzial)
  {
    podzial = (m - n + maszyny) / maszyny;
    
    poczatek = (podzial - 1) * MyNodeId() + 1;
    koniec = min(poczatek + podzial - 1, m);
    hashh = 0;
    for(int i = poczatek; i <= koniec; i++)
    {
      hashh = Multiply(hashh, PRIME, MOD);
      hashh = (hashh + SignalAt(i)) % MOD;
    }
    PutLL(0, hashh);
    Send(0);
    
    poczatek = poczatek + n;
    koniec = min(poczatek + podzial - 1, m);
    hashh = 0;
    for(int i = poczatek; i <= koniec; i++)
    {
      hashh = Multiply(hashh, PRIME, MOD);
      hashh = (hashh + SignalAt(i)) % MOD;
    }
    PutLL(0, hashh);
    Send(0);
  }
  else
  {
    podzial = (m - n + maszyny) / maszyny;
    poczatek = podzial * MyNodeId() + 1;
    koniec = poczatek + n - 1;
    hashh = 0;
    for(int i = poczatek; i <= koniec; i++)
    {
      hashh = Multiply(hashh, PRIME, MOD);
      hashh = (hashh + SignalAt(i)) % MOD;
    }
    PutLL(0, hashh);
    Send(0);
  }
  
  
  if(MyNodeId() == 0)
  {
    if(n > podzial)
    {
      hashh = firstSeq;
      for(int i = 1; i < maszyny; i++)
      {
        Receive(i);
        LL pocz = GetLL(i);
        pocz = Multiply(pocz, primePow3, MOD);
        LL kon = GetLL(i);
        hashh = (hashh - pocz + MOD) % MOD;
        hashh = Multiply(hashh, primePow, MOD);
        hashh = (hashh + kon) % MOD;
        Start.PB(hashh);
      }
    }
    else
    {
      for(int i = 1; i < maszyny; i++)
      {
        Receive(i);
        Start.PB(GetLL(i));
      }
    }
    for(int i = 0; i < maszyny; i++)
    {
      PutLL(i, Start[i - 1]);
      Send(i);
    }
  }
  
  //obliczamy hasze kolejny podslow
  Receive(0);
  hashh = GetLL(0);
  ilezgodnych += (hashh == signalHash);
  podzial = (m - n + maszyny) / maszyny;
  
  poczatek = podzial * MyNodeId() + 1;
  koniec = min(poczatek + podzial - 1, n);
  for(int i = poczatek; i <= koniec - 1; i++)
  {
    LL val = Multiply(SeqAt((LL)(i)), primePow2, MOD);
    hashh = (hashh - val + MOD) % MOD;
    hashh = Multiply(hashh, PRIME, MOD);
    hashh = (hashh + SeqAt((LL)(i + podzial))) % MOD;
    ilezgodnych += (hashh == signalHash);
  }
  PutLL(0, ilezgodnych);
  Send(0);
  
  if(MyNodeId() == 0)
  {
    for(int i = 0; i < maszyny; i++)
    {
      Receive(i);
      wynik += GetLL(i);
    }
    printf("%lld\n", wynik);
  }
}

LL Multiply(LL A, LL B, LL M)
{
  LL res = 0;
  do
  {
    if(A & 1)
    {
      res += B;
      res %= M;
    }
    B += B;
    B %+ M;
  }
  while(A >>= 1);
  return res;
}