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
#include "message.h"
#include "poszukiwania.h"
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef long long i64;
typedef __int128 i128;

template<typename T>
inline T pow2(int x) { return T(1) << x; }

inline int getSig(int i) { return SignalAt(i+1)+1; }
inline int getSeq(int i) { return SeqAt(i+1)+1; }

struct Problem
{
  Problem() : s_(SignalLength()), m_(SeqLength()), n_(NumberOfNodes()), ix_(MyNodeId()),
              seqPieces_(min(m_,n_*10)), seqPS_(m_/seqPieces_),
              sigHash_(0),
              seqPieceHash_(seqPieces_)
  {}
  
  int go();
  void compSignalHash();
  void compSeqPieceHashes();
  int compOccurrences();
  i64 bPow(unsigned e);
  
  const int s_,m_,n_,ix_;
  const int seqPieces_, seqPS_;
  i64 sigHash_;
  vector<i64> seqPieceHash_;
  static constexpr int b_ = 1000000033;
  static constexpr i64 p_ = 4000000000000000121ll;
};

int Problem::go()
{
  compSignalHash();
  compSeqPieceHashes();
  int result = compOccurrences();
  if (ix_ != 0)
  {
    PutInt(0,result);
    Send(0);
    return -1;
  }
  for (int i=1; i<n_; ++i)
  {
    Receive(i);
    result += GetInt(i);
  }
  return result;
}

void Problem::compSignalHash()
{
  if (s_ < n_)
  {
    for (int i=0; i<s_; ++i)
      sigHash_ = ((i128)sigHash_*b_ + getSig(i)) % p_;
    return;
  }
  int sigPS = s_/n_;
  i64 h = 0;
  int end = (ix_ == n_-1) ? s_ : (ix_+1)*sigPS;
  for (int i=ix_*sigPS; i<end; ++i)
    h = ((i128)h*b_ + getSig(i)) % p_;
  h = ((i128)h * bPow(s_-end)) % p_;
  if (ix_ == 0)
  {
    for (int i=1; i<n_; ++i)
    {
      Receive(i);
      h = (h + GetLL(i)) % p_;
    }
    sigHash_ = h;
    for (int i=1; i<n_; ++i)
    {
      PutLL(i,h);
      Send(i);
    }
  }
  else
  {
    PutLL(0,h);
    Send(0);
    Receive(0);
    sigHash_ = GetLL(0);
  }
}

void Problem::compSeqPieceHashes()
{
  if (m_ < n_)
  {
    for (int i=0; i<m_; ++i)
      seqPieceHash_[i] = getSeq(i);
    return;
  }
  int piecesPerNode = seqPieces_ / n_;
  int piecesEnd = (ix_ == n_-1) ? seqPieces_ : (ix_+1)*piecesPerNode;
  for (int i=ix_*piecesPerNode; i<piecesEnd; ++i)
  {
    int end = (i == seqPieces_-1) ? m_ : (i+1)*seqPS_;
    i64 h = 0;
    for (int j=i*seqPS_; j<end; ++j)
      h = ((i128)h*b_ + getSeq(j)) % p_;
    seqPieceHash_[i] = h;
    if (ix_ != 0)
      PutLL(0,h);
  }
  if (ix_ == 0)
  {
    for (int i=1; i<n_; ++i)
    {
      Receive(i);
      int iPiecesEnd = (i == n_-1) ? seqPieces_ : (i+1)*piecesPerNode;
      for (int j=i*piecesPerNode; j<iPiecesEnd; ++j)
        seqPieceHash_[j] = GetLL(i);
    }
    for (int i=1; i<n_; ++i)
    {
      for (int j=0; j<seqPieces_; ++j)
        PutLL(i, seqPieceHash_[j]);
      Send(i);
    }
  }
  else
  {
    Send(0);
    Receive(0);
    for (int i=0; i<seqPieces_; ++i)
      seqPieceHash_[i] = GetLL(0);
  }
}

int Problem::compOccurrences()
{
  if (ix_ > m_-s_)
    return 0;
  int occPS = max(1, (m_-s_+1)/n_);
  i64 h = 0;
  i64 bSeqPS = bPow(seqPS_);
  for (int i=0; i<s_; ++i)
  {
    int j = i + ix_*occPS;
    if (j%seqPS_ == 0)
    {
      int ps = (j/seqPS_ == seqPieces_-1) ? m_-(seqPieces_-1)*seqPS_ : seqPS_;
      if (i + ps <= s_)
      {
        i64 bps = (ps == seqPS_) ? bSeqPS : bPow(ps);
        h = ((i128)h * bps + seqPieceHash_[j/seqPS_]) % p_;
        i += ps-1;
      }
      else
        h = ((i128)h*b_ + getSeq(j)) % p_;
    }
    else
      h = ((i128)h*b_ + getSeq(j)) % p_;
  }
  int end = (ix_ == n_-1) ? m_-s_+1 : (ix_+1)*occPS;
  int result = (h == sigHash_);
  i64 bS1 = bPow(s_-1);
  for (int i=ix_*occPS; i<end-1; ++i)
  {
    h = (h - (i128)getSeq(i)*bS1) % p_;
    if (h < 0)
      h += p_;
    h = ((i128)h*b_ + getSeq(i+s_)) % p_;
    result += (h == sigHash_);
  }
  return result;
}

i64 Problem::bPow(unsigned e)
{
  if (e == 0)
    return 1;
  i64 x = 1;
  for (int i=31-__builtin_clz(e); i>=0; --i)
  {
    x = ((i128)x*x) % p_;
    if (e & pow2<unsigned>(i))
      x = ((i128)x*b_) % p_;
  }
  return x;
}

int main()
{
  Problem p;
  int r = p.go();
  if (r != -1)
    printf("%d\n", r);
}