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
#include "message.h"
#include "poszukiwania.h"
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull M=1e9+7, P[4]={23, 29, 31, 37};
const int N=1e6+5;
struct Hash
{
	ull h[4];
};
Hash next_hash(Hash h, int a)
{
	for(int i=0; i<4; ++i) h.h[i]=(h.h[i]*P[i]+a)%M;
	return h;
}
ull pm[4];
Hash mult(Hash h)
{
	for(int i=0; i<4; ++i) h.h[i]=(h.h[i]*pm[i])%M;
	return h;
}
Hash operator +(Hash h1, Hash h2)
{
	for(int i=0; i<4; ++i) h1.h[i]=(h1.h[i]+h2.h[i])%M;
	return h1;
}
bool operator ==(Hash h1, Hash h2)
{
	for(int i=0; i<4; ++i) if(h1.h[i]!=h2.h[i]) return 0;
	return 1;
}
ull pow(ull a, ull n)
{
	if(n==0) return 1;
	if(n%2==0) return pow(a*a%M, n/2);
	return pow(a*a%M, n/2)*a%M;
}
void PutHash(int target, Hash h)
{
	for(int i=0; i<4; ++i) PutLL(target, h.h[i]);
}
Hash GetHash(int source)
{
	Hash h;
	for(int j=0; j<4; ++j) h.h[j]=GetLL(source);
	return h;
}
void printHash(Hash h)
{
	for(int i=0; i<4; ++i) printf("%d: %lld\n", i, h.h[i]);
}
Hash h1, h2, hs, t[1000], tmp;
vector<Hash> v[1000];
int main()
{
	int nodes=NumberOfNodes(), my_id=MyNodeId(), res=0;
	ull ln=SeqLength(), lm=SignalLength();
	for(int i=0; i<4; ++i) pm[i]=pow(P[i], lm-((my_id+1)*lm)/nodes);
	for(int i=(my_id*lm)/nodes+1; i<=((my_id+1)*lm)/nodes; ++i) hs=next_hash(hs, SignalAt(i));
	PutHash(0, mult(hs));
	Send(0);
	if(my_id==0)
	{
		for(int j=0; j<4; ++j) hs.h[j]=0;
		for(int i=0; i<nodes; ++i)
		{
			Receive(i);
			hs=hs+GetHash(i);
		}
		//for(int j=0; j<4; ++j) printf("%d: %lld\n", j, hs.h[j]);
		for(int i=0; i<nodes; ++i)
		{
			PutHash(i, hs);
			Send(i);
		}
	}
	Receive(0);
	hs=GetHash(0);
	if(lm<=2e7)
	{
		for(int i=0; i<4; ++i) pm[i]=pow(P[i], lm);
		for(int i=(my_id*ln)/nodes+1; i<=(my_id*ln)/nodes+lm&&i<=ln; ++i)
		{
			//printf("Instance %d at i=%d\n", my_id, i);
			h1=next_hash(h1, SeqAt(i));
		}
		if(my_id==0) if(mult(h2)+hs==h1) ++res;
		for(int i=(my_id*ln)/nodes+1; i<=((my_id+1)*ln)/nodes&&i+lm<=ln; ++i)
		{
			//printf("Instance %d at i=%d\n", my_id, i);
			h1=next_hash(h1, SeqAt(i+lm));
			h2=next_hash(h2, SeqAt(i));
			if(mult(h2)+hs==h1) ++res;
		}
		//printf("Instance %d, res %d\n", my_id, res);
		PutInt(0, res);
		Send(0);
		if(my_id!=0) return 0;
		int gres=0;
		for(int i=0; i<nodes; ++i)
		{
			Receive(i);
			gres+=GetInt(i);
		}
		printf("%d\n", gres);
		return 0;
	}
	for(int i=(lm*my_id)/nodes+1; i<=(lm*(my_id+1))/nodes; ++i) for(int j=0; j*lm+i<=ln; ++j) t[j]=next_hash(t[j], SeqAt(i+j*lm));
	for(int j=0; j*lm+(lm*my_id)/nodes<ln; ++j) PutHash(0, t[j]);
	Send(0);
	if(my_id==0)
	{
		for(int i=0; i<nodes; ++i)
		{
			Receive(i);
			for(int j=0; j*lm+(lm*i)/nodes<ln; ++j) v[j].push_back(GetHash(i));
		}
		//for(int i=0; i<nodes; ++i) printf("%d\n", v[i].size());
		for(int j=0; j*lm<ln; ++j)
		{
			//if(my_id==0) printf("Here!\n");
			for(int i=0; i<nodes&&j*lm+(lm*i)/nodes<ln; ++i)
			{
				//if(my_id==0) printf("Here! i=%d\n", i);
				for(int k=0; k<4; ++k) pm[k]=pow(P[k], (lm*(i+1))/nodes-(lm*i)/nodes);
				tmp=mult(tmp)+v[j][i];
				//printf("\nj=%d i=%d\n", j, i);
				//printHash(tmp);
				if(j*lm+(lm*(i+1))/nodes<ln) PutHash((i+1)%nodes, tmp);
			}
		}
		for(int i=0; i<nodes; ++i) Send(i);
	}
	//if(my_id==0) printf("Here!\n");
	Receive(0);
	if(my_id!=0) h1=GetHash(0);
	for(int i=0; i<4; ++i) pm[i]=pow(P[i], lm);
	for(int j=0; (j+1)*lm+(lm*my_id)/nodes<ln; ++j)
	{
		tmp=h2=GetHash(0);
		if(j==0&&my_id==0) if(mult(h1)+hs==h2) ++res;
		for(int i=(lm*my_id)/nodes+1; i<=(lm*(my_id+1))/nodes&&(j+1)*lm+i<=ln; ++i)
		{
			h1=next_hash(h1, SeqAt(i+j*lm));
			h2=next_hash(h2, SeqAt(i+(j+1)*lm));
			if(mult(h1)+hs==h2) ++res;
		}
		h1=tmp;
	}
	//printf("Instance %d, res=%d\n", my_id, res);
	PutInt(0, res);
	Send(0);
	if(my_id!=0) return 0;
	int gres=0;
	for(int i=0; i<nodes; ++i)
	{
		Receive(i);
		gres+=GetInt(i);
	}
	printf("%d\n", gres);
	return 0;
}
/*
0: 305809
1: 758641
2: 986049
3: 1979649

0: 935493891
1: 976642109
2: 146285384
3: 983440951

0: 269855243
1: 257507579
2: 149157677
3: 767892365

*/