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
#ifdef _MSC_VER
  #ifndef __GNUC__
    #pragma warning(disable: 4996)
  #endif
  #define main main0
#endif
#include <iostream>
#include "poszukiwania.h"
#include "message.h"
using namespace std;

static const unsigned long long MaxValue = 1000000001;
static const unsigned long long modulo  = 18446744025LL;

int main() {
  ios_base::sync_with_stdio(0);
  //cin.tie(NULL);

  long long m = SeqLength();
  long long n = SignalLength();
  long MyId = MyNodeId();
  long nodes = NumberOfNodes();
  long long current = (m - n) * MyId / nodes + 1;
  long long end = (m - n) * (MyId + 1) / nodes + 1;
  long long result = 0;
  long long hash_length = 1;
  unsigned long long signal_hash = SignalAt(1);
  unsigned long long seq_hash = SeqAt(current);
  unsigned long long power = 1;
  long length = n < 10000 ? static_cast<long>(n): 10000;

  while(signal_hash != seq_hash && current < end) {
      seq_hash = SeqAt(++current);        // przesun seq
  }

  while(hash_length < n && current < end) {
    if(signal_hash != seq_hash) {         // przesun seq
      seq_hash = ((seq_hash - SeqAt(current) * power) * MaxValue + SeqAt(current + hash_length)) % modulo;
      ++current;
    }
    else {                                // wydluz hash
      seq_hash = (seq_hash * MaxValue + SeqAt(current + hash_length)) % modulo;
      signal_hash = (signal_hash * MaxValue + SignalAt(++hash_length)) % modulo;
      power = (power * MaxValue) % modulo;
    }
  }

  while(current < end) {
    if(signal_hash == seq_hash) {         // bingo, no prawie, bo trzeba by porownac liczby
      bool good = true;
      for(long i = 0; i < length; ++i)
        if(SeqAt(current + i) != SignalAt(i + 1)) {
          good = false;
          break;
        }
      if(good)
        ++result;
    }
    seq_hash = ((seq_hash - SeqAt(current) * power) * MaxValue + SeqAt(current + hash_length)) % modulo;// przesun seq
    ++current;
  }

  if(MyId > 0) {
    PutLL(0, result);
    Send(0);
  }
  else {
    for(long receives = nodes - 1; receives > 0; --receives) {
      long instance = Receive(-1);
      result += GetLL(instance);
    }
    cout << result << endl;
  }

  return 0;
}