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

#include "message.h"
#include "poszukiwania.h"

#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
#define VAR(x) #x ": " << x << " "
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define gcd __gcd
#define x first
#define y second
#define st first
#define nd second
#define pb push_back

using namespace std;

template<typename T> ostream& operator<<(ostream &out, const vector<T> &v){ out << "{"; for(const T &a : v) out << a << ", "; out << "}"; return out; }
template<typename S, typename T> ostream& operator<<(ostream &out, const pair<S,T> &p){ out << "(" << p.st << ", " << p.nd << ")"; return out; }

typedef long long int64;
typedef pair<int, int> PII;
typedef pair<int64, int64> PLL;
typedef long double K;
typedef vector<int> VI;

const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};

const int64 INF = SQ(1000LL * 1000 * 1000);

const int64 AM = 2000000011;
const int64 AW = 1000000021;

const int64 BM = 1000000007;
const int64 BW = 257;

int64 pow(int64 a, int64 b, int64 c){
	int64 r = 1;
	while(b){
		if(b&1) r = r * a % c;
		a = a * a % c;
		b /= 2;
	}
	return r;
}

int instance;
int instance_count;

int n, m, res;

int main(){
	instance = MyNodeId();
	instance_count = NumberOfNodes();

	n = SignalLength();
	m = SeqLength();

	int a = (int)((int64)n * instance / instance_count);
	int b = (int)((int64)n * (instance+1) / instance_count);

	int64 AS = 0;
	int64 ACW = pow(AW, a, AM);
	int64 BS = 0;
	int64 BCW = pow(BW, a, BM);
	FWD(i,a,b){
		int x = SignalAt(i+1);

		AS = (AS + ACW * x) % AM;
		ACW = ACW * AW % AM;

		BS = (BS + BCW * x) % BM;
		BCW = BCW * BW % BM;
	}

	if (instance > 0) {
		PutInt(0, AS);
		PutInt(0, BS);
		Send(0);
	} else {
		for (int i = 1; i < instance_count; ++i) {
			int source = Receive(-1);
			AS = (AS + GetInt(source)) % AM;
			BS = (BS + GetInt(source)) % BM;
		}
	}

	if (instance > 0) {
		Receive(0);
		AS = GetInt(0);
		BS = GetInt(0);
	} else {
		for (int i = 1; i < instance_count; ++i) {
			PutInt(i, AS);
			PutInt(i, BS);
			Send(i);
		}
	}

	int64 hashA = AS;
	int64 hashB = BS;

	a = (int)((int64)m * instance / instance_count);
	b = (int)((int64)m * (instance+1) / instance_count);

	int64 Aatn = 0;
	int64 Batn = 0;

	AS = BS = 0;
	ACW = pow(AW, a, AM);
	BCW = pow(BW, a, BM);
	FWD(i,a,b){
		int x = SeqAt(i+1);

		AS = (AS + ACW * x) % AM;
		ACW = ACW * AW % AM;

		BS = (BS + BCW * x) % BM;
		BCW = BCW * BW % BM;

		if(i == n-1){
			Aatn = AS;
			Batn = BS;
		}
	}

	int64 Alow = 0, Blow = 0;

	if (instance > 0){
		Receive(instance - 1);
		Alow = GetInt(instance - 1);
		Blow = GetInt(instance - 1);
	}
	if (instance + 1 < instance_count){
		PutInt(instance + 1, (Alow + AS)%AM);
		PutInt(instance + 1, (Blow + BS)%BM);
		Send(instance + 1);
	}

	int64 Ahigh = 0, Bhigh = 0;

	if(instance > 0){
		if(a <= n-1 && n-1 < b){
			PutInt(0, (Aatn + Alow)%AM);
			PutInt(0, (Batn + Blow)%BM);
			Send(0);
		}
	}else{
		if(a <= n-1 && n-1 < b){
			Ahigh = Aatn;
			Bhigh = Batn;
		}else{
			int source = Receive(-1);
			Ahigh = GetInt(source);
			Bhigh = GetInt(source);
		}
	}

	AS = BS = 0;
	ACW = pow(AW, a+n, AM);
	BCW = pow(BW, a+n, BM);
	FWD(i,a+n,min(b+n,m)){
		int x = SeqAt(i+1);

		AS = (AS + ACW * x) % AM;
		ACW = ACW * AW % AM;

		BS = (BS + BCW * x) % BM;
		BCW = BCW * BW % BM;
	}

	if (instance > 0){
		Receive(instance - 1);
		Ahigh = GetInt(instance - 1);
		Bhigh = GetInt(instance - 1);
	}
	if (instance + 1 < instance_count){
		PutInt(instance + 1, (Ahigh + AS)%AM);
		PutInt(instance + 1, (Bhigh + BS)%BM);
		Send(instance + 1);
	}

	if(instance == 0){
		if(Ahigh == hashA && Bhigh == hashB){
			++res;
		}
	}

	hashA = hashA * pow(AW, a, AM) % AM;
	hashB = hashB * pow(BW, a, BM) % BM;

	int64 ACWlow = pow(AW, a, AM);
	int64 BCWlow = pow(BW, a, BM);
	int64 ACWhigh = pow(AW, a+n, AM);
	int64 BCWhigh = pow(BW, a+n, BM);
	FWD(i,a+n,min(b+n,m)){
		int x = SeqAt(i-n+1);

		Alow = (Alow + ACWlow * x) % AM;
		ACWlow = ACWlow * AW % AM;

		Blow = (Blow + BCWlow * x) % BM;
		BCWlow = BCWlow * BW % BM;

		int y = SeqAt(i+1);

		Ahigh = (Ahigh + ACWhigh * y) % AM;
		ACWhigh = ACWhigh * AW % AM;

		Bhigh = (Bhigh + BCWhigh * y) % BM;
		BCWhigh = BCWhigh * BW % BM;

		hashA = hashA * AW % AM;
		hashB = hashB * BW % BM;

		if(Ahigh == (Alow + hashA) % AM &&
			 Bhigh == (Blow + hashB) % BM){
			++res;
		}
	}

	if (instance > 0) {
		PutInt(0, res);
		Send(0);
	} else {
		for (int i = 1; i < instance_count; ++i) {
			int source = Receive(-1);
			res += GetInt(source);
		}
		printf("%d\n", res);
	}
	return 0;
}