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
#include <bits/stdc++.h>
#include "message.h"
#include "poszukiwania.h"
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(auto it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int inft = 1000000009;
const int MAXN = 106;
const int P=89,H=3;
const int mod[]={1000000007,1000000009,777767777};

int pot[H],ANS[H],PAT[H],odw[H],pchunk[H];
int W[MAXN][MAXN],M,chunk;
int qp(int a,int b,int m){
	int ret=1;
	while(b){
		if(b%2)ret=1LL*ret*a%m;
		b/=2;
		a=1LL*a*a%m;
	}
	return ret;
}
int T[H];
void get(int ind,int mn){
	if(ind<0)return;
	int ret[H],pot[H];
	fru(h,H)ret[h]=0;
	fru(h,H)pot[h]=1;
	for(int i=0;i<=ind;){
		if(i+chunk<=ind+1){
			fru(h,H){
				ret[h]+=1LL*W[i/chunk][h]*pot[h]%mod[h];
				if(ret[h]>=mod[h])ret[h]-=mod[h];
				pot[h]=1LL*pot[h]*qp(P,chunk,mod[h])%mod[h];
			}
			i+=chunk;
		}
		else{
			int u=SeqAt(i+1);
			fru(h,H){
				ret[h]+=1LL*u*pot[h]%mod[h];
				if(ret[h]>=mod[h])ret[h]-=mod[h];
				pot[h]=1LL*pot[h]*P%mod[h];
			}
			i++;
		}
	//	printf("i %d\n",i);
	//	fru(h,H)printf("%d ",ret[h]);puts("");
	//	fru(h,H)printf("%d ",pot[h]);puts("");

	}
	fru(h,H)T[h]+=mn*ret[h];
}
void solve() {
	M=SignalLength();
	int myid=MyNodeId(),nodes=NumberOfNodes();
	chunk=(M+nodes-1)/nodes;
	fru(i,H)pot[i]=1;
	fru(i,H)odw[i]=qp(P,mod[i]-2,mod[i]);
	for(int i=myid*chunk;i<min(M,(myid+1)*chunk);i++){
		int u=SignalAt(i+1);
		fru(h,H){
			ANS[h]+=1LL*u*pot[h]%mod[h];
			if(ANS[h]>=mod[h])ANS[h]-=mod[h];
			pot[h]=1LL*pot[h]*P%mod[h];
		}
	}
	if(myid){
		fru(h,H)PutInt(0,ANS[h]);
		Send(0);
		//odbierz calosc
		Receive(0);
		fru(h,H)PAT[h]=GetInt(0);
	}
	else{
		fru(i,nodes)if(i){
			Receive(i);
			fru(h,H){
				int u=GetInt(i);
				ANS[h]+=1LL*u*qp(P,i*chunk,mod[h])%mod[h];
				if(ANS[h]>=mod[h])ANS[h]-=mod[h];
			}
		}
		fru(i,nodes)if(i){
			fru(h,H)PutInt(i,ANS[h]);
			Send(i);
		}
		fru(h,H)PAT[h]=ANS[h];
	//	fru(h,H)printf("%d ",PAT[h]);puts("");
	}
	//end of processing M, PAT[] -> hashes of M
	int S=SeqLength();
	chunk=(S+nodes-1)/nodes;
	fru(h,H)pchunk[h]=qp(P,M-1,mod[h]);
	fru(i,H)pot[i]=1;
	fru(i,H)ANS[i]=0;
	for(int i=myid*chunk;i<min(S,(myid+1)*chunk);i++){
		int u=SeqAt(i+1);
		fru(h,H){
			ANS[h]+=1LL*u*pot[h]%mod[h];
			if(ANS[h]>=mod[h])ANS[h]-=mod[h];
			pot[h]=1LL*pot[h]*P%mod[h];
		}
	}
	fru(h,H)W[myid][h]=ANS[h];
	fru(i,nodes)if(i!=myid){
		fru(h,H)PutInt(i,ANS[h]);
		Send(i);
	}
	fru(i,nodes)if(i!=myid){
		Receive(i);
		fru(h,H)W[i][h]=GetInt(i);
	}
	//check my interval for matching occurrences
	int ans=0;
	fru(h,H)T[h]=0;
	for(int i=myid*chunk;i<min(S-M+1,(myid+1)*chunk);i++){
		//if T[i...i+M-1] is ok
		bool ok=1;
		if(i==myid*chunk){
			get(i+M-1,1);
			get(i-1,-1);
			fru(h,H){
				if(T[h]<0)T[h]+=mod[h];
				T[h]=1LL*T[h]*qp(odw[h],i,mod[h])%mod[h];
			}
		}
		else{
			//minus previous
			int u=SeqAt(i);
			int v=SeqAt(i+M);
			fru(h,H){
				T[h]-=u;
				if(T[h]<0)T[h]+=mod[h];
	//	{printf("us %d, do d %d node %d, i %d: ",u,v,myid,i);fru(h,H)printf("%d ",T[h]);puts("");}
				T[h]=1LL*T[h]*odw[h]%mod[h];
				T[h]+=1LL*v*pchunk[h]%mod[h];
				if(T[h]>=mod[h])T[h]-=mod[h];
			}
		}
	//	{printf("node %d, i %d: ",myid,i);fru(h,H)printf("%d ",T[h]);puts("");}
		fru(h,H)if(T[h]!=PAT[h]){ok=0;break;}
		ans+=ok;
	//	if(ok)printf("poz %d\n",i);
	}
	if(myid){
		PutInt(0,ans);
		Send(0);
	}
	else{
		fru(i,nodes)if(i){
			Receive(i);
			ans+=GetInt(i);
		}
		printf("%d\n",ans);
	}


}

int main() {
	solve();
	return 0;
}