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
//Piotr Pusz
#include "poszukiwania.h"
#include "message.h"
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

long long instancje, jam;
long long hasz1, hasz2, dlsygnalu, dlciagu, literkas, literkac, potega1=1, potega2=1, hasz1sygn, hasz2sygn, hasz11, hasz22, potega11=1, potega22=1, hasz1megopoczatku, hasz2megopoczatku;
long long p1=1000000007, p2=1000000009, hasz1tutaj, hasz2tutaj, p3=1000000021;
int pocz, kon, pocz2, kon2, skadchceinfo, odkogochceinfo;
long long jakietoinfo1, jakietoinfo2;
long long hasze[3000][5], maodpowiedz, lacznaodpowiedz, pot111=1, pot222=1;
int tab1[500], tab2[500];

long long SignalAt (int j)
{
    return tab1[j];
}

long long SeqAt (int j)
{
    return tab2[j];
}

int main()
{

    ios_base::sync_with_stdio(0);

    instancje=NumberOfNodes();
    jam=MyNodeId();
    //instancje=1;
    //jam=0;

//cin>>dlsygnalu>>dlciagu;
    dlsygnalu=SignalLength();
    dlciagu=SeqLength();
//for (int i=1; i<=dlsygnalu; ++i) cin>>tab1[i];
//for (int i=1; i<=dlciagu; ++i) cin>>tab2[i];
    pocz=((instancje-1+dlsygnalu)/instancje)*jam+1;
    kon=min((long long)pocz+((instancje-1+dlsygnalu)/instancje)-1, dlsygnalu);

    // 0-->1, 1-->2,   *3 przed nast polem

    for (int i=pocz; i<=kon; ++i)
    {
        hasz1=(p3*hasz1+SignalAt(i)+1)%p1;
        hasz2=(p3*hasz2+SignalAt(i)+1)%p2;
        potega1=(p3*potega1)%p1;
        potega2=(p3*potega2)%p2;
        pot111=(p3*pot111)%p1;
        pot222=(p3*pot222)%p2;
        cout<<":"<<hasz1<<' '<<hasz2<<endl;
    }

    pocz2=((instancje-1+dlciagu)/instancje)*jam+1;
    kon2=min((long long)pocz2+((instancje-1+dlciagu)/instancje)-1, dlciagu);

    for (int i=pocz2; i<=kon2; ++i)
    {
        hasz11=(p3*hasz11+SeqAt(i)+1)%p1;
        hasz22=(p3*hasz22+SeqAt(i)+1)%p2;
        potega11=(p3*potega11)%p1;
        potega22=(p3*potega22)%p2;
        cout<<";"<<hasz11<<' '<<hasz22<<endl;
    }
    //for (int i=kon2+1; i<=pocz2+jam-1; ++i)
    //{
    //    potega11=(3*potega11)%p1;
    //    potega22=(3*potega22)%p2;
    //}
    hasze[jam][1]=hasz1;
    hasze[jam][2]=hasz2;
    hasze[jam][3]=hasz11;
    hasze[jam][4]=hasz22;

    if (jam!=0)
    {
        PutLL(0,hasz1);
        PutLL(0,hasz2);
        PutLL(0,hasz11);
        PutLL(0,hasz22);
        PutLL(0,pot111);
        PutLL(0,pot222);
        //wszystko lepiej w jednym wyslaniu zeby sie nie zepsulo cos
        Send(0);
    }
    else
    {
        for (int i=1; i<instancje; ++i)
        {
            Receive(i);
            hasze[i][1]=GetLL(i);
            hasze[i][2]=GetLL(i);
            hasze[i][3]=GetLL(i);
            hasze[i][4]=GetLL(i);
            pot111=(pot111*GetLL(i))%p1;
            pot222=(pot222*GetLL(i))%p2;
            hasze[i][1]=((hasze[i-1][1]*potega1)+hasze[i][1])%p1;
            hasze[i][2]=((hasze[i-1][2]*potega2)+hasze[i][2])%p2;
            hasze[i][3]=((hasze[i-1][3]*potega11)+hasze[i][3])%p1;
            hasze[i][4]=((hasze[i-1][4]*potega22)+hasze[i][4])%p2;
        }
        hasz1sygn=hasze[instancje-1][1];
        hasz2sygn=hasze[instancje-1][2];
        for (int i=1; i<instancje; ++i)
        {
            PutLL(i,hasz1sygn);
            PutLL(i,hasz2sygn);
            PutLL(i,hasze[i-1][3]);
            PutLL(i,hasze[i-1][4]);
            PutLL(i,pot111);
            PutLL(i,pot222);
            Send(i);
        }
    }
    //haszpoczatkutowlasciwiehaszpopredniego

    if (jam==0)
    {
        hasz1megopoczatku=0;
        hasz2megopoczatku=0;
    }
    else
    {
        Receive(0);
        hasz1sygn=GetLL(0);
        hasz2sygn=GetLL(0);
        hasz1megopoczatku=GetLL(0);
        hasz2megopoczatku=GetLL(0);
        pot111=GetLL(0);
        pot222=GetLL(0);
    }

    hasz1tutaj=hasz1megopoczatku;
    hasz2tutaj=hasz2megopoczatku;
    for (int i=pocz2; i<=kon2; ++i)
    {
        int komu=(i+dlsygnalu)/((instancje-1+dlsygnalu)/instancje);
        if (komu>=instancje) continue;
        PutLL(komu,hasze[i][3]);
        PutLL(komu,hasze[i][4]);
        Send(komu);
    }
    
    int skad=(dlsygnalu+((instancje-1+dlsygnalu)/instancje)-1)/((instancje-1+dlsygnalu)/instancje);
    long long przeczekaj=(((long long)((instancje-1+dlsygnalu)/instancje)*(long long)1000000003)-dlsygnalu)%((instancje-1+dlsygnalu)/instancje);
    if (przeczekaj<0) przeczekaj+=1000000003;
    long long spam=0;
    for (int i=0; i<przeczekaj; ++i)
    {
        skad=(pocz2+i-dlsygnalu-1)/((instancje-1+dlsygnalu)/instancje);
        Receive(skad);
        spam=GetLL(skad);
        spam=GetLL(skad);
    }
    
    for (int i=pocz2; i<=kon2; ++i)
    {
        hasz1tutaj=(hasz1tutaj*p3+SeqAt(i)+1)%p1;
        hasz2tutaj=(hasz2tutaj*p3+SeqAt(i)+1)%p2;
        
        jakietoinfo1=0;
        jakietoinfo2=0;
        skadchceinfo=i-dlsygnalu;
        odkogochceinfo=skadchceinfo/((instancje-1+dlsygnalu)/instancje);
        if (skadchceinfo>0) 
        {
            Receive(odkogochceinfo);
            jakietoinfo1=GetLL(odkogochceinfo);
            jakietoinfo2=GetLL(odkogochceinfo);
        }
        long long haszsz1=(hasz1tutaj-((jakietoinfo1*pot111)%p1))%p1;
        if (haszsz1<0) haszsz1+=p1;
        long long haszsz2=(hasz2tutaj-((jakietoinfo2*pot222)%p2))%p2;
        if (haszsz2<0) haszsz2+=p2;
        if (hasz1sygn==haszsz1 && hasz2sygn==haszsz2) ++maodpowiedz;
    }

    if (jam!=0)
    {
        PutLL(0,maodpowiedz);
        Send(0);
    }
    else
    {
        lacznaodpowiedz=maodpowiedz;
        for (int i=1; i<instancje; ++i)
        {
            Receive(i);
            lacznaodpowiedz+=GetLL(i);
        }
        cout<<lacznaodpowiedz;
    }

    return 0;
}