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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//#include "poszukiwania-test.h"
//#include "poszukiwania2.h"
#include "poszukiwania.h"
#include "message.h"

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#include <ext/numeric>
using namespace std ;
using namespace __gnu_cxx ;
typedef long long LL ;
typedef pair<int,int> PII ;
typedef vector<int> VI ;
const int INF = 1e9+100 ;
const LL INFLL = (LL)INF * (LL)INF ;
#define REP(i,n) for(i=0;i<(n);++i)
#define ALL(c) c.begin(),c.end()
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)
#define CLEAR(t) memset(t,0,sizeof(t))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second

template<class T1,class T2> ostream& operator<<(ostream &s, const pair<T1,T2> &x) { s<< "(" << x.FI << "," << x.SE << ")"; return s;}
template<class T>           ostream& operator<<(ostream &s, const vector<T>   &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T>           ostream& operator<<(ostream &s, const set<T>      &t) { FOREACH(it, t) s << *it << " " ; return s ; }
template<class T1,class T2> ostream& operator<<(ostream &s, const map<T1, T2> &t) { FOREACH(it, t) s << *it << " " ; return s ; }

///////////////////////////////////////////

template<class T>
struct UpperClass {
	//..
} ;

template<>
struct UpperClass<LL> {
	typedef __int128 cls ;
} ;

template<>
struct UpperClass<unsigned int> {
	typedef unsigned long long cls ;
} ;

template<>
struct UpperClass<int> {
	typedef LL cls ;
} ;

template<class T, class T2>
T expMod(typename UpperClass<T>::cls a, T2 b, T m) {
	typename UpperClass<T>::cls s=1 ;
	while(b>0) {
		if((b&1)!=0) s = (s*a)%m ;
		a = (a*a)%m ;
		b >>= 1 ;
	}
	return s ;
}
/////////////////////////////////////////

const int P = 1000000007 ;
//const LL Q1 = 100000000000000003LL ;
const unsigned int Q1 = 4000000009LL ;
const LL Q2 = 1LL<<50 ;

struct myHash {
	LL H ;	// jeden hash modulo Q1
	LL S ;	// drugi hash modulo Q2
	myHash(LL _h=0, LL _s=0) : H(_h), S(_s) {}
} ;
bool operator==(const myHash &a, const myHash &b) {
	return (a.H==b.H) && (a.S==b.S) ;
}

inline void stepHash(myHash &hash, int next) {
	hash.H = ((unsigned long long)hash.H*P+(next+1))%Q1 ;
	hash.S = (          (__int128)hash.S*P+(next+1))&(Q2-1) ;
}
inline void joinTwoHash(myHash &hash1, const myHash &hash2, int len) {
	hash1.H = ((unsigned long long)hash1.H * expMod(P,len,Q1) + hash2.H)%Q1 ;
	hash1.S = (          (__int128)hash1.S * expMod(P,len,Q2) + hash2.S)&(Q2-1) ;
}
inline myHash subtractHashes(const myHash &h2, const myHash &h1, const myHash &shift) {
	LL tmp1 = ((unsigned long long)h1.H*shift.H)%Q1 ;
	LL tmp2 = (          (__int128)h1.S*shift.S)&(Q2-1) ;
	return myHash((h2.H+(Q1-tmp1))%Q1, (h2.S+(Q2-tmp2))&(Q2-1)) ;
}

// dzieli przedzial [1..M] na [start,end)
inline LL _start(LL M, int node) {
	return M*node/NumberOfNodes()+1 ;
}
inline LL _end(LL M, int nr) {
	return _start(M, nr+1) ;
}

void sendHashToZeroNode(myHash &hash, int len) {
	PutLL(0, hash.H) ;
	PutLL(0, hash.S) ;
	PutInt(0, len) ;
	Send(0) ;
		
	Receive(0) ;
	hash.H = GetLL(0) ;
	hash.S = GetLL(0) ;
}

void sumResults(int ret) {
	if(MyNodeId()>0) {
		PutInt(0, ret) ;
		Send(0) ;
	}
	else {
		for(int i=1 ; i<NumberOfNodes() ; i++) {
			int v = Receive(-1) ;
			ret += GetInt(v) ;
		}
		cout << ret << endl ;
	}
}

inline myHash getHash(LL start, LL end, LL(*getVal)(LL)) {
	myHash h ;
	for(LL i=start ; i<end ; i++)
		stepHash(h, getVal(i)) ;
	return h ;
}

void joinHashes(myHash &hash, int len) {
	if(MyNodeId()>0) sendHashToZeroNode(hash, len) ;
	else {
		for(int node=1 ; node<NumberOfNodes() ; node++) {
			Receive(node) ;
			LL h = GetLL(node) ;
			LL s = GetLL(node) ;
			int l = GetInt(node) ;
			joinTwoHash(hash, myHash(h,s), l) ;
		}
		for(int node=1 ; node<NumberOfNodes() ; node++) {
			PutLL(node, hash.H) ;
			PutLL(node, hash.S) ;
			Send(node) ;
		}
	}
}
void joinProgresive(myHash &hash, int len) {
	if(MyNodeId()>0) sendHashToZeroNode(hash, len) ;
	else {
		myHash tmp=hash ;
		for(int node=1 ; node<NumberOfNodes() ; node++) {
			Receive(node) ;
			LL h = GetLL(node) ;
			LL s = GetLL(node) ;
			int l = GetInt(node) ;
			joinTwoHash(tmp, myHash(h,s), l) ;
			
			PutLL(node, tmp.H) ;
			PutLL(node, tmp.S) ;
			Send(node) ;
		}
	}
}

void solveByHashing(LL N, LL M) {
	// liczenie hasha wzorca
	myHash patternHash = getHash(_start(M,MyNodeId()), _end(M,MyNodeId()), SignalAt) ;
	joinHashes(patternHash, _end(M,MyNodeId())-_start(M,MyNodeId())) ;
	//cout << patternHash.H << endl ;
	
	myHash h1 = (MyNodeId()==0 ? myHash() : getHash(_start(N-M+1, MyNodeId()-1), _end(N-M+1, MyNodeId()-1), SeqAt)) ;
	joinProgresive(h1, MyNodeId()==0 ? 0 : _end(N-M+1, MyNodeId()-1)-_start(N-M+1, MyNodeId()-1)) ;
	
	myHash h2 = getHash(_start(M, MyNodeId()), _end(M, MyNodeId()), SeqAt) ;
	joinHashes(h2, _end(M,MyNodeId())-_start(M,MyNodeId())) ;
	if(MyNodeId()!=0)
		h2 = getHash(_start(N-M+1, MyNodeId()-1)+M, _end(N-M+1, MyNodeId()-1)+M, SeqAt) ;
	joinProgresive(h2, MyNodeId()==0 ? 0 : _end(N-M+1, MyNodeId()-1)-_start(N-M+1, MyNodeId()-1)) ;
	
	myHash shift(expMod(P, M, Q1), expMod(P, M, Q2)) ;
	int ret=0 ;
	const LL start = _start(N-M+1, MyNodeId()), end = _end(N-M+1, MyNodeId()) ;
	for(LL i=start ; i<end ; i++) {
		//cout << i << ") " << h2.H << " " << h1.H << " " << shift.H << endl ;
		if(subtractHashes(h2,h1,shift) == patternHash) {
			ret++ ;
		}
		
		stepHash(h1, SeqAt(i)) ;
		if(i+M<=N)
			stepHash(h2, SeqAt(i+M)) ;
	}
	
	sumResults(ret) ;
}

//////////////////////////////////////////////////////////////

void solveByKMP(LL N, LL M) {
	int *pref = new int[M+1]() ;
	pref[1] = 0 ;
	int k=0 ;
	for(int q=2 ; q<=M ; q++) {
		int next = SignalAt(q) ;
		while(k>0 && next != SignalAt(k+1)) k = pref[k] ;
		if(next == SignalAt(k+1)) k++ ;
		pref[q] = k ;
	}
	
	int ret=0 ;
	k=0 ;
	int start =     _start(N, MyNodeId()) ;
	int end   = min(  _end(N, MyNodeId())+(M-1), N+1) ;
	for(int q=start ; q<end ; q++) {
		int next = SeqAt(q) ;
		while(k>0 && next != SignalAt(k+1)) k = pref[k] ;
		if(next == SignalAt(k+1)) k++ ;
		if(k==M) {
			ret ++ ;
			k = pref[k] ;
		}
	}
	
	sumResults(ret) ;
}

/////////////////////////////////////////////////////////////

int main()
{
	ios_base::sync_with_stdio(0) ;
	LL M=SignalLength() ;
	LL N=SeqLength() ;
	//if(MyNodeId()==0) cout << N << " " << M << endl ;
	if(M>N) {
		if(MyNodeId()==0) cout << 0 << endl ;
		return 0 ;
	}
	
	if(M<=25000000 // zmiesci sie w pamieci
		&& (M<=10000 || M<=N/(1+NumberOfNodes())) )
			solveByKMP(N, M) ;
	else
		solveByHashing(N, M) ;
	
}