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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "poszukiwania.h"
#include "message.h"

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

typedef long long ll;

//#define mod 981168724994134051ll
#define mod 1800040013 

int S;
ll M;
int myId;
int nodes;

ll* finger;
int fingerPerNode;


void print_mat(ll* M) {
 cout << M[0] << " " << M[1] << "\n";
 cout << M[2] << " " << M[3] << "\n";
}

ll mulmod(ll a, ll b){
  return (a*b)%mod;
  /*  
  ll x = 0,y=a;
  ll t;   
  while(b !=0){
    if((b & 1) !=0){
      t = x+y;
      x = (t>mod) ? t-mod : t;
    }
    t = y<<1;
    y = (t>mod) ? t-mod : t;        
    b >>=1;
  }
  return x;   */ 
}

ll pow(ll x, ll k) {
		if(k==1)return x;
		ll y = pow(x, k/2);
		ll r = mulmod(y,y);
		if((k&1)==1) {
			r = mulmod(r,x);
		}
		return r;
}

ll inv(long x) {
  return pow(x, mod-2);
}

ll* mul(ll* a, ll* b) {
  ll* res = new ll[4];
  for(int i=0;i<2;i++)for(int j=0;j<2;j++){
      res[2*i+j]=0;
			for(int k=0;k<2;k++){
				res[2*i+j]+=a[2*i+k]*b[2*k+j];//mulmod(a[2*i+k],b[2*k+j]);
			}
			res[2*i+j] %=mod;
		}
  return res;
}

inline void mul2(ll* a, ll* b, ll* res) {
  res[0]=(a[0]*b[0] + a[1]*b[2])%mod;
  res[1]=(a[0]*b[1] + a[1]*b[3])%mod;
  res[2]=(a[2]*b[0] + a[3]*b[2])%mod;
  res[3]=(a[2]*b[1] + a[3]*b[3])%mod;
      /*
  for(int i=0;i<2;i++)for(int j=0;j<2;j++){
      res[2*i+j]=(a[2*i]*b[j] + a[2*i+1]*b[2+j])%mod;
			for(int k=0;k<2;k++){
				res[2*i+j]+=a[2*i+k]*b[2*k+j];
				//res[2*i+j]+=mulmod(a[2*i+k],b[2*k+j]);
			}
			res[2*i+j] %=mod;
		}
      */
}

bool eq(ll*a, ll*b) {
  for(int i=0;i<4;i++)if(a[i]!=b[i])return false;
  return true;
}

void reverse(ll* M, ll*R) {
  ll d = (M[0] * M[3] + mod - (M[1] * M[2])%mod) % mod;
  d = inv(d);
  R[0]=(M[3] * d)%mod;
  R[3]=(M[0] *d)%mod;
  R[1]=((mod-M[1]) *d)%mod;
  R[2]=((mod-M[2]) * d) % mod;
}

ll* clone(ll* m) {
  ll* res = new ll[4];
  memcpy(res, m, 4 * sizeof(ll));
  return res;
}

ll* create(ll a, ll b, ll c, ll d) {
  ll* res = new ll[4];
  res[0]=a;res[1]=b;res[2]=c;res[3]=d;
  return res;
}

ll** leftM;
ll** revLeftM;
ll mask15 = (1<<15) -1;

ll* hashbits(int x, int bitFrom, int bitTo) {
  ll* res = create(1,0,0,1); 
  ll* m0 = create(1,0,1,1);
  ll* m1 = create(1,1,0,1);
  for(int b =bitFrom; b < bitTo;b++) {
    ll* nres;
    if(x & (1<<b)) {
        nres = mul(res,m1);
    } else {
       nres = mul(res,m0);
    }
    delete(res); 
    res=nres;
  }
  return res;
}

int init(){
  leftM = new ll*[1<<15];
  revLeftM = new ll*[1<<15];
  for(int x = 0;x<1<<15;x++) {
    leftM[x] = hashbits(x,0,15);
    revLeftM[x]=new ll[4];
    reverse(leftM[x], revLeftM[x]); 
  }
}

inline void fastHash(int x, ll*res) {
  ll* left = leftM[x & mask15];
  ll* right =leftM[x>>15];
  mul2(left,right, res);
}

inline void fastHashRev(int x, ll*res) {
  ll* left = revLeftM[x & mask15];
  ll* right =revLeftM[x>>15];
  mul2(right,left, res);
}

void hashNext(ll*cur, int x, ll*res) {
  static ll*m = new ll[4];
  fastHash(x,m);
  mul2(cur,m,res);
}

void hashNextRev(ll*cur, int x, ll*res) {
  static ll*m = new ll[4];
  fastHashRev(x,m);
  mul2(m,cur,res);
}

ll* hash1(int*a, ll n) {
  ll* res = create(1,0,0,1); 
  ll* tmp = new ll[4];
  for(int i=0;i<n;i++) {
    hashNext(res,a[i],tmp);
    ll*t = res;
    res = tmp;
    tmp = t;
  }
  return res;
}



void sendMat(int target, ll*M) {
  for(int i=0;i<4;i++)
    PutLL(target, M[i]);
  Send(target);
}

ll* receiveMat(int target) {
  Receive(target);
  ll* res = new ll[4];
  for(int i=0;i<4;i++)res[i]=GetLL(target);
  return res;
}

void doFinger0() {
  for(int node = 1; node < nodes; node++) {
    ll* p = receiveMat(node);
    ll* nf = mul(finger, p);
    delete(finger);delete(p);
    finger = nf;
  }
  for(int node = 1; node < nodes; node++) {
    sendMat(node, finger);
  }
  //print_mat(finger);
}

void dofinger() {
  int perNode = (int)((S+nodes-1)/nodes);
  fingerPerNode = perNode;
  int myStart = myId * perNode;
  int myEnd = min((int)S, myStart+perNode);
  int myCount = myEnd-myStart;
  int *S = new int[myCount];
  for(int i=myStart;i<myEnd;i++) {
    S[i-myStart] = SignalAt(i+1);
  }
  ll* part = hash1(S, myCount);
  if (myId == 0) {
    finger = part;
    doFinger0();
  }
  else {
    sendMat(0, part);
    finger = receiveMat(0);
    //print_mat(finger);
  } 
}

void doHash0(ll* mine) {
  sendMat(1, mine);
  ll* pref = mine;
  for(int node = 1; node + 1 < nodes;node++) {
    ll* r = receiveMat(node);
    pref = mul(pref, r);
    sendMat(node+1, pref);
  }
}

int* loadInverse(int start, int end) {
  int count = end - start;
  int *a= new int[count];
  for(int i=start;i<end;i++) {
    a[i-start] = SeqAt(i+1);
  }
  return a;
}


// do, do hash, do hash nicht
void doHash() {
  int perNode = (int)((M+nodes-1)/nodes);
  int myStart = myId * perNode;
  int myEnd = min((int)M, myStart+perNode);
  int myCount = myEnd-myStart;
  int *a = new int[myCount];
  for(int i=myStart;i<myEnd;i++) {
    a[i-myStart] = SeqAt(i+1);
  }
  ll* last = hash1(a, myCount);
  ll* myPref;
  if(myId > 0 && myId +1 < nodes) {
    sendMat(0, last);
  }
  if(myId==0) {
    doHash0(last);
    myPref = create(1,0,0,1);
  } else {
    myPref = receiveMat(0);
  }
  int waitFrom = -1;
  int myInverseStart = -1;
  int myInverseEnd = -1; 
  for(int node =0;node<nodes;node++) {
    int nodeStart = node * perNode;
    int nodeEnd = min((int)M, nodeStart+perNode);
    if (nodeEnd >= S) {
      int nodeInverseStart = max(0, nodeStart -S);
      int nodeInverseEnd = max(0, nodeEnd -S);
      if(node == myId) {
        myInverseStart = nodeInverseStart;
        myInverseEnd = nodeInverseEnd;
      }
      if(nodeInverseStart >0) {
        int nodeNeeds = nodeInverseStart -1;
        int owner = nodeNeeds / perNode;
        if(owner == myId) {
          int idx = nodeNeeds - myStart;
          ll* h = hash1(a,idx+1);
          ll* p = mul(myPref, h);
          sendMat(node, p);
        }
        if(node == myId) waitFrom = owner;
      }
    }
  }
  ll* inversePref;
  if (waitFrom == -1) inversePref = create(1,0,0,1);
  else inversePref = receiveMat(waitFrom);
  int* Inv = NULL;
  if (myInverseEnd > 0) {
    Inv = loadInverse(myInverseStart, myInverseEnd);
  } 
  int result =0;
  ll * currentHash = myPref;
  ll * currentInver = new ll[4];
  reverse(inversePref, currentInver);
  ll* tmp = new ll[4];
  for(int id = myStart;id<myEnd;id++) {
    hashNext(currentHash, a[id-myStart],tmp);
    ll*t = currentHash; currentHash=tmp;tmp=t;
    ll* mat = currentHash;
    if(id<S-1)continue;
    if(id>S-1) {
      int inverseId = id - S;
      int in = Inv[inverseId - myInverseStart];
      hashNextRev(currentInver, in,tmp);
      t = currentInver; currentInver=tmp;tmp=t;
      mul2(currentInver, currentHash,tmp);
      mat = tmp;
    }
    if(eq(mat,finger)) {
      result++;
    }
  }
  if (myId !=0) {
    PutInt(0, result);
    Send(0);
  } else {
    for(int i=1;i<nodes;i++) {
      Receive(i);
      int x = GetInt(i);
      result+=x;
    }
    cout << result <<"\n";
  }
}

int main() {
  M = SeqLength();
  S = (int)SignalLength();
  myId = MyNodeId();
  nodes = NumberOfNodes();
  if(nodes > S/2) nodes = S/2;
  if(myId>=nodes)return 0;
  init();
  dofinger();
  doHash();
  return 0;
}