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
259
260
#include <iostream>
#include <cassert>
#include <cstdio>
#include <vector>
#include <algorithm>
#include "message.h"
#include "poszukiwania.h"

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()
#define FORE(i,c) FOR(i,0,SIZE(c)-1)
#define FORDE(i,c) FORD(i,SIZE(c)-1,0)
#define PB push_back
#define MP make_pair
#define ST first
#define ND second

using namespace std;

typedef long long int LLI;
typedef pair < int , int > PII;
typedef pair < LLI , LLI > PLL;

typedef vector < int > VI;
typedef vector < bool > VB;
typedef vector < PII > VP;
typedef vector < LLI > VL;
typedef vector < PLL > VPL;

/*************************************************************************/

const int P = 1000000001;
const int MOD1 = 1000000007;
const int MOD2 = 1000000009;

int REV_P1;
int REV_P2;

/*************************************************************************/

LLI expo(LLI a, LLI n, LLI mod)
{
    if (!n)
        return 1;
    else
    {
        LLI temp = expo(a, n/2, mod);
        temp = (temp * temp)%mod;

        if (n%2)
            temp = (temp * a)%mod;

        return temp;
    }
}

/*************************************************************************/

void appendToHash(PLL &curr, PLL v, int pieceLen = 1)
{
    curr.ST = (v.ST + curr.ST * expo(P, pieceLen, MOD1))%MOD1;
    curr.ND = (v.ND + curr.ND * expo(P, pieceLen, MOD2))%MOD2;
}

/* get hash of pattern[i:j] */
PLL getPatternHash(PII slice)
{
    PLL ans = MP(0,0);

    FORD(it,slice.ND,slice.ST)
    {
        LLI val = SignalAt(it + 1);
        appendToHash(ans, MP(val,val));
    }

    return ans;
}

/* get hash of text[i:j] */
PLL getTextHash(PII slice)
{
    PLL ans = MP(0,0);

    FORD(it,slice.ND,slice.ST)
    {
        LLI val = SeqAt(it + 1);
        appendToHash(ans, MP(val,val));
    }

    return ans;
}

void rollHash(PLL &curr, int &L, int &R, PLL &power)
{
    R++;

    PLL first = getTextHash(MP(L,L)),
        last = getTextHash(MP(R,R));

    curr.ST = (curr.ST + last.ST * power.ST)%MOD1;
    curr.ND = (curr.ND + last.ND * power.ND)%MOD2;

    curr.ST = (curr.ST + MOD1 - first.ST)%MOD1;
    curr.ND = (curr.ND + MOD2 - first.ND)%MOD2;

    curr.ST = (curr.ST * REV_P1)%MOD1;
    curr.ND = (curr.ND * REV_P2)%MOD2;

    L++;
}

/*************************************************************************/

int divide(int len)
    { return (len + NumberOfNodes() - 1) / NumberOfNodes(); }

PII getMyPiece(int len)
{
    int piece = divide(len);
    int myId = MyNodeId();

    return MP(myId * piece, min(len, (myId + 1) * piece) - 1);
}

void PutPLL(int target, PLL value)
{
    PutLL(target, value.ST);
    PutLL(target, value.ND);
}

PLL GetPLL(int source)
{
    LLI x = GetLL(source);
    LLI y = GetLL(source);

    return MP(x,y);
}

/*************************************************************************/

int main()
{
    ios_base::sync_with_stdio(0);

    REV_P1 = expo(P, MOD1 - 2, MOD1);
    REV_P2 = expo(P, MOD2 - 2, MOD2);

    int patternLen = SignalLength();
    int textLen = SeqLength();

    int patternPiece = divide(patternLen);
    int textPiece = divide(textLen);

    int myId = MyNodeId();
    int nodes = NumberOfNodes();

    /* hash of my piece of the pattern */
    PLL myPatternHash = getPatternHash(getMyPiece(patternLen));

    /* my piece of the text */
    PII myTextPiece = getMyPiece(textLen);

    /* hash of my piece of the text */
    PLL myTextHash = getTextHash(myTextPiece);

    PutPLL(0, myPatternHash);
    PutPLL(0, myTextHash);
    Send(0);

    if (myTextPiece.ST + patternLen >= textLen)
    {
        /* this node won't have any pattern searching to do */
        return 0;
    }

    /* pieces of text that fit in the pattern */
    int piecesCnt = patternLen / textPiece;

    if (!myId)
    {
        /* combined pattern hash */
        PLL wholePattern = MP(0,0);

        VPL textHashes;

        FORD(i,nodes-1,0)
        {
            Receive(i);
            appendToHash(wholePattern, GetPLL(i), patternPiece);
        }

        FOR(i,0,nodes-1)
            textHashes.PB(GetPLL(i));

        FOR(i,0,nodes-1)
        {
            PLL fragHash = MP(0,0);

            if (i * textPiece + patternLen >= textLen)
                continue ;

            int L = i,
                R = i + piecesCnt- 1;

            FORD(j,R,L)
                appendToHash(fragHash, textHashes[j], textPiece);

            PutPLL(i, wholePattern);
            PutPLL(i, fragHash);
            Send(i);
        }
    }

    Receive(0);

    PLL wholePattern = GetPLL(0);
    PLL fragHash = GetPLL(0);

    int L = myTextPiece.ST,
        R = L + patternLen - 1,
        currR = L + piecesCnt * textPiece - 1;

    PLL rollingHash = getTextHash( MP(currR + 1, R) );
    appendToHash(rollingHash, fragHash, currR - L + 1);

    int cnt = (rollingHash == wholePattern);

    PLL power = MP(
        expo(P, R - L + 1, MOD1),
        expo(P, R - L + 1, MOD2)
    );

    while (L < myTextPiece.ND && R < textLen - 1)
    {
        rollHash(rollingHash, L, R, power);
        cnt += (rollingHash == wholePattern);
    }

    PutInt(0, cnt);
    Send(0);

    if (!myId)
    {
        int sum = 0;

        FOR(i,0,nodes-1)
            if (i * textPiece + patternLen < textLen)
            {
                Receive(i);
                sum += GetInt(i);
            }

        cout << sum;
    }

    return 0;
}

/*************************************************************************/