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
#include "poszukiwania.h"
#include "message.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define INF 1001001001
#define FOR(i,n) for(int (i)=0;(i)<(n);++(i))
#define FORI(i,n) for(int (i)=1;(i)<=(n);++(i))
#define mp make_pair
#define pii pair<int,int>
#define ll long long
#define vi vector<int>
#define SZ(x) ((int)((x).size()))
#define fi first
#define se second
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));
#define wez3(n,m,k) int (n),(m),(k); scanf("%d %d %d",&(n),&(m),&(k));
inline void pisz(int n) { printf("%d\n",n); }
template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream& operator<<(ostream &s,vector<T> t){FOR(i,SZ(t))s<<t[i]<<" ";return s; }
#define DBG(vari) cout<<"["<<__LINE__<<"] "<<#vari<<" = "<<(vari)<<endl;
#define ALL(t) t.begin(),t.end()
#define FOREACH(i,t) for (__typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define TESTS wez(testow)while(testow--)
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)
#define REPD(i,a,b) for(int (i)=(a); (i)>=(b);--i)
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define IOS ios_base::sync_with_stdio(0);

constexpr int P1 = 2000500163; // postaci 2p+1
typedef unsigned long long ull;
struct Int {
   unsigned x1;
   ull u;
   Int() : x1(0), u(0) {}
   Int (unsigned x) : x1(x), u(x) {} // 0 <= x < P2!
   Int (unsigned y1, ull y2) : x1(y1), u(y2) {}
   inline Int operator + (const Int &x) const {
      Int y = *this;
      y.x1 += x.x1;
      if (y.x1 >= P1) y.x1 -= P1;
      y.u += x.u;
      return y;
   }
   inline void operator += (const Int &x) {
      x1 += x.x1;
      if (x1 >= P1) x1 -= P1;
      u += x.u;
   }
   inline Int operator * (const Int &x) const {
      return Int(((ull)x1*x.x1)%P1, u*x.u);
   }
   inline Int operator - (const Int &x) const {
      return Int((x1 >= x.x1) ? x1 - x.x1 : P1 + x1 - x.x1,
                 u - x.u
                );
   }
   inline bool operator == (const Int &x) const {
      return x1==x.x1 && u==x.u;
   }
   void sendTo (int target) const {
      PutInt(target, static_cast<int>(x1));
      PutLL(target, static_cast<ll>(u));
      Send(target);
   }
};
Int receiveFrom (int source) {
   Receive(source);
   unsigned x1 = static_cast<unsigned>(GetInt(source));
   ull u = static_cast<ull>(GetLL(source));
   return Int(x1, u);
}

const Int Q(2000000503); // rzedu P-1 w grupie Z*_P
Int Qpowbin[32];

Int Qpow (int k) {
   Int res(1);
   REP(b,0,30) if ((1<<b) & k) res = res * Qpowbin[b];
   return res;
}

int main() {
   Qpowbin[0] = Q;
   REP(k,1,30) Qpowbin[k] = Qpowbin[k-1] * Qpowbin[k-1];

   const int n = SeqLength(),
             m = SignalLength(),
             my = MyNodeId() + 1,
             allWorkers = NumberOfNodes();

   // faza 1 - wylicz hasz wzorca
   const int workers1 = min(allWorkers, m);
   if (my > workers1) {
      // jestem niepotrzebny :C
   } else {
      const int from = 1LL * m * (my - 1) / workers1 + 1,
                  to = 1LL * m * my / workers1;
      Int h(0);
      REP(x,from,to) {
         h = h * Q + Int(SignalAt(x));
      }
      h.sendTo(0);

      if (my == 1) {
         Int hashP = 0;
         REP(worker,1,workers1) {
            Int hFromWorker = receiveFrom(worker - 1);
            const int len = 1LL * m * worker / workers1 - 1LL * m * (worker - 1) / workers1;
            hashP = hashP * Qpow(len) + hFromWorker;
         }
         // hashP gotowy
         REP(worker,1,allWorkers) {
            hashP.sendTo(worker - 1);
         }
      }
   }
   Int hashP = receiveFrom(0);

   // faza 2 - wylicz hasze blokow tekstu
   const int textWorkers = min(allWorkers, n);
   if (my > textWorkers) {
      // jestem niepotrzebny :C
      return 0;
   }
   // 1 <= my <= textWorkers

   Int hashOfTextBlock[1111];
   const int fromText = 1LL * n * (my - 1) / textWorkers + 1,
               toText = 1LL * n * my / textWorkers;
   Int hBlock(0);
   REP(x,fromText,toText) {
      //cache[x - fromText] = SeqAt(x);
      hBlock = hBlock * Q + Int(SeqAt(x));
   }
   REP(worker,1,textWorkers) hBlock.sendTo(worker - 1);
   REP(worker,1,textWorkers) hashOfTextBlock[worker] = receiveFrom(worker - 1);

   // faza 3 - liczenie
   const int workers3 = min(allWorkers, n-m+1);
   if (my > workers3) {
      // jestem niepotrzebny :C
      return 0;
   }
   // 1 <= my <= workers3

   const int from = 1LL * (n-m+1) * (my - 1) / workers3,
               to = 1LL * (n-m+1) * my / workers3 - 1;
   // licze hasz dla offsetu from, czyli Text[from + 1 .. from + m]
   Int h(0);
   REP(block,1,textWorkers) {
      const int blockFrom = 1LL * n * (block - 1) / textWorkers + 1,
                  blockTo = 1LL * n * block / textWorkers;
      if (blockTo < from+1) continue;
      if (from+m < blockFrom) break;
      if (from+1 <= blockFrom && blockTo <= from+m) {
         // caly lezy w srodku
         h = h * Qpow(blockTo - blockFrom + 1) + hashOfTextBlock[block];
      } else {
         // czesciowy match
         const int a = max(from+1, blockFrom), b = min(from+m, blockTo);
         REP(x,a,b) {
            h = h * Q + Int(SeqAt(x));
         }
      }
   }
   int matches = 0;
   REP(offset,from,to) {
      // Text[offset + 1, offset + m]
      if (h == hashP) ++matches;
      if (offset < to) {
         // offset -> offset + 1
         h = h * Q + Int(SeqAt(offset+1 + m)) - Int(SeqAt(offset+1)) * Qpow(m);
      }
   }

   PutInt(0, matches);
   Send(0);

   if (my == 1) {
      int totalMatches = 0;
      REP(worker,1,workers3) {
         int source = Receive(-1);
         totalMatches += GetInt(source);
      }
      cout << totalMatches << endl;

      /*if (n <= 100 && m <= 100) {
         cerr << "n = " << n << ", m = " << m << endl;
         cerr << "text = ";
         REP(x,1,n) cerr << SeqAt(x) << " ";
         cerr << "\n";
         cerr << "pattern = ";
         REP(x,1,m) cerr << SignalAt(x) << " ";
         cerr << "\n";
      }*/

   }
   return 0;
}