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
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include "message.h"
#include "poszukiwania.h"

using namespace std;

/*
long long SignalLength() { return 100; }
long long SeqLength() { return 201; }

long long SignalAt(long long i) { return i % 3; }
long long SeqAt(long long i) { return i % 3; }
//*/

#define FOR_P for (int j = 0; j < 2; ++j)

int n, m;
int myIndex, allSize;
const int P[2] = { 2000000203, 2000000227 };
const int mult = 1000000102;
int sigHash[2];
int fstHash[2];

int powMod(int a, int e, int p)
{
	int res = 1;
	while (e > 0)
	{
		if (e & 1)
		{
			res = ((long long)res * a) % p;
		}
		a = ((long long)a * a) % p;
		e >>= 1;
	}
	return res;
}

void computeSignalHash()
{
	const int sliceSize = (n + allSize - 1) / allSize;
	const int start = sliceSize * myIndex;
	const int end = min(sliceSize * (myIndex + 1), n);
	
	sigHash[0] = sigHash[1] = 0;
	fstHash[0] = fstHash[1] = 0;
	for (int i = start; i < end; ++i)
	{
		FOR_P
		{
			sigHash[j] = ((long long)sigHash[j] * mult + SignalAt(i + 1) + 1) % P[j];
			
			fstHash[j] = ((long long)fstHash[j] * mult + SeqAt(i + 1) + 1) % P[j];
		}
	}
	FOR_P
	{
		sigHash[j] = ((long long)sigHash[j] * powMod(mult, n - end, P[j])) % P[j];
		
		fstHash[j] = ((long long)fstHash[j] * powMod(mult, n - end, P[j])) % P[j];
	}
	
	if (myIndex > 0)
	{
		FOR_P
		{
			PutInt(0, sigHash[j]);
			PutInt(0, fstHash[j]);
		}
		Send(0);
	}
	else
	{
		for (int i = 1; i < allSize; ++i)
		{
			Receive(i);
			FOR_P
			{
				sigHash[j] = ((long long)sigHash[j] + GetInt(i)) % P[j];
				fstHash[j] = ((long long)fstHash[j] + GetInt(i)) % P[j];
			}
		}
	}
	
	if (myIndex == 0)
	{
		for (int i = 1; i < allSize; ++i)
		{
			FOR_P
			{
				PutInt(i, sigHash[j]);
				PutInt(i, fstHash[j]);
			}
			Send(i);
		}
	}
	else
	{
		Receive(0);
		FOR_P
		{
			sigHash[j] = GetInt(0);
			fstHash[j] = GetInt(0);
		}
	}
}

void computeSeqHash()
{
	const int sliceSize = (m - n + allSize - 1) / allSize;
	const int start = n + sliceSize * myIndex;
	const int end = min(n + sliceSize * (myIndex + 1), m);
	
	int delta[2] = { 0, 0 };
	int powP[2];
	FOR_P
	{
		powP[j] = powMod(mult, n - 1, P[j]);
	}
	
	for (int i = start; i < end; ++i)
	{
		FOR_P
		{
			// next = (cur - powP * SeqAt()) * mult + SeqAt()
			int sub = ((long long)powP[j] * (SeqAt(i - n + 1) + 1)) % P[j];
			delta[j] = ((long long)(delta[j] - sub) * mult + SeqAt(i + 1) + 1) % P[j];
			if (delta[j] < 0)
			{
				delta[j] += P[j];
			}
		}
	}
	
	int curHash[2];
	int hashLim[2];
	if (myIndex == 0)
	{
		FOR_P
		{
			curHash[j] = fstHash[j];
			hashLim[j] = ((long long)curHash[j] * powMod(mult, end - start, P[j]) + delta[j]) % P[j];
		}
		if (allSize > 1)
		{
			FOR_P
			{
				PutInt(1, hashLim[j]);
			}
			Send(1);
		}
	}
	else
	{
		Receive(myIndex - 1);
		FOR_P
		{
			curHash[j] = GetInt(myIndex - 1);
			hashLim[j] = ((long long)curHash[j] * powMod(mult, end - start, P[j]) + delta[j]) % P[j];
		}
		
		if (myIndex + 1 < allSize)
		{
			FOR_P
			{
				PutInt(myIndex + 1, hashLim[j]);
			}
			Send(myIndex + 1);
		}
	}
	
	int res = 0;
	for (int i = start; i < end; ++i)
	{
		FOR_P
		{
			int sub = ((long long)powP[j] * (SeqAt(i - n + 1) + 1)) % P[j];
			curHash[j] = ((long long)(curHash[j] - sub) * mult + SeqAt(i + 1) + 1) % P[j];
			if (curHash[j] < 0)
			{
				curHash[j] += P[j];
			}
		}
		
		if (curHash[0] == sigHash[0] && curHash[1] == sigHash[1])
		{
			++res;
		}
	}
	
	if (myIndex > 0)
	{
		PutInt(0, res);
		Send(0);
	}
	else
	{
		if (sigHash[0] == fstHash[0] && sigHash[1] == fstHash[1])
		{
			++res;
		}
		
		for (int i = 1; i < allSize; ++i)
		{
			Receive(i);
			res += GetInt(i);
		}
		printf("%d\n", res);
	}
}

int main()
{
	n = (int)SignalLength();
	m = (int)SeqLength();
	myIndex = MyNodeId();
	allSize = NumberOfNodes();
	
	computeSignalHash();
	computeSeqHash();
	
	return 0;
}