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

using namespace std;

const int HASHES = 2;
const int INST = 105;
const int M[] = {1030492823, 1030492831},
          P[] = {1020492833, 1020492797};

          
int n, m, instances, instanceId;

int powerOfPattern[HASHES];

int patternHash[HASHES];
int beginIntervalHash[HASHES];
int endIntervalHash[HASHES];
int prefixHash[HASHES];

int wholePatternHash[HASHES];
int prefBeginIntervalHash[INST][HASHES];
int prefEndIntervalHash[INST][HASHES];
int wholePrefixHash[HASHES];

int patternLength;
int beginIntervalLength;
int endIntervalLength;
int prefixLength;

int power(int a, int k, int mod) {
    if (k == 0) {
        return 1;
    }
    return (long long)power((long long)a * a % mod, k / 2, mod) * (k % 2? a : 1) % mod;
}

void calcOnInterval(int *t, int firstLetter, int lastLetter, int type) {
    if (type == 0) {
        for (int i = firstLetter; i <= lastLetter; i++) {
            for (int j = 0; j < HASHES; j++) {
                t[j] = ((long long)t[j] * P[j] + SignalAt(i + 1)) % M[j];
            }
        }
    } else {
        for (int i = firstLetter; i <= lastLetter; i++) {
            for (int j = 0; j < HASHES; j++) {
                t[j] = ((long long)t[j] * P[j] + SeqAt(i + 1)) % M[j];
            }
        }
    }
}

void calcPatternHash() {
    int lettersPerInstance = (m + instances - 1) / instances;
    int firstLetter = instanceId * lettersPerInstance;
    int lastLetter = min(m, (instanceId + 1) * lettersPerInstance) - 1;
    patternLength = max(0, lastLetter - firstLetter + 1);
    calcOnInterval(patternHash, firstLetter, lastLetter, 0);
}

void calcBeginIntervalHash() {
    int l = n - m + 1;
    int lettersPerInstance = (l + instances - 1) / instances;
    int firstLetter = instanceId * lettersPerInstance;
    int lastLetter = min(l, (instanceId + 1) * lettersPerInstance) - 1;
    beginIntervalLength = max(0, lastLetter - firstLetter + 1);
    calcOnInterval(beginIntervalHash, firstLetter, lastLetter, 1);
}

void calcEndIntervalHash() {
    int l = n - m + 1;
    int lettersPerInstance = (l + instances - 1) / instances;
    int firstLetter = instanceId * lettersPerInstance + m - 1;
    int lastLetter = min(n, (instanceId + 1) * lettersPerInstance + m - 1) - 1;
    endIntervalLength = max(0, lastLetter - firstLetter + 1);
    calcOnInterval(endIntervalHash, firstLetter, lastLetter, 1);
}

void calcPrefixOfWordHash() {
    int l = m - 1;
    int lettersPerInstance = (l + instances - 1) / instances;
    int firstLetter = instanceId * lettersPerInstance;
    int lastLetter = min(l, (instanceId + 1) * lettersPerInstance) - 1;
    prefixLength = max(0, lastLetter - firstLetter + 1);
    calcOnInterval(prefixHash, firstLetter, lastLetter, 1);
}

void sendDataToMainInstance() {
   
    PutInt(0, patternLength);
    PutInt(0, beginIntervalLength);
    PutInt(0, endIntervalLength);
    PutInt(0, prefixLength);
    
    for (int i = 0; i < HASHES; i++) {
        PutInt(0, patternHash[i]);
        PutInt(0, beginIntervalHash[i]);
        PutInt(0, endIntervalHash[i]);
        PutInt(0, prefixHash[i]);
    }
    
    Send(0);    
}
int lengths[INST];
void processData() {
    for (int i = 0; i < instances; i++) {
        Receive(i);
        
        int patLen = GetInt(i);
        int begIntLen = GetInt(i);
        int endIntLen = GetInt(i);
        int prefLen = GetInt(i);
        lengths[i + 1] = endIntLen;
        for (int j = 0; j < HASHES; j++) {
            int pattHash = GetInt(i);
            int begIntHash = GetInt(i);
            int endIntHash = GetInt(i);
            int prefHash = GetInt(i);
            wholePatternHash[j] = ((long long)wholePatternHash[j] * power(P[j], patLen, M[j]) + pattHash) % M[j];
            wholePrefixHash[j] = ((long long)wholePrefixHash[j] * power(P[j], prefLen, M[j]) + prefHash) % M[j];

            prefBeginIntervalHash[i + 1][j] = ((long long)prefBeginIntervalHash[i][j] * power(P[j], begIntLen, M[j]) + begIntHash) % M[j];
            prefEndIntervalHash[i + 1][j] = ((long long)prefEndIntervalHash[i][j] * power(P[j], endIntLen, M[j]) + endIntHash) % M[j];            
        }
    }
    int sumOfLengths = 0;
    for (int i = 0; i < instances; i++) {
        sumOfLengths += lengths[i];
        for (int j = 0; j < HASHES; j++) {
            prefEndIntervalHash[i][j] = (prefEndIntervalHash[i][j] + (long long)power(P[j], sumOfLengths, M[j]) * wholePrefixHash[j]) % M[j];
        }
    }
    for (int i = 0; i < instances; i++) {
      
        for (int j = 0; j < HASHES; j++) {
            PutInt(i, wholePatternHash[j]);
            PutInt(i, prefBeginIntervalHash[i][j]);
            PutInt(i, prefEndIntervalHash[i][j]);
        }
        Send(i);
    }
    
}

int myPatternHash[HASHES];
int myBeginIntervalHash[HASHES];
int myEndIntervalHash[HASHES];

void calcPatternOccurences() {
    Receive(0);
    for (int i = 0; i < HASHES; i++) {
        myPatternHash[i] = GetInt(0);
        myBeginIntervalHash[i] = GetInt(0);
        myEndIntervalHash[i] = GetInt(0);
    }
    int l = n - m + 1;
    int lettersPerInstance = (l + instances - 1) / instances;
    int firstLetter = (instanceId * lettersPerInstance);
    int lastLetter = min(l, (instanceId + 1) * lettersPerInstance) - 1;
    int ans = 0;
    for (int i = firstLetter; i <= lastLetter; i++) {
        bool equal = true;
        for (int j = 0; j < HASHES; j++) {
            myEndIntervalHash[j] = ((long long)myEndIntervalHash[j] * P[j] + SeqAt(i + m)) % M[j];
            if (((myEndIntervalHash[j] - ((long long)myBeginIntervalHash[j] * powerOfPattern[j] % M[j])) + M[j]) % M[j] != myPatternHash[j]) {
                equal = false;
            }
            myBeginIntervalHash[j] = ((long long)myBeginIntervalHash[j] * P[j] + SeqAt(i + 1)) % M[j];
        }
        if (equal) {
            ans++;
        }
    }
    PutInt(0, ans);
    Send(0);
}

int getAnswer() {
    int ret = 0;
    for (int i = 0; i < instances; i++) {
        Receive(i);
        ret += GetInt(i);
    }
    return ret;
}

int main() {
    
    instances = NumberOfNodes();
    instanceId = MyNodeId();
   
    n = SeqLength();
    m = SignalLength();

    for (int i = 0; i < HASHES; i++) {
        powerOfPattern[i] = power(P[i], m, M[i]);
    }
    
    calcPatternHash();
    calcBeginIntervalHash();
    calcEndIntervalHash();
    calcPrefixOfWordHash();
    sendDataToMainInstance();
    
    if (instanceId == 0) {
        processData();
    }
    
    calcPatternOccurences();
    
    if (instanceId == 0) {
        printf("%d\n", getAnswer());
    }
    
    return 0;
}