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
#include "poszukiwania.h"
#include "message.h"
#include <cstdio>
#include <algorithm>
using namespace std;
/*
long long SeqAt(int a)
{
    return 1LL;
}

long long SignalAt(int a)
{
    return 1LL;
}

long long SeqLength()
{
    return 1LL;
}

long long SignalLength()
{
    return 1LL;
}

void PutLL(int a, long long b)
{
    a = 3;
}

void PutInt(int a, int b){
    a = 3;
}

void PutChar(int a, char b){
    a = 3;
}

void Send(int a){
    a = 3;
}

void Receive(int a){
    a = 3;
}


long long GetLL(int a)
{
    return 1LL;
}

int GetInt(int a)
{
    return 1;
}

char GetChar(int a)
{
    return 'a';
}

int MyNodeId()
{
    return 1;
}

int NumberOfNodes()
{
    return 1;
}
*/
const long long hash_base = 1000000033, mod = 100000000000000019LL;
const int mm_base = 64, max_amount_of_nodes = 1000;

long long parts_of_hashW[max_amount_of_nodes];

long long mm_temp;

long long modular_multiplication(long long a, long long b)
{
    if (b <= mm_base)
        return ((a * b) % mod);

    mm_temp = modular_multiplication(a, b / mm_base);
    mm_temp = ((mm_temp * mm_base) % mod);
    mm_temp = ((mm_temp + a * (b % mod)) % mod);

    return mm_temp;
}

long long count_hash_on_segment(int begin, int end, bool pattern)
{
    long long res = 0;

    for (int i=end-1; i>=begin; i--)
    {
        res = modular_multiplication(res, hash_base);
        res = ((res + 1 + (pattern ? SignalAt(i + 1) : SeqAt(i + 1))) % mod);
    }

    return res;
}

long long me_temp;

long long modular_exp(long long base, int exp)
{
    if (exp == 0)
        return 1LL;

    me_temp = modular_exp(base, exp / 2);
    me_temp = modular_multiplication(me_temp, me_temp);

    if (exp % 2 == 1)
        me_temp = modular_multiplication(me_temp, base);

    return me_temp;
}

long long hash_of_pattern, offset, range;

void count_my_part_of_hp_and_send()
{
    int begin = SignalLength() / NumberOfNodes() *  MyNodeId();
    int end = begin + SignalLength() / NumberOfNodes();

    if (((MyNodeId() == NumberOfNodes() - 1) and (SignalLength() >= NumberOfNodes())) or ((MyNodeId() == 0) and (SignalLength() < NumberOfNodes())))
        end = SignalLength();

    PutLL(0, count_hash_on_segment(begin, end, 1));
    Send(0);
}

void create_hash_of_pattern_and_send()
{
    long long offset_temp = 1LL;

    for (int i=0; i<NumberOfNodes(); i++)
    {
        Receive(i);
        hash_of_pattern = ((hash_of_pattern + modular_multiplication(GetLL(i), offset_temp)) % mod);
        offset_temp = modular_multiplication(offset_temp, offset);
    }

    for (int i=0; i<NumberOfNodes(); i++)
    {
        PutLL(i, hash_of_pattern);
        Send(i);
    }
}

long long modulo(long long a)
{
    return (((a % mod) + mod) % mod);
}

int main()
{
    offset = modular_exp(hash_base, SignalLength() / NumberOfNodes());

    count_my_part_of_hp_and_send();

    if (MyNodeId() == 0)
        create_hash_of_pattern_and_send();

    Receive(0);
    hash_of_pattern = GetLL(0);

    int begin = SeqLength() / NumberOfNodes() *  MyNodeId();
    int end = begin + SeqLength() / NumberOfNodes();

    if (((MyNodeId() == NumberOfNodes() - 1) and (SeqLength() >= NumberOfNodes())) or ((MyNodeId() == 0) and (SeqLength() < NumberOfNodes())))
        end = SeqLength();

    parts_of_hashW[MyNodeId()] = count_hash_on_segment(begin, end, 0);

    for (int i=0; i<MyNodeId(); i++)
    {
        PutLL(i, parts_of_hashW[MyNodeId()]);
        Send(i);
    }

    for (int i=MyNodeId()+1; i<NumberOfNodes(); i++)
    {
        Receive(i);
        parts_of_hashW[i] = GetLL(i);
    }

    int first_occ = min((long long)end - 1, SeqLength() - SignalLength());

    if (first_occ < begin)
    {
        PutInt(0, 0);
        Send(0);
        return 0;
    }

    int current_end = end;
    long long offset_temp = modular_exp(hash_base, current_end - first_occ);
    long long current_hash = count_hash_on_segment(first_occ, end, 0);
    int width_of_segment = SeqLength() / NumberOfNodes();
    offset = modular_exp(hash_base, width_of_segment);

    for (int i=MyNodeId()+1; i<NumberOfNodes(); i++)
    {
        current_end += width_of_segment;

        long long hash_temp = parts_of_hashW[i];

        if ((i == NumberOfNodes() - 1) or (first_occ + SignalLength() < current_end))
        {
            hash_temp = count_hash_on_segment(current_end - width_of_segment, first_occ + SignalLength(), 0);
            i = NumberOfNodes();
        }

        current_hash = ((current_hash + modular_multiplication(offset_temp, hash_temp)) % mod);
        offset_temp = modular_multiplication(offset_temp, offset);
    }

    offset = modular_exp(hash_base, SignalLength() - 1);

    int res_temp = 0;

    for (int i=end-1; i>=begin; i--)
    {
        if (current_hash == hash_of_pattern)
            res_temp++;

        if (i == begin)
            break;

        current_hash = modulo(current_hash - modular_multiplication(SeqAt(i + SignalLength()) + 1, offset));
        current_hash = modular_multiplication(current_hash, hash_base);
        current_hash = (current_hash + SeqAt(i) + 1);
    }

    PutInt(0, res_temp);
    Send(0);

    if (MyNodeId() > 0)
        return 0;

    int res = 0;

    for (int i=0; i<NumberOfNodes(); i++)
    {
        Receive(i);
        res += GetInt(i);
    }

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