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
#include "message.h"
#include "poszukiwania.h"

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>

const int HASH_COUNT = 8;
const unsigned long long int PRIMES[HASH_COUNT] = {101,193,15485863,49979687,104395301,217645177,353868013,633910099};
typedef unsigned long long int Hash[HASH_COUNT];

unsigned long long int power(unsigned long long int val, unsigned long long int pow) {
	unsigned long long int result = 1;
	while (pow>0) {
		if (pow&1) result = result*val;
        val = val*val;
        pow >>= 1;
    }
    return result;
}

void hashAdd(unsigned long long int symbol, Hash hash) {
	for (int i=0; i<HASH_COUNT; ++i) {
		hash[i] = hash[i]*PRIMES[i] + symbol;
	}
}

void hashShift(int count, Hash hash) {
	for (int i=0; i<HASH_COUNT; ++i) {
		unsigned long long int p = power(PRIMES[i],count);
		hash[i] = hash[i]*p;
	}
}

void hashUpdate(Hash source, Hash result) {
	for (int i=0; i<HASH_COUNT; ++i) {
		result[i] += source[i];
	}
}

void hashPrint(Hash hash) {
	for (int i=0; i<HASH_COUNT; ++i) {
		printf("hash[%d]=%llu\n",i,hash[i]);
	}
}

int main() {
	int myId = MyNodeId();
	int textLength = SeqLength();
	int patternLength = SignalLength();
	int nodes = std::min(NumberOfNodes(), textLength);
	int res = 0;

	if (myId >= nodes) return 0;

	int patternBegin = ((long long int) myId * (long long int) patternLength / nodes) + 1;
	int patternEnd = (long long int)(myId+1) * (long long int) patternLength / nodes;
	int textBegin = ((long long int) myId * (long long int) textLength / nodes) + 1;
	int textEnd = (long long int)(myId+1) * (long long int) textLength / nodes;

	std::vector<int> need[nodes];

	//hash dla patternu
	Hash patternPartHash[nodes];
	memset(patternPartHash,0,nodes*sizeof(Hash));
	for (int i=patternBegin; i<=patternEnd; ++i) {
		hashAdd(SignalAt(i), patternPartHash[myId]);
	}

	//broadcast my pattern hash
	for (int i=0; i<nodes; ++i) {
		if (i!=myId) {
			for (int j=0; j<HASH_COUNT;j++) PutLL(i,(long long int) patternPartHash[myId][j]);
			Send(i);
		}
	}

	//what hash prefixes will be needed
	for (int i=nodes-1; i>=0; --i) {
		int nBegin = ((long long int) i * (long long int) textLength / nodes) + 1;
		int nEnd = (long long int)(i+1) * (long long int) textLength / nodes;
		if (i>0) need[i].push_back(std::min(patternLength-1, nBegin-1));
		if (i != nodes-1) {
			for (int j=0; j<need[i+1].size(); ++j) {
				if (need[i+1][j] - (nEnd-nBegin+1) > 0) need[i].push_back(need[i+1][j] - (nEnd-nBegin+1));
			}
		}
		std::sort(need[i].begin(),need[i].end());
	}

	//receive hashes
	for (int i=0; i<nodes; ++i) {
		if (i!=myId) {
			Receive(i);
			for (int j=0; j<HASH_COUNT;j++) patternPartHash[i][j] = (unsigned long long int) GetLL(i);
		}
	}

	//calculate full pattern hash
	Hash patternHash;
	memset(patternHash,0,sizeof(Hash));
	for (int i=0; i<nodes; ++i) {
		int nBegin = ((long long int) i * (long long int) patternLength / nodes) + 1;
		int nEnd = (long long int)(i+1) * (long long int) patternLength / nodes;
		hashShift(nEnd+1-nBegin, patternHash);
		hashUpdate(patternPartHash[i], patternHash);
	}

	//######################################################################################################

/*
printf("Zakres %d %d\n",textBegin,textEnd);
printf("NEED :");
for (int j=0;j<need[myId].size();++j) printf("%d ",need[myId][j]);
printf("\n");
*/

	//count hash for next node
	Hash textPartHash;
	memset(textPartHash,0,sizeof(Hash));
	int npos = 0;
	if (myId != nodes-1) {
		int length = 0;
		unsigned long long int powers[HASH_COUNT];
		for (int i=0; i<HASH_COUNT; ++i) powers[i] = 1;

		for (int i=textEnd;i>=textBegin && npos<need[myId+1].size();--i) {
			length++;
			unsigned long long int val = SeqAt(i);
			for (int j=0; j<HASH_COUNT; ++j) {
				textPartHash[j] = textPartHash[j] + val*powers[j];
				powers[j] = powers[j] * PRIMES[j];
			}

			while (npos<need[myId+1].size() && need[myId+1][npos] == length) {
//printf("Send %llu\n",textPartHash[0]);
				for (int j=0; j<HASH_COUNT;j++) PutLL(myId+1,(long long int) textPartHash[j]);
				npos++;
			}
		}
	}

//printf("FULL %llu\n",textPartHash[0]);

	Hash rHash;
	memset(rHash,0,sizeof(Hash));
	if (myId > 0) {
		Receive(myId-1);
		for (int i=0; i<need[myId].size(); ++i) {
			for (int j=0; j<HASH_COUNT;j++) rHash[j] = (unsigned long long int) GetLL(myId-1);
//printf("Rec %llu\n",rHash[0]);

			if (myId != nodes-1 && npos<need[myId+1].size()) {
				hashShift(textEnd-textBegin+1, rHash);
				hashUpdate(textPartHash, rHash);
//printf("Send2 %llu\n",rHash[0]);
				for (int j=0; j<HASH_COUNT;j++) PutLL(myId+1,(long long int) rHash[j]);
				npos++;
			}
		}

	}

	if (myId != nodes-1) Send(myId+1);

	//hash dla textu
	unsigned long long int dp[HASH_COUNT];
	for (int i=0; i<HASH_COUNT;++i) dp[i] = power(PRIMES[i], patternLength-1);

	for (int i=textBegin; i<=textEnd; ++i) {
		hashAdd(SeqAt(i), rHash);
		if (i >= patternLength) {
			if (memcmp(rHash,patternHash,sizeof(Hash)) == 0) res++;

			for (int j=0; j<HASH_COUNT; ++j) {
				unsigned long long int val = SeqAt(i-patternLength+1);
				val *= dp[j];
				rHash[j] = rHash[j] - val;
			}
		}
	}

	if (myId!=0) {
		PutLL(0,res);
		Send(0);
	} else {
		for (int i=1; i<nodes;++i) {
			Receive(i);
			res += GetLL(i);
		}

		printf("%d\n",res);
	}

	return 0;
}