#include <bits/stdc++.h> using namespace std; #define PB push_back #define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i) #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) #define FI first #define SE second typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ll, ll> pll; const ll MOD = 1e9 + 7; #include "message.h" #include "poszukiwania.h" const ll HASH_BASES[] = { 1003452347, 1003452371 }; const int HLEN = sizeof(HASH_BASES) / sizeof(HASH_BASES[0]); const ll MAX_NODES = 100; ll powmod(ll a, ll b) { ll r = 1; while (b > 0) { if (b % 2 > 0) r = (r * a) % MOD; a = (a * a) % MOD; b /= 2; } return r; } struct Hash { ll values[HLEN]; int len; Hash() { REP(i, HLEN) { values[i] = 0; } len = 0; } void add(ll x) { ++len; REP (i, HLEN) { values[i] = ((values[i] * HASH_BASES[i]) + x) % MOD; } } void remove(ll x) { --len; REP (i, HLEN) { values[i] = ((values[i] - (x * powmod(HASH_BASES[i], len)) % MOD) + MOD) % MOD; } } void add(const Hash &other) { len += other.len; REP (i, HLEN) { values[i] = ((values[i] * powmod(HASH_BASES[i], other.len)) % MOD + other.values[i]) % MOD; } } void send(ll node) { REP (i, HLEN) { PutLL(node, values[i]); } PutInt(node, len); Send(node); } void receive(ll node) { Receive(node); REP (i, HLEN) { values[i] = GetLL(node); } len = GetInt(node); } bool eq(const Hash &other) const { if (len != other.len) { return false; } REP(i, HLEN) { if (values[i] != other.values[i]) { return false; } } return true; } }; ll plen, tlen; ll me = MyNodeId(); ll nodes_count = NumberOfNodes(); Hash hashes_of_pattern_parts[MAX_NODES]; Hash hashes_of_text_parts[MAX_NODES]; Hash pattern_hash; ll inline p(ll i) { return SignalAt(i); } ll inline t(ll i) { return SeqAt(i); } pll inline get_range(ll node, ll range_max) { ll a = range_max * node / nodes_count + 1; ll b = range_max * (node + 1) / nodes_count; return pll(a, b); } void compute_hash_of_pattern() { // ll a = plen * me / nodes_count + 1; // ll b = plen * (me + 1) / nodes_count; pll range = get_range(me, plen); // printf("0 pattern_parts: me=%lld range=%lld..%lld\n", me, range.FI, range.SE); FOR(i, range.FI, range.SE) { hashes_of_pattern_parts[me].add(p(i)); } REP (node, nodes_count) { if (node != me) { hashes_of_pattern_parts[me].send(node); } } REP (node, nodes_count) { if (node != me) { hashes_of_pattern_parts[node].receive(node); } // printf("1 pattern_parts: node=%lld hash=%lld\n", node, (ll) hashes_of_pattern_parts[node].values[0]); pattern_hash.add(hashes_of_pattern_parts[node]); } // printf("2 PATTERN_HASH=%lld %lld\n", pattern_hash.values[0], pattern_hash.values[1]); } void compute_hash_of_text_parts() { // ll a = tlen * me / nodes_count + 1; // ll b = tlen * (me + 1) / nodes_count; pll range = get_range(me, tlen); // printf("3 text_parts: me=%lld range=%lld..%lld\n", me, range.FI, range.SE); FOR(i, range.FI, range.SE) { // FOR(i, a, b) { hashes_of_text_parts[me].add(t(i)); } REP (node, nodes_count) { if (node != me) { hashes_of_text_parts[me].send(node); } } REP (node, nodes_count) { if (node != me) { hashes_of_text_parts[node].receive(node); } // printf("4 text_parts: node=%lld hash=%lld\n", node, (ll) hashes_of_text_parts[node].values[0]); } } ll inline which_part(ll x, ll max_x) { ll xx = x * nodes_count; return xx / max_x + (xx % max_x > 0 ? 1 : 0) - 1; } void count_occurences() { ll result = 0; ll max_start = tlen - plen + 1; // ll a = max_start * me / nodes_count + 1; // ll b = max_start * (me + 1) / nodes_count; pll range = get_range(me, max_start); // printf("5 occurences: me=%lld range=%lld..%lld\n", me, range.FI, range.SE); Hash hash; FOR (start, range.FI, range.SE) { ll end = start + plen - 1; // printf("6 occurences: me=%lld s=%lld e=%lld\n", me, start, end); if (start == range.FI) { ll start_part = which_part(start, tlen); ll end_part = which_part(end, tlen); if (start_part == end_part) { FOR (i, start, end) { hash.add(t(i)); } } else { // printf("7 occurences: me=%lld s=%lld e=%lld big hash %lld..%lld + ... + %lld..%lld\n", // me, start, end, // start, get_range(start_part, tlen).SE, // get_range(end_part, tlen).FI, end // ); FOR(i, start, get_range(start_part, tlen).SE) { hash.add(t(i)); } FOR(part, start_part + 1, end_part - 1) { hash.add(hashes_of_text_parts[part]); } FOR(i, get_range(end_part, tlen).FI, end) { hash.add(t(i)); } } } else { hash.remove(t(start - 1)); hash.add(t(end)); } if (hash.eq(pattern_hash)) { // printf("6 occurences: me=%lld s=%lld e=%lld <-- MATCH %lld %lld (%lld)\n", // me, start, end, hash.values[0], pattern_hash.values[0], hash.values[1]); ++result; } else { // printf("6 occurences: me=%lld s=%lld e=%lld <-- not %lld %lld\n", // me, start, end, hash.values[0], pattern_hash.values[0]); } } PutLL(0, result); Send(0); } void print_result() { if (me == 0) { ll result = 0; REP (node, nodes_count) { Receive(node); result += GetLL(node); } printf("%lld\n", result); } } void inline one() { plen = SignalLength(); tlen = SeqLength(); compute_hash_of_pattern(); compute_hash_of_text_parts(); count_occurences(); print_result(); } int main() { //int z; scanf("%d", &z); while(z--) one(); }
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i) #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) #define FI first #define SE second typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair <ll, ll> pll; const ll MOD = 1e9 + 7; #include "message.h" #include "poszukiwania.h" const ll HASH_BASES[] = { 1003452347, 1003452371 }; const int HLEN = sizeof(HASH_BASES) / sizeof(HASH_BASES[0]); const ll MAX_NODES = 100; ll powmod(ll a, ll b) { ll r = 1; while (b > 0) { if (b % 2 > 0) r = (r * a) % MOD; a = (a * a) % MOD; b /= 2; } return r; } struct Hash { ll values[HLEN]; int len; Hash() { REP(i, HLEN) { values[i] = 0; } len = 0; } void add(ll x) { ++len; REP (i, HLEN) { values[i] = ((values[i] * HASH_BASES[i]) + x) % MOD; } } void remove(ll x) { --len; REP (i, HLEN) { values[i] = ((values[i] - (x * powmod(HASH_BASES[i], len)) % MOD) + MOD) % MOD; } } void add(const Hash &other) { len += other.len; REP (i, HLEN) { values[i] = ((values[i] * powmod(HASH_BASES[i], other.len)) % MOD + other.values[i]) % MOD; } } void send(ll node) { REP (i, HLEN) { PutLL(node, values[i]); } PutInt(node, len); Send(node); } void receive(ll node) { Receive(node); REP (i, HLEN) { values[i] = GetLL(node); } len = GetInt(node); } bool eq(const Hash &other) const { if (len != other.len) { return false; } REP(i, HLEN) { if (values[i] != other.values[i]) { return false; } } return true; } }; ll plen, tlen; ll me = MyNodeId(); ll nodes_count = NumberOfNodes(); Hash hashes_of_pattern_parts[MAX_NODES]; Hash hashes_of_text_parts[MAX_NODES]; Hash pattern_hash; ll inline p(ll i) { return SignalAt(i); } ll inline t(ll i) { return SeqAt(i); } pll inline get_range(ll node, ll range_max) { ll a = range_max * node / nodes_count + 1; ll b = range_max * (node + 1) / nodes_count; return pll(a, b); } void compute_hash_of_pattern() { // ll a = plen * me / nodes_count + 1; // ll b = plen * (me + 1) / nodes_count; pll range = get_range(me, plen); // printf("0 pattern_parts: me=%lld range=%lld..%lld\n", me, range.FI, range.SE); FOR(i, range.FI, range.SE) { hashes_of_pattern_parts[me].add(p(i)); } REP (node, nodes_count) { if (node != me) { hashes_of_pattern_parts[me].send(node); } } REP (node, nodes_count) { if (node != me) { hashes_of_pattern_parts[node].receive(node); } // printf("1 pattern_parts: node=%lld hash=%lld\n", node, (ll) hashes_of_pattern_parts[node].values[0]); pattern_hash.add(hashes_of_pattern_parts[node]); } // printf("2 PATTERN_HASH=%lld %lld\n", pattern_hash.values[0], pattern_hash.values[1]); } void compute_hash_of_text_parts() { // ll a = tlen * me / nodes_count + 1; // ll b = tlen * (me + 1) / nodes_count; pll range = get_range(me, tlen); // printf("3 text_parts: me=%lld range=%lld..%lld\n", me, range.FI, range.SE); FOR(i, range.FI, range.SE) { // FOR(i, a, b) { hashes_of_text_parts[me].add(t(i)); } REP (node, nodes_count) { if (node != me) { hashes_of_text_parts[me].send(node); } } REP (node, nodes_count) { if (node != me) { hashes_of_text_parts[node].receive(node); } // printf("4 text_parts: node=%lld hash=%lld\n", node, (ll) hashes_of_text_parts[node].values[0]); } } ll inline which_part(ll x, ll max_x) { ll xx = x * nodes_count; return xx / max_x + (xx % max_x > 0 ? 1 : 0) - 1; } void count_occurences() { ll result = 0; ll max_start = tlen - plen + 1; // ll a = max_start * me / nodes_count + 1; // ll b = max_start * (me + 1) / nodes_count; pll range = get_range(me, max_start); // printf("5 occurences: me=%lld range=%lld..%lld\n", me, range.FI, range.SE); Hash hash; FOR (start, range.FI, range.SE) { ll end = start + plen - 1; // printf("6 occurences: me=%lld s=%lld e=%lld\n", me, start, end); if (start == range.FI) { ll start_part = which_part(start, tlen); ll end_part = which_part(end, tlen); if (start_part == end_part) { FOR (i, start, end) { hash.add(t(i)); } } else { // printf("7 occurences: me=%lld s=%lld e=%lld big hash %lld..%lld + ... + %lld..%lld\n", // me, start, end, // start, get_range(start_part, tlen).SE, // get_range(end_part, tlen).FI, end // ); FOR(i, start, get_range(start_part, tlen).SE) { hash.add(t(i)); } FOR(part, start_part + 1, end_part - 1) { hash.add(hashes_of_text_parts[part]); } FOR(i, get_range(end_part, tlen).FI, end) { hash.add(t(i)); } } } else { hash.remove(t(start - 1)); hash.add(t(end)); } if (hash.eq(pattern_hash)) { // printf("6 occurences: me=%lld s=%lld e=%lld <-- MATCH %lld %lld (%lld)\n", // me, start, end, hash.values[0], pattern_hash.values[0], hash.values[1]); ++result; } else { // printf("6 occurences: me=%lld s=%lld e=%lld <-- not %lld %lld\n", // me, start, end, hash.values[0], pattern_hash.values[0]); } } PutLL(0, result); Send(0); } void print_result() { if (me == 0) { ll result = 0; REP (node, nodes_count) { Receive(node); result += GetLL(node); } printf("%lld\n", result); } } void inline one() { plen = SignalLength(); tlen = SeqLength(); compute_hash_of_pattern(); compute_hash_of_text_parts(); count_occurences(); print_result(); } int main() { //int z; scanf("%d", &z); while(z--) one(); } |