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
#include "poszukiwania.h"
#include "message.h"
#include <bits/stdc++.h>
#include <assert.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define all(v) (v).begin(),(v).end()

#define VI vector<int>
#define PII pair<int,int>
#define st first
#define nd second
#define mp make_pair
#define pb push_back
#define lint long long int

#define debug(x) {cerr <<#x <<" = " <<x <<endl; }
#define debug2(x,y) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y <<endl; } 
#define debug3(x,y,z) {cerr <<#x <<" = " <<x << ", "<<#y<<" = "<< y << ", " << #z << " = " << z <<endl; } 
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<endl; }}
#define debugt(t,n) {{cerr <<#t <<" = "; FOR(it,0,(n)) cerr <<t[it] <<", "; cerr <<endl; }}

using namespace std;

int n, s, nodes, myId, HASH, HASH2;

int p = 1000000931;
int q = 1070000027;

int p2 = 1000000241;
int q2 = 1070000221;

int powmod(int a, int n, int mod) {
  if (n==0) return 1;
  int x = powmod(a, n/2, mod);
  x = (x*1LL*x)%mod;
  if (n % 2 == 0) {
    return x;
  } else {
    return (x *1LL* a)%mod;
  }
}
 
void HashujWzorzec(){
  int myHash = 0;
  int myHash2 = 0;
  int skok = (s/nodes)+1;
  
  FOR(i, myId*skok, min(myId*skok+skok,s)) {
    myHash = (myHash*1LL*p)%q;
    myHash2= (myHash2*1LL*p2)%q2;
    myHash = (myHash+  SignalAt(i+1));
    myHash2 = (myHash2+  SignalAt(i+1));
    if (myHash >= q) myHash-=q;
    if (myHash2 >= q2) myHash2-=q2;
  }
  PutInt(0, myHash);
  PutInt(0, myHash2);
  Send(0);
}

void ZlozHashWzorca(){
      VI hashe;
      VI hashe2;
      int skok = (s/nodes) + 1;
      FOR(i,0,nodes) {
        Receive(i);
        hashe.pb(GetInt(i));
        hashe2.pb(GetInt(i));
      }
      HASH = 0;
      HASH2 = 0;
      FOR(i,0,nodes) {
        HASH = (HASH * 1LL * powmod(p,max(0,min(skok,s-i*skok)),q))%q;
        HASH2 = (HASH2 * 1LL * powmod(p2,max(0,min(skok,s-i*skok)),q2))%q2;
        HASH = (HASH+hashe[i]);
        HASH2 = (HASH2+hashe2[i]);
        if (HASH >= q) HASH -= q;
        if (HASH2 >= q2) HASH2 -=q2;
      }
      FOR(i,0,nodes) {
        PutInt(i, HASH);
        PutInt(i, HASH2);
        Send(i);
      }
}

void odbierzLokalneWypisz() {
      int res = 0;
      FOR(i,0,nodes) {
        Receive(i);
        res += GetInt(i);
      }
      printf("%d\n",res);
}

void liczHashKrotki() {
  HASH = 0;
  HASH2 = 0;
  FOR(i,0,s) {
    HASH = (HASH * 1LL * p) % q;
    HASH2 = (HASH2 * 1LL * p2) % q2;
    HASH = (HASH + SignalAt(i + 1));
    HASH2 = (HASH2 + SignalAt(i + 1));
    if (HASH>=q) HASH-=q;
    if (HASH2>=q2) HASH2-=q2;
  }
}

PII startowyHashKrotki(int start) {
  int res = 0;
  int res2 = 0;
  FOR(i,start,start+s) {
    res = (res*1LL*p)%q;
    res2 = (res2*1LL*p2)%q2;
    res = (res+SeqAt(i+1));
    res2 = (res2+SeqAt(i+1));
    if(res>=q) res-=q;
    if(res2>=q2) res2-=q2;
  }
  return mp(res,res2);
}

void krotkiWzorzec() {
   liczHashKrotki();
   int myAns = 0;
   int skok = (n-s+1)/nodes + 1;
   int start = skok*myId;
   int stop  = min(skok*myId + skok, n-s+1);
   
   if (start + s > n) {
     PutInt(0,0);
     Send(0);
   } else {
    PII myHashPara = startowyHashKrotki(start);
    int myHash = myHashPara.st;
    int myHash2 = myHashPara.nd;

    int bp = q - powmod(p,s,q);
    int bp2 = q2 - powmod(p2,s,q2);
    FOR(i, start, stop) {
      //debug2(myHash, HASH);
      if(myHash == HASH && myHash2 == HASH2) myAns++;
      if (i+s+1 <= n) {
        myHash = (myHash*1LL*p)%q;
        myHash2 = (myHash2*1LL*p2)%q2;
        myHash = (myHash+SeqAt(i+s+1));
        myHash2 = (myHash2+SeqAt(i+s+1));
        if (myHash >= q) myHash -= q;
        if (myHash2 >= q2) myHash2 -= q2;
        myHash = (myHash+((bp*1LL*SeqAt(i+1))%q));
        myHash2 = (myHash2+((bp2*1LL*SeqAt(i+1))%q2));
        if (myHash >= q) myHash -= q;
        if (myHash2 >= q2) myHash2 -= q2;
      }
    }
    PutInt(0,myAns);
    Send(0);
   }

   if (myId == 0) odbierzLokalneWypisz();
}

VI pohashowal;
VI pohashowal2;

void HashujTekst(){
    int skok3 = (n/(2*nodes)+1);
    int locHash1 = 0;
    int locHash12 = 0;
    int locHash2 = 0;
    int locHash22 = 0;
    int start = 2*myId*skok3;
    int middle = min(2*myId*skok3+skok3,n);
    int end = min(2*myId*skok3+2*skok3,n);
    FOR(i, start, middle) {
      locHash1 = (locHash1*1LL*p)%q;
      locHash12 = (locHash12*1LL*p2)%q2;
      locHash1 = (locHash1+SeqAt(i+1));
      locHash12 = (locHash12+SeqAt(i+1));
      if(locHash1>=q) locHash1 -=q;
      if(locHash12>=q2) locHash12 -=q2;
    }
    FOR(i, middle, end) {
      locHash2 = (locHash2*1LL*p)%q;
      locHash22 = (locHash22*1LL*p2)%q2;
      locHash2 = (locHash2+SeqAt(i+1));
      locHash22 = (locHash22+SeqAt(i+1));
      if(locHash2>=q) locHash2 -=q;
      if(locHash22>=q2) locHash22 -=q2;
    }
    /*FOR(i,0,nodes) {
      PutInt(0, locHash1);
      PutInt(0, locHash2);
      Send(0);
    }
    if (myId==0) {
      FOR(i,0,nodes) {
        Receive(i);
        pohashowal.pb(GetInt(i));
        pohashowal.pb(GetInt(i));
      }
      FOR(i,1,nodes){
        FOR(j,0,2*nodes){
          PutInt(i,pohashowal[j]);
        }
        Send(i);
      }
    } else {
      Receive(0);
      FOR(i,0,2*nodes) {
        pohashowal.pb(GetInt(0));
      }
    }*/
    FOR(i,0,nodes) {
      PutInt(i, locHash1);
      PutInt(i, locHash12);
      PutInt(i, locHash2);
      PutInt(i, locHash22);
      Send(i);
    }
    FOR(i,0,nodes) {
      Receive(i);
      pohashowal.pb(GetInt(i));
      pohashowal2.pb(GetInt(i));
      pohashowal.pb(GetInt(i));
      pohashowal2.pb(GetInt(i));
    }
}

PII zlozPoczatkowyHash(int start) {
  int res = 0;
  int res2 = 0;
  int koniec = start+s;
  int dlugoscBloku = n/(2*nodes) + 1;
  int potega = powmod(p,dlugoscBloku,q);
  int potega2 = powmod(p2,dlugoscBloku,q2);
  int i = start;

  while (i != koniec) {
    if ( (i % dlugoscBloku) != 0 || (i + dlugoscBloku > koniec) ) {
      res = (res*1LL*p)%q;
      res2 = (res2*1LL*p2)%q2;
      res = (res + SeqAt(i+1));
      res2 = (res2 + SeqAt(i+1));
      if(res>=q) res-=q;
      if(res2>=q2) res2-=q2;
      i++;
    } else {
      if (i + dlugoscBloku <= koniec){
        res = (res*1LL*potega)%q;
        res2 = (res2*1LL*potega2)%q2;
        res = (res+pohashowal[i/dlugoscBloku]);
        res2 = (res2+pohashowal2[i/dlugoscBloku]);
      if(res>=q) res-=q;
      if(res2>=q2) res2-=q2;
        i += dlugoscBloku;
      }
    }
  }
  return mp(res,res2);
}

void dlugiWzorzec() {
  HashujWzorzec();
  if (myId == 0) ZlozHashWzorca();
  
  Receive(0);
  HASH = GetInt(0);
  HASH2 = GetInt(0);
  HashujTekst();
  
  int myAns = 0;
  int skok = (n-s+1)/nodes + 1;
  int start = skok*myId;
  int stop  = min(skok*myId + skok, n-s+1);
  
  if (start + s > n) {
    PutInt(0,0);
    Send(0);
  } else {
    PII myHashPara = zlozPoczatkowyHash(start);
    int myHash = myHashPara.st;
    int myHash2 = myHashPara.nd;

    int bp = q - powmod(p,s,q);
    int bp2 = q2 - powmod(p2,s,q2);
    FOR(i, start, stop) {
      if(myHash == HASH && myHash2==HASH2) myAns++;
      if (i+s+1 <= n) {
        myHash = (myHash*1LL*p)%q;
        myHash2 = (myHash2*1LL*p2)%q2;
        myHash = (myHash+SeqAt(i+s+1));
        myHash2 = (myHash2+SeqAt(i+s+1));
        if(myHash >=q) myHash-=q;
        if(myHash2 >=q2) myHash2-=q2;
        myHash = (myHash+((bp*1LL*SeqAt(i+1))%q));
        myHash2 = (myHash2+((bp2*1LL*SeqAt(i+1))%q2));
        if(myHash >=q) myHash-=q;
        if(myHash2 >=q2) myHash2-=q2;
      }
    }
    PutInt(0,myAns);
    Send(0);
  }
  if (myId == 0) odbierzLokalneWypisz();
}

int main() {
  n = SeqLength();
  s = SignalLength();
  nodes = NumberOfNodes();
  myId = MyNodeId();
  if (s < n/nodes || s < 1e5) krotkiWzorzec();
  else dlugiWzorzec();
}