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
//Przemysław Jakub Kozłowski
#include "poszukiwania.h"
#include "message.h"
#include <iostream>
#include <cstdio>
#include <algorithm>
#define FI first
#define SE second
#define MP make_pair
using namespace std;
typedef long long LL;
typedef long long (*AtFun)(long long);
const int HP = 1000100033, HPP = 1000100077;

int pot(int a, int k, int mod)
{
    int ret = 1;
    while(k)
    {
        if(k%2) ret = (LL)ret*a%mod;
        k /= 2;
        a = (LL)a*a%mod;
    }
    return ret;
}

int NUM, ID;
int T, W;

inline pair<int,int> get_przedzial(int fn, int fnum, int fid)
{
    int fwn = max(1,fn/fnum);
    if(fwn*fid+1 > fn) return MP(fn+2, fn+1);
    return MP(fwn*fid+1, (fid == fnum-1 ? fn : fwn*fid+fwn));
}
inline int ofsize(const pair<int,int> &prz) {return prz.SE-prz.FI+1;}

int obl_hash_na_przedziale(pair<int,int> prz, AtFun At)
{
    if(prz.SE < prz.FI) return -1;
    int ret = 0;
    for(int i = prz.FI;i <= prz.SE;i++)
    {
        int a = At(i);
        ret = ((LL)ret*HP+a)%HPP;
    }
    return ret;
}

inline int merge(int A, int sizB, int B)
{
    return ((LL)A*pot(HP, sizB, HPP)+B)%HPP;
}

// S - wzorzec - Signal - W
// M - teskt - Seq - T
// S <= M

int faza2(int wzorzec_hash, const vector<int> &Thashe)
{
    pair<int,int> prz = get_przedzial(T, NUM, ID);
    if((LL)prz.FI+W-1 > T) return 0;

    int curr_hash = 0;
    int rozmiar = 0;
    for(int i = ID;i < NUM;i++)
    {
        pair<int,int> oprz = get_przedzial(T, NUM, i);
        if(oprz.SE <= prz.FI+W-1)
        {
            rozmiar += ofsize(oprz);
            curr_hash = merge(curr_hash, ofsize(oprz), Thashe[i]);
        }
        else break;
    }
    while(rozmiar < W)
    {
        //curr_hash = merge(curr_hash, 1, SeqAt(prz.FI+rozmiar));
        curr_hash = ((LL)curr_hash*HP+SeqAt(prz.FI+rozmiar))%HPP;
        rozmiar++;
    }

    int ret = 0;
    if(curr_hash == wzorzec_hash) ret++;
    int potcach = pot(HP, W-1, HPP);
    for(int i = prz.FI+1;i <= min(prz.SE, T-W+1);i++)
    {
        curr_hash = ((LL)HPP+curr_hash-(LL)SeqAt(i-1)*potcach%HPP)%HPP;
        curr_hash = ((LL)curr_hash*HP+SeqAt(i+W-1))%HPP;
        if(curr_hash == wzorzec_hash) ret++;
    }

    return ret;
}

int main()
{
    NUM = NumberOfNodes();
    ID = MyNodeId();
    T = SeqLength();
    W = SignalLength();

    // Obliczanie 1
    //int vector_size = ofsize(get_przedzial(T, NUM, ID))+1;
    //cerr << "vector_size: " << vector_size << endl;
    //vector<int> cache(vector_size);
    //cerr << "ID: " << ID << " Przedzialy: " << get_przedzial(T, NUM, ID).FI << " " << get_przedzial(T, NUM, ID).SE << " " << get_przedzial(W, NUM, ID).FI << " " << get_przedzial(W, NUM, ID).SE << endl;
    int Thash = obl_hash_na_przedziale(get_przedzial(T, NUM, ID), SeqAt);
    int Whash = obl_hash_na_przedziale(get_przedzial(W, NUM, ID), SignalAt);

    // Wysyłanie 2
    PutInt(0, Whash);
    PutInt(0, Thash);
    Send(0);

    //cerr << "ID: " << ID << " A" << endl;

    // Kumulacja 3
    if(ID == 0)
    {
        vector<int> Whashe(NUM);
        vector<int> Thashe(NUM);
        for(int i = 0;i < NUM;i++)
        {
            Receive(i);
            Whashe[i] = GetInt(i);
            Thashe[i] = GetInt(i);
        }

        int wzorc_hash = 0;
        for(int i = 0;i < NUM;i++)
            if(Whashe[i] != -1)
                wzorc_hash = ((LL)wzorc_hash*pot(HP, ofsize(get_przedzial(W, NUM, i)), HPP)+Whashe[i])%HPP;

        for(int i = 0;i < NUM;i++)
        {
            PutInt(i, wzorc_hash);
            for(int j = 0;j < NUM;j++)
                PutInt(i, Thashe[j]);
            Send(i);
        }
    }

    //cerr << "ID: " << ID << " B" << endl;

    // Odbieranie 4
    int wzorzec_hash;
    vector<int> Thashe(NUM);

    Receive(0);
    wzorzec_hash = GetInt(0);
    for(int j = 0;j < NUM;j++)
        Thashe[j] = GetInt(0);

    //cerr << "ID: " << ID << " C" << endl;

    // Mamy dhashe i signal_hash i cache
    // Następna faza
    int twyn = faza2(wzorzec_hash, Thashe);
    PutInt(0, twyn);
    Send(0);

    //cerr << "ID: " << ID << " D" << endl;

    // Wypisywanie
    if(ID == 0)
    {
        int wyn = 0;
        for(int j = 0;j < NUM;j++)
        {
            Receive(j);
            wyn += GetInt(j);
        }
        printf("%d\n", wyn);
    }

    //cerr << "ID: " << ID << " E" << endl;

    return 0;
}