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

#include<bits/stdc++.h>
#define FOR(i,s,e) for(int i=(s);i<=(e);i++)
#define FORL(i,s,e) for(LL i=(s);i<=(e);i++)
#define FORD(i,s,e) for(int i=(s);i>=(e);i--)
#define FORLD(i,s,e) for(LL i=(s);i>=(e);i--)
#define ALL(k) (k).begin(),(k).end()
#define e1 first 
#define e2 second
#define MP make_pair
#define PB push_back
#define EB emplace_back 
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<int,PII> PIP;
typedef pair<PLL,PLL> PPP;
const LL TYS=1000;
const LL MLD=TYS*TYS*TYS;
const LL MOD[]={MLD+33,MLD+696969};
const LL BASE[]={MLD+7,MLD+9};

int NuNo,CuNo;
LL n,interval_size,lw,pw;
LL m;

LL potega(LL a,LL k,LL MD){
	LL wyn=1;
	while(k>0){
		if(k%2) wyn=(wyn*a)%MD;
		a=(a*a)%MD;
		k/=2;
	}
	return wyn;
}
LL h[2],potb[2];
LL hpre[2];
LL funkcja(LL pos,int par){
	if(par==1) return SignalAt(pos+1);
	return SeqAt(pos+1);
}


void hasz0(LL hasz0,LL hasz1,int par){
	//FOR(j,0,1) h[j].clear(),potb[j].clear();
	if(CuNo==0){h[0]=hasz0,h[1]=hasz1;}
	else {h[0]=0;h[1]=0;}
	potb[0]=1,potb[1]=1;
	FORL(i,lw,pw){
		FOR(j,0,1) {
			LL val=funkcja(i,par);
			h[j]=((h[j]*BASE[j]+(val+1))%MOD[j]);
			potb[j]=((potb[j]*BASE[j])%MOD[j]);
		}
	}
	//printf("Inst %d %lld %lld %lld\n",CuNo,lw,h[0],potb[0]);
	if(CuNo==0) {hpre[0]=hasz0,hpre[1]=hasz1;}
	else hpre[0]=hpre[1]=0;
	if(CuNo>0) Receive(CuNo-1);
	FOR(j,0,1){
		LL h1=h[j];
		if(CuNo>0){
			LL hh=GetLL(CuNo-1);
			hpre[j]=hh;
			h1+=(hh*potb[j])%MOD[j];
			h1%=MOD[j];
		}
		h[j]=h1;
		if(CuNo<NuNo-1) PutLL(CuNo+1,h1);
	}
	//printf("prefix %d %lld %lld\n",CuNo,hpre[0],hpre[1]);
	if(CuNo<NuNo-1)Send(CuNo+1);
	
}

const int MAXNODES=222;
LL lw1[MAXNODES];
LL pref[2][MAXNODES];




main(){
	NuNo=NumberOfNodes();
	CuNo=MyNodeId();
	n=SignalLength();
	interval_size=n/NuNo;	
	lw=CuNo*interval_size,pw=(CuNo+1)*interval_size-1;
	if(CuNo==NuNo-1) pw=n-1;
	/*vector<LL> V0;
	FORL(i,lw,pw)
		V0.PB(SignalAt(i+1));*/
	hasz0(0,0,1);
	PLL haszwzorca={h[0],h[1]};
	if(CuNo<NuNo-1){
		Receive(CuNo+1);
		haszwzorca.e1=GetLL(CuNo+1);
		haszwzorca.e2=GetLL(CuNo+1);
	}
	if(CuNo>0) {
		PutLL(CuNo-1,haszwzorca.e1);
		PutLL(CuNo-1,haszwzorca.e2);
		Send(CuNo-1);
	}
	//printf("wzorzec %d %lld %lld\n",CuNo,haszwzorca.e1,haszwzorca.e2);
	m=SeqLength();
	if(m<n){if(CuNo==0)printf("0\n");return 0;}
	//hasz wzorca
	//V0.clear();
	//FOR(i,lw,pw)V0.PB(SeqAt(i+1));
	hasz0(0,0,0);
	//printf("1:Instancja %d %lld %lld\n",CuNo,lw,pw);
	PLL haszpr={h[0],h[1]};
	//if(CuNo==NuNo-1) printf("%lld %lld\n\n",haszpr.e1,haszpr.e2);
	if(CuNo<NuNo-1){
		Receive(CuNo+1);
		haszpr.e1=GetLL(CuNo+1);
		haszpr.e2=GetLL(CuNo+1);
	}
	if(CuNo>0) {
		PutLL(CuNo-1,haszpr.e1);
		PutLL(CuNo-1,haszpr.e2);
		Send(CuNo-1);
	}
	int len=m;
	interval_size=len/NuNo;
	lw=CuNo*interval_size,pw=(CuNo+1)*interval_size-1;
	if(CuNo==NuNo-1) pw=len-1;
	//printf("2:Instancja %d %lld %lld\n",CuNo,lw,pw);
	hasz0(0,0,0);
	//printf("haszpr %d %lld %lld\n",CuNo,hpre[0],hpre[1]);
	
	//printf("3:Instancja %d %lld %lld\n",CuNo,lw,pw);
	FOR(i,0,NuNo-1) lw1[i]=i*interval_size;
	lw1[NuNo]=n;
	FOR(i,0,NuNo-1){
		PutLL(i,hpre[0]);
		PutLL(i,hpre[1]);
		Send(i);
	}
	FOR(i,0,NuNo-1){
		Receive(i);
		pref[0][i]=GetLL(i);
		pref[1][i]=GetLL(i);
	}
	if(CuNo==NuNo-1) pw=m;
	lw1[NuNo]=m+1;
	pref[0][NuNo]=hpre[0],pref[1][NuNo]=hpre[1];
	//if(CuNo==NuNo-1){FOR(j,0,CuNo) printf("%d %lld %lld %lld\n",j,lw1[j],pref[0][j],pref[1][j]);}
	LL ha1[]={hpre[0],hpre[1]};
	LL ha2[]={0,0};
	LL l1=lw;
	LL l2=0;
	FOR(i,0,NuNo-1){
		if(lw1[i+1]>=l1+n){
			FOR(j,0,1) ha2[j]=pref[j][i];
			l2=lw1[i];
			break;
		}
	}
	//printf("In %d %lld %lld;; %lld %lld\n",CuNo,l1,ha1[0],l2,ha2[0]);
	while(l2-l1<n&&l2<m){
		LL val=SeqAt(l2+1);
		FOR(j,0,1) ha2[j]=(ha2[j]*BASE[j]+val+1)%MOD[j];
		l2++;
	}
	//printf("In %d %lld %lld;; %lld %lld\n",CuNo,l1,ha1[0],l2,ha2[0]);
	LL wynik=0;
	LL pot[]={potega(BASE[0],n,MOD[0]),potega(BASE[1],n,MOD[1])};
	if(l2-l1==n){
		while(l1<=pw&&l2<=m){
			int ok=1;
			FOR(j,0,1){
				LL hp=ha2[j];
				LL hl=(ha1[j]*pot[j])%MOD[j];
				hp+=(MOD[j]-hl);
				hp%=MOD[j];
				if(j==0){
					//printf("chk %lld %lld %lld;; %lld %lld %lld %lld\n",
					//l1,ha1[0],hl,l2,ha2[0],hp,haszwzorca.e1);
				}
				if(j==0&&hp!=haszwzorca.e1) ok=0;
				if(j==1&&hp!=haszwzorca.e2) ok=0;
				
			}
			wynik+=ok;
			//if(ok) printf("WYST %lld %lld\n",l1,l2);
			if(l2==m||l1==pw) break;
			LL val=SeqAt(l1+1);
			FOR(j,0,1) ha1[j]=(ha1[j]*BASE[j]+val+1)%MOD[j];
			val=SeqAt(l2+1);
			FOR(j,0,1) ha2[j]=(ha2[j]*BASE[j]+val+1)%MOD[j];
			l1++,l2++;
		}
	}
	if(CuNo==0){
		FOR(j,1,NuNo-1){
			Receive(j);
			wynik+=GetLL(j);
		}
		printf("%lld\n",wynik);
	}
	else {
		PutLL(0,wynik);
		Send(0);
	}
	
}