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
#include <cstdio>
#include <vector>
#include "message.h"
#include "poszukiwania.h"
using namespace std;
long long n,m,k;
long long p = 1000000009, mod[3] = {2000000011, 2000000033, 2000000063}, hash_ilosc = 3;
inline long long modulo(long long l, int j){
    return ((l % mod[j]) + mod[j]) % mod[j];
}
inline long long max(long long a, long long b){
    if (a > b)
        return a;
    return b;
}
int main(){
    n = SignalLength();
    m = SeqLength();
    k = NumberOfNodes();
    if (m < n){
        if (MyNodeId() == 0){
            printf("0\n");
        }
        return 0;
    }
    long long e = MyNodeId() * n / k + 1, f = (MyNodeId() + 1) * n / k - 1 + 1;
    long long hash[3] = {0,0,0};
    long long pot[3] = {1,1,1};
    for (int i = e; i <= f; i++){
        for (int j = 0; j < hash_ilosc; j++){
            pot[j] = modulo(p * pot[j], j);
            hash[j] = modulo(pot[j] * (SignalAt(i)+1) + hash[j], j);
        }
    }
    if (MyNodeId() > 0){
        Receive(MyNodeId()-1);
        for (int i = 0; i < hash_ilosc; i++){
            int _pot = GetLL(MyNodeId()-1);
            int _hash = GetLL(MyNodeId()-1);
            pot[i] = modulo(pot[i] * _pot, i);
            hash[i] = modulo(hash[i] * _pot + _hash, i);
        }
    }
    if (MyNodeId() < k-1){
        for (int i = 0; i < hash_ilosc; i++){
            PutLL(MyNodeId()+1, pot[i]);
            PutLL(MyNodeId()+1, hash[i]);
        }
        Send(MyNodeId()+1);
        Receive(k-1);
        for (int i = 0; i < hash_ilosc; i++){
            hash[i] = GetLL(k-1);
        }
    }
    else {
        for (int i = 0; i < k-1; i++){
            for (int j = 0; j < hash_ilosc; j++){
                PutLL(i, hash[j]);
            }
            Send(i);
        }
    }
    if ((n <= m/(2*k)) || (n == 1)){
        e = max(n, MyNodeId() * m / k + 1);
        f = (MyNodeId() + 1) * m / k - 1 + 1;
        int ee = e - n + 1;
        if (f < e){
            PutInt(k-1,0);
            Send(k-1);
            return 0;
        }
        long long hash2[3] = {0,0,0};
        long long pot2[3] = {1,1,1};
        long long hash3[3] = {0,0,0};
        long long pot3[3] = {1,1,1};
        long long hash4[3] = {0,0,0};
        long long pot4[3] = {1,1,1};
        for (int i = ee; i < e; i++){
            for (int j = 0; j < hash_ilosc; j++){
                pot2[j] = modulo(p * pot2[j], j);
                hash2[j] = modulo(pot2[j] * (SeqAt(i)+1) + hash2[j], j);
            }
        }
        int count = 0;
        for (int i = 0; i < hash_ilosc; i++){
            pot3[i] = pot2[i];
            hash3[i] = hash2[i];
        }
        for (int i = e; i <= f; i++){
            for (int j = 0; j < hash_ilosc; j++){
                pot3[j] = modulo(p * pot3[j], j);
                hash3[j] = modulo(pot3[j] * (SeqAt(i)+1) + hash3[j], j);
            }
            bool found = true;
            for (int j = 0; j < hash_ilosc; j++){
                if (modulo(hash[j] * pot4[j], j) != modulo(hash3[j] - hash4[j], j)){
                    found = false;
                }
            }
            for (int j = 0; j < hash_ilosc; j++){
                pot4[j] = modulo(p * pot4[j], j);
                hash4[j] = modulo(pot4[j] * (SeqAt(i-n+1)+1) + hash4[j], j);
            }
            if (found){
                count++;
            }
        }
        if (MyNodeId() < k-1){
            PutInt(k-1, count);
            Send(k-1);
        }
        else {
            for (int i = 0; i < k-1; i++){
                Receive(i);
                count += GetInt(i);
            }
            printf("%d\n", count);
        }
        return 0;
    }
    else {
        if (k > n-1){
            k = n-1;
        }
        if (MyNodeId() >= k){
            return 0;
        }
        e = MyNodeId() * (n-1) / k + 1;
        f = (MyNodeId() + 1) * (n-1) / k - 1 + 1;
        vector < long long > pocz, kon;
        while (e <= m){
            pocz.push_back(e);
            kon.push_back(f);
            e += n-1;
            f += n-1;
            f = min((long long)m, f);
        }
        long long hash3[3] = {0,0,0};
        long long pot3[3] = {1,1,1};
        long long hash4[3] = {0,0,0};
        long long pot4[3] = {1,1,1};
        long long hash5[3] = {0,0,0};
        long long pot5[3] = {1,1,1};
        int count = 0;
        for (int i = 0; i < pocz.size(); i++){
            int to = (MyNodeId() + 1) % k;
            long long hash2[3] = {0,0,0};
            long long pot2[3] = {1,1,1};
            for (int j = pocz[i]; j <= kon[i]; j++){
                for (int l = 0; l < hash_ilosc; l++){
                    pot2[l] = modulo(p * pot2[l], l);
                    hash2[l] = modulo(pot2[l] * (SeqAt(j)+1) + hash2[l], l);
                }
            }
            if ((MyNodeId() > 0) || (i != 0)){
                int from = ((MyNodeId()-1) + k) % k;
                Receive(from);
                for (int j = 0; j < hash_ilosc; j++){
                    long long _pot = GetLL(from);
                    long long _hash = GetLL(from);
                    pot3[j] = _pot;
                    hash3[j] = _hash;
                    pot5[j] = _pot;
                    hash5[j] = _hash;
                    pot2[j] = modulo(pot2[j] * _pot, j);
                    hash2[j] = modulo(hash2[j] * _pot + _hash, j);
                }
            }
            for (int j = 0; j < hash_ilosc; j++){
                PutLL(to, pot2[j]);
                PutLL(to, hash2[j]);
            }
            Send(to);
            if (i > 0){
                for (int j = pocz[i]; j <= kon[i]; j++){
                    for (int l = 0; l < hash_ilosc; l++){
                        pot3[l] = modulo(p * pot3[l], l);
                        hash3[l] = modulo(pot3[l] * (SeqAt(j)+1) + hash3[l], l);
                    }
                    bool found = true;
                    for (int l = 0; l < hash_ilosc; l++){
                        if (modulo(hash[l] * pot4[l], l) != modulo(hash3[l] - hash4[l], l)){
                            found = false;
                        }
                    }
                    for (int l = 0; l < hash_ilosc; l++){
                        pot4[l] = modulo(p * pot4[l], l);
                        hash4[l] = modulo(pot4[l] * (SeqAt(j-n+1)+1) + hash4[l], l);
                    }
                    if (found){
                        count++;
                    }
                }
            }
            for (int j = 0; j < hash_ilosc; j++){
                pot4[j] = pot5[j];
                hash4[j] = hash5[j];
            }
        }
        if (MyNodeId() > 0){
            PutLL(0, -1);
            PutInt(0, count);
            Send(0);
        }
        else {
            for (int i = 1; i < k; i++){
                Receive(i);
                long long t = GetLL(i);
                while (t != -1){
                    for (int i = 0; i < 2*hash_ilosc-1; i++){
                        GetLL(i);
                    }
                    Receive(i);
                    t = GetLL(i);
                }
                count += GetInt(i);
            }
            printf("%d\n", count);
        }
        return 0;
    }
}