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
#include<iostream>
#include<vector>
#include<algorithm>
#include "poszukiwania.h"
#include "message.h"
#define LL long long
using namespace std;
const int p1=1000000007,m1=1000000033,p2=1000000009,m2=1000000087;
int h_ins[100][2],h_wzo[2],h[2];
int pocz[100],kon[100];
int poteg(int x,int y,int m){
	LL res=1;
	if(y==0)return res;
	while(y>0){
		if(y%2){
			res=(res*x)%m;	
		}
		x=((LL)x*x)%m;
		y=y/2;
	}
	return res;
}
/*int sequence[100],signal[100],se_len,si_len,INS_ID,INS_NUM;
void get_test(){
	cin>>se_len;
	for(int i=0;i<se_len;i++)cin>>sequence[i];
	cin>>si_len;
	for(int i=0;i<si_len;i++)cin>>signal[i];
	cin>>INS_ID>>INS_NUM;
}
int NumberOfNodes(){
	return INS_NUM;	
}
int MyNodeId(){
	return INS_ID;	
}
int SignalLength(){
	return si_len;	
}
int SeqLength(){
	return se_len;	
}
int SeqAt(int x){
	return sequence[x-1];	
}
int SignalAt(int x){
	return signal[x-1];	
}
int Send(int x){	
}
int Receive(int x){
}
int PutInt(int x,int a){
	cout<<"wysylam "<<a<<" do "<<x<<"\n";	
}
int GetInt(int x){
	cout<<"odbieram od "<<x<<"\n";
	cin>>x;
	return x;	
}*/
main(){
	//get_test();
	int t=SeqLength(),ins=NumberOfNodes(),d,nr=MyNodeId(),w=SignalLength(),ins2;
	if(ins>t){
		ins=t;
		if(nr>=ins)return 0;	
	}
	ins2=ins;
	if(ins2>w){
		ins2=w;	
	}
	d=w/ins2;
	for(int i=0;i<ins2;i++){
		pocz[i]=d*i+1;
		kon[i]=d*(i+1);
	}
	kon[ins2-1]=w;
	//cout<<"p/k\n";
	//for(int i=0;i<ins2;i++)cout<<pocz[i]<<" "<<kon[i]<<"\n";
	if(nr<ins2){
		h[0]=h[1]=0;
		for(int i=pocz[nr];i<=kon[nr];i++){
			h[0]=((LL)h[0]*p1)%m1;
			h[1]=((LL)h[1]*p2)%m2;
			h[0]=(h[0]+SignalAt(i)+3)%m1;
			h[1]=(h[1]+SignalAt(i)+3)%m2;	
		}
		h[0]=((LL)h[0]*poteg(p1,pocz[ins2-1]-pocz[nr],m1))%m1;
		h[1]=((LL)h[1]*poteg(p2,pocz[ins2-1]-pocz[nr],m2))%m2;
		PutInt(0,h[0]);
		PutInt(0,h[1]);
		Send(0);
	}
	if(nr==0){
		h[0]=h[1]=0;
		for(int i=0;i<ins2;i++){
			Receive(i);
			h[0]=(h[0]+GetInt(i))%m1;
			h[1]=(h[1]+GetInt(i))%m2;
		}
		for(int i=0;i<ins;i++){
			PutInt(i,h[0]);
			PutInt(i,h[1]);	
			Send(i);
		}
	}
	Receive(0);
	h_wzo[0]=GetInt(0);
	h_wzo[1]=GetInt(0);
	d=t/ins;
	for(int i=0;i<ins;i++){
		pocz[i]=d*i+1;
		kon[i]=d*(i+1);
	}
	kon[ins-1]=t;
	//cout<<"p/k\n";
	//for(int i=0;i<ins2;i++)cout<<pocz[i]<<" "<<kon[i]<<"\n";
	//hasze calego przedzialu
	h_ins[nr][0]=h_ins[nr][1]=0;
	for(int i=pocz[nr];i<=kon[nr];i++){
		h_ins[nr][0]=((LL)h_ins[nr][0]*p1)%m1;
		h_ins[nr][1]=((LL)h_ins[nr][1]*p2)%m2;
		h_ins[nr][0]=(h_ins[nr][0]+SeqAt(i)+3)%m1;
		h_ins[nr][1]=(h_ins[nr][1]+SeqAt(i)+3)%m2;
	}
	//wysylam innym
	for(int i=0;i<ins;i++){
		PutInt(i,h_ins[nr][0]);
		PutInt(i,h_ins[nr][1]);
		Send(i);	
	}
	//odbieram
	for(int i=0;i<ins;i++){
		Receive(i);
		h_ins[i][0]=GetInt(i);
		h_ins[i][1]=GetInt(i);	
	}
	//sprawdzam komu wyslac poczatek
	int kto=-1,od=-1;
	for(int i=0;i<ins;i++){
		if(pocz[i]<=w)continue;
		int zacz=pocz[i]-w+1;
		if(zacz>=pocz[nr]&&zacz<=kon[nr]){
			kto=i;
			od=zacz;
		}
	}
	//wysylam
	if(kto!=-1){
		h[0]=h[1]=0;
		for(int i=od;i<=kon[nr];i++){
			h[0]=((LL)h[0]*p1)%m1;
			h[1]=((LL)h[1]*p2)%m2;
			h[0]=(h[0]+SeqAt(i)+3)%m1;
			h[1]=(h[1]+SeqAt(i)+3)%m2;	
		}
		PutInt(kto,h[0]);
		PutInt(kto,h[1]);
		Send(kto);
	}
	//twoze pierwszy hasz
	int wyk=0,it=nr-1,a,b;
	if(w<=kon[nr]){
		h[0]=h[1]=0;
		if(w>pocz[nr]){
			a=1;b=w;
			for(int i=pocz[nr];i<=b;i++){
				wyk++;
				h[0]=((LL)h[0]*p1)%m1;
				h[1]=((LL)h[1]*p2)%m2;
				h[0]=(h[0]+SeqAt(i)+3)%m1;
				h[1]=(h[1]+SeqAt(i)+3)%m2;	
			}
		}
		else{
			a=pocz[nr]-w+1;b=pocz[nr];
			h[0]=SeqAt(b)+3;
			h[1]=SeqAt(b)+3;
			wyk=1;	
		}
		for(;it>=0;it--){
			if(pocz[it]>=a){
				h[0]=((LL)h[0]+(LL)poteg(p1,wyk,m1)*h_ins[it][0])%m1;
				h[1]=((LL)h[1]+(LL)poteg(p2,wyk,m2)*h_ins[it][1])%m2;	
				wyk+=d;
			}
			else{
                if(pocz[it]<a&&kon[it]>=a){
				    Receive(it);
				    h[0]=((LL)h[0]+(LL)poteg(p1,wyk,m1)*GetInt(it))%m1;
				    h[1]=((LL)h[1]+(LL)poteg(p2,wyk,m2)*GetInt(it))%m2;
				    wyk+=kon[it]-a+1;
				    break;	
                }
			}
		}
	}
	//przechodze po haszach
	int ile=0,pot_w[2];
	pot_w[0]=poteg(p1,w-1,m1);
	pot_w[1]=poteg(p2,w-1,m2);
	//cout<<"#\n";
	for(int i=b;i<=kon[nr];i++){
		if(h[0]==h_wzo[0]&&h[1]==h_wzo[1])ile++;
		//cout<<h[0]<<" "<<h[1]<<"\n";
		if(i==kon[nr])break;
		h[0]=((LL)h[0]+m1-((LL)pot_w[0]*(SeqAt(i-w+1)+3))%m1)%m1;
		h[1]=((LL)h[1]+m2-((LL)pot_w[1]*(SeqAt(i-w+1)+3))%m2)%m2;
		h[0]=((LL)h[0]*p1)%m1;
		h[1]=((LL)h[1]*p2)%m2;
		h[0]=(h[0]+SeqAt(i+1)+3)%m1;
		h[1]=(h[1]+SeqAt(i+1)+3)%m2;	
	}
	PutInt(0,ile);
	Send(0);
	if(nr>0)return 0;
	int wynik=0;
	for(int i=0;i<ins;i++){
		Receive(i);
		wynik+=GetInt(i);
	}
	cout<<wynik<<"\n";
	return 0;
}