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
#include "poszukiwania.h"
#include "message.h"

#include <algorithm>
#include <iostream>
#include <vector>
#include <functional>
#include <cstdlib>
using namespace std;

const int base = 1000000003, mod1 = 1000000007, mod2 = 1000000009;
typedef long long ll;
ll s, m;
int nodes, id;

int pow(int a, int b, int mod)
{
    if(b == 0) return 1;
    if(b & 1) return (ll)a * pow(a, b - 1, mod) % mod;
    return pow((ll)a * a % mod, b / 2, mod);
}

vector<pair<int, int>> getHash(ll length, function<ll(ll)> func, int &block, int &tail)
{
    block = (length + nodes - 1) / nodes;
    tail = block * nodes - length;
    int start = id * block + 1;
    ll h1 = 0, h2 = 0;
    for(int i = start; i < start + block; i++)
    {
	if(i <= length)
	{
	    h1 = (h1 * base + func(i)) % mod1;
	    h2 = (h2 * base + func(i)) % mod2;
	}
	else
	{
	    h1 = h1 * base % mod1;
	    h2 = h2 * base % mod2;
	}
    }
    for(int i = 0; i < nodes; i++)
    {
	if(i == id) continue;
	PutInt(i, h1);
	PutInt(i, h2);
	Send(i);
    }
    vector<pair<int, int>> ans(nodes);
    ans[id] = make_pair((int)h1, (int)h2);
    for(int i = 0; i < nodes; i++)
    {
	if(i == id) continue;
	Receive(i);
	ans[i].first = GetInt(i);
	ans[i].second = GetInt(i);
    }
    return ans;
}

pair<ll, ll> totalHash(const vector<pair<int, int>> &vec, int block)
{
    ll h1 = 0, h2 = 0;
    int a1 = pow(base, block, mod1), a2 = pow(base, block, mod2);
    for(auto p: vec)
    {
	h1 = (h1 * a1 + p.first) % mod1;
	h2 = (h2 * a2 + p.second) % mod2;
    }
    return make_pair(h1, h2);
}

void answer(int ans)
{
    if(id == 0)
    {
	for(int i = 1; i < nodes; i++)
	{
	    Receive(i);
	    ans += GetInt(i);
	}
	printf("%d\n", ans);
	exit(0);
    }
    PutInt(0, ans);
    Send(0);
    exit(0);
}

int main()
{
    s = SignalLength();
    m = SeqLength();
    nodes = NumberOfNodes();
    id = MyNodeId();
    int sb, mb, st, mt;
    auto vec = getHash(s, SignalAt, sb, st);
    auto h = totalHash(vec, sb);
    vec = getHash(m, SeqAt, mb, mt);
    int ans = 0, start = mb * id;
    if(start + s > m) answer(0);
    ll h1 = 0, h2 = 0, length = 0;
    int a1 = pow(base, mb, mod1), a2 = pow(base, mb, mod2), A1 = pow(base, s - 1, mod1), A2 = pow(base, s - 1, mod2);
    int t1 = pow(base, st, mod1), t2 = pow(base, st, mod2);
    for(int i = id; ; i++)
    {
	if(length + mb > s) break;
	length += mb;
	h1 = (h1 * a1 + vec[i].first) % mod1;
	h2 = (h2 * a2 + vec[i].second) % mod2;
    }
    while(length < s)
    {
	auto seq = SeqAt(start + ++length);
	h1 = (h1 * base + seq) % mod1;
	h2 = (h2 * base + seq) % mod2;
    }
    for(int i = start + 1; i < start + mb + 1; i++)
    {
	if(h == make_pair(h1 * t1 % mod1, h2 * t2 % mod2))
	    ans++;
	int seq = SeqAt(i);
	h1 = (h1 - (ll)seq * A1) % mod1;
	h2 = (h2 - (ll)seq * A2) % mod2;
	if(h1 < 0) h1 += mod1;
	if(h2 < 0) h2 += mod2;
	if(i + s > m) break;
	seq = SeqAt(i + s);
	h1 = (h1 * base + seq) % mod1;
	h2 = (h2 * base + seq) % mod2;
    }
    answer(ans);
}