#include <algorithm> #include <iostream> #include <cassert> #include <cstdint> #include <limits> #include <vector> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #include <deque> // #define MYDEBUG #include "poszukiwania.h" #include "message.h" #ifdef MYDEBUG #define AND_FALSE #else #define AND_FALSE && false #endif #define CHECK(cond) do \ if (!(cond) AND_FALSE) { \ cerr << "CHECK failed in line " << __LINE__ << ": " << #cond << endl; \ exit(1); \ } \ while (false) using namespace std; using i64 = int64_t; namespace std { template <typename T> ostream& operator<< (ostream& out, const vector<T>& v) { out << "{"; const char* sep = ""; for (const auto& x : v) { out << sep << x; sep = ", "; } out << "}"; return out; } } inline i64 s(i64 i) { return SignalAt(i + 1); } inline i64 m(i64 i) { return SeqAt(i + 1); } struct RollingHash { static const i64 mod = 2147483647ll; static const i64 base = 1583458089ll; // static const i64 mod = 16381ll; // static const i64 base = 572ll; void Add(i64 new_value) { base_pow = (base_pow * base) % mod; hash = ((hash * base) + new_value) % mod; CHECK(hash >= 0); // cout << "Add(" << new_value << ") -> " << hash << endl; ++window_length; } void Shift(i64 old_value, i64 new_value) { hash = ((hash * base) + new_value + mod - ((base_pow * old_value) % mod)) % mod; // cout << "Shift(" << old_value << ", " << new_value << ") -> " << hash << endl; CHECK(hash >= 0); } void Join(const RollingHash& rhs) { hash = (hash * rhs.base_pow + rhs.hash) % mod; window_length += rhs.window_length; base_pow = (base_pow * rhs.base_pow) % mod; } void Put(int node) const { PutLL(node, window_length); PutLL(node, hash); PutLL(node, base_pow); PutInt(node, start_idx); PutInt(node, end_idx); } void Get(int node) { window_length = GetLL(node); hash = GetLL(node); base_pow = GetLL(node); start_idx = GetInt(node); end_idx = GetInt(node); } i64 window_length = 0; i64 hash = 0; i64 base_pow = 1; int start_idx = -1; int end_idx = -1; }; int main() { // cout << "SeqLength: " << SeqLength() << endl; // cout << "SignalLength: " << SignalLength() << endl; const i64 S = SignalLength(); const i64 M = SeqLength(); { vector<int> points; for (int node = 0; node < NumberOfNodes(); node++) { i64 start_pos = node * (M - S) / NumberOfNodes(); points.push_back(start_pos); points.push_back(start_pos + S); } sort(points.begin(), points.end()); // cout << "points: " << points << endl; vector<RollingHash> hashes; for (int interval = 0; interval < points.size() - 1; ++interval) { if (interval % NumberOfNodes() == MyNodeId()) { RollingHash hash; for (int i = points[interval]; i < points[interval+1]; ++i) { hash.Add(m(i)); } hash.start_idx = points[interval]; hash.end_idx = points[interval+1]; hashes.push_back(hash); } } PutInt(0, hashes.size()); for (const RollingHash& h : hashes) { h.Put(0); } Send(0); } if (MyNodeId() == 0) { vector<RollingHash> hashes; for (int node = 0; node < NumberOfNodes(); ++node) { Receive(node); int n = GetInt(node); for (int i = 0; i < n; ++i) { RollingHash h; h.Get(node); hashes.push_back(h); } } sort(hashes.begin(), hashes.end(), [](const RollingHash& lhs, const RollingHash& rhs) { return lhs.start_idx < rhs.start_idx; }); /* for (const RollingHash& h : hashes) { cout << h.start_idx << "->" << h.end_idx << endl; } */ { for (int node = 0; node < NumberOfNodes(); node++) { i64 start_pos = node * (M - S) / NumberOfNodes(); RollingHash initial_m_hash; for (const RollingHash& h : hashes) { if (h.start_idx >= start_pos && h.end_idx <= start_pos + S) { initial_m_hash.Join(h); if (initial_m_hash.start_idx < 0) initial_m_hash.start_idx = h.start_idx; initial_m_hash.end_idx = h.end_idx; } } // cout << "Start hash for " << node << " is " << initial_m_hash.hash << endl; initial_m_hash.Put(node); Send(node); } } } Receive(0); RollingHash m_hash; m_hash.Get(0); // Calculate hash for S. { long long s_start = MyNodeId() * S / NumberOfNodes(); long long s_end = (MyNodeId() + 1) * S / NumberOfNodes(); //cout << s_start << "..." << s_end << endl; RollingHash s_hash_part; for (int i = s_start; i < s_end; ++i) { s_hash_part.Add(s(i)); } s_hash_part.Put(0); } Send(0); if (MyNodeId() == 0) { RollingHash s_hash; //cout << "s_hash:" << s_hash.hash << endl; for (int node = 0; node < NumberOfNodes(); ++node) { Receive(node); RollingHash s_part; s_part.Get(node); s_hash.Join(s_part); //cout << "s_hash:" << s_hash.hash << endl; } for (int node = 0; node < NumberOfNodes(); ++node) { s_hash.Put(node); Send(node); } } Receive(0); RollingHash to_find; to_find.Get(0); //cout << "to_find: " << to_find.hash << endl; i64 counter = 0; i64 start_pos = MyNodeId() * (M - S) / NumberOfNodes() + S; /* cout << "start_pos-S: " << start_pos-S << " start_pos: " << start_pos << " m_hash.start_idx:" << m_hash.start_idx << " m_hash.end_idx:" << m_hash.end_idx << endl; */ i64 end_pos = (MyNodeId() + 1) * (M - S) / NumberOfNodes() + S; if (MyNodeId() == 0 && m_hash.hash == to_find.hash) { // cout << "found at " << start_pos << endl; ++counter; } for (int i = start_pos; i < end_pos; ++i) { m_hash.Shift(m(i-S), m(i)); // cout << "hash: " << hash.hash << endl; if (m_hash.hash == to_find.hash) { // cout << "found at " << i - S + 1 << endl; ++counter; } } PutLL(0, counter); Send(0); //cout << counter << endl; if (MyNodeId() == 0) { i64 sum = 0; for (int node = 0; node < NumberOfNodes(); ++node) { Receive(node); sum += GetLL(node); } cout << sum << endl; } }
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 | #include <algorithm> #include <iostream> #include <cassert> #include <cstdint> #include <limits> #include <vector> #include <map> #include <set> #include <unordered_set> #include <unordered_map> #include <deque> // #define MYDEBUG #include "poszukiwania.h" #include "message.h" #ifdef MYDEBUG #define AND_FALSE #else #define AND_FALSE && false #endif #define CHECK(cond) do \ if (!(cond) AND_FALSE) { \ cerr << "CHECK failed in line " << __LINE__ << ": " << #cond << endl; \ exit(1); \ } \ while (false) using namespace std; using i64 = int64_t; namespace std { template <typename T> ostream& operator<< (ostream& out, const vector<T>& v) { out << "{"; const char* sep = ""; for (const auto& x : v) { out << sep << x; sep = ", "; } out << "}"; return out; } } inline i64 s(i64 i) { return SignalAt(i + 1); } inline i64 m(i64 i) { return SeqAt(i + 1); } struct RollingHash { static const i64 mod = 2147483647ll; static const i64 base = 1583458089ll; // static const i64 mod = 16381ll; // static const i64 base = 572ll; void Add(i64 new_value) { base_pow = (base_pow * base) % mod; hash = ((hash * base) + new_value) % mod; CHECK(hash >= 0); // cout << "Add(" << new_value << ") -> " << hash << endl; ++window_length; } void Shift(i64 old_value, i64 new_value) { hash = ((hash * base) + new_value + mod - ((base_pow * old_value) % mod)) % mod; // cout << "Shift(" << old_value << ", " << new_value << ") -> " << hash << endl; CHECK(hash >= 0); } void Join(const RollingHash& rhs) { hash = (hash * rhs.base_pow + rhs.hash) % mod; window_length += rhs.window_length; base_pow = (base_pow * rhs.base_pow) % mod; } void Put(int node) const { PutLL(node, window_length); PutLL(node, hash); PutLL(node, base_pow); PutInt(node, start_idx); PutInt(node, end_idx); } void Get(int node) { window_length = GetLL(node); hash = GetLL(node); base_pow = GetLL(node); start_idx = GetInt(node); end_idx = GetInt(node); } i64 window_length = 0; i64 hash = 0; i64 base_pow = 1; int start_idx = -1; int end_idx = -1; }; int main() { // cout << "SeqLength: " << SeqLength() << endl; // cout << "SignalLength: " << SignalLength() << endl; const i64 S = SignalLength(); const i64 M = SeqLength(); { vector<int> points; for (int node = 0; node < NumberOfNodes(); node++) { i64 start_pos = node * (M - S) / NumberOfNodes(); points.push_back(start_pos); points.push_back(start_pos + S); } sort(points.begin(), points.end()); // cout << "points: " << points << endl; vector<RollingHash> hashes; for (int interval = 0; interval < points.size() - 1; ++interval) { if (interval % NumberOfNodes() == MyNodeId()) { RollingHash hash; for (int i = points[interval]; i < points[interval+1]; ++i) { hash.Add(m(i)); } hash.start_idx = points[interval]; hash.end_idx = points[interval+1]; hashes.push_back(hash); } } PutInt(0, hashes.size()); for (const RollingHash& h : hashes) { h.Put(0); } Send(0); } if (MyNodeId() == 0) { vector<RollingHash> hashes; for (int node = 0; node < NumberOfNodes(); ++node) { Receive(node); int n = GetInt(node); for (int i = 0; i < n; ++i) { RollingHash h; h.Get(node); hashes.push_back(h); } } sort(hashes.begin(), hashes.end(), [](const RollingHash& lhs, const RollingHash& rhs) { return lhs.start_idx < rhs.start_idx; }); /* for (const RollingHash& h : hashes) { cout << h.start_idx << "->" << h.end_idx << endl; } */ { for (int node = 0; node < NumberOfNodes(); node++) { i64 start_pos = node * (M - S) / NumberOfNodes(); RollingHash initial_m_hash; for (const RollingHash& h : hashes) { if (h.start_idx >= start_pos && h.end_idx <= start_pos + S) { initial_m_hash.Join(h); if (initial_m_hash.start_idx < 0) initial_m_hash.start_idx = h.start_idx; initial_m_hash.end_idx = h.end_idx; } } // cout << "Start hash for " << node << " is " << initial_m_hash.hash << endl; initial_m_hash.Put(node); Send(node); } } } Receive(0); RollingHash m_hash; m_hash.Get(0); // Calculate hash for S. { long long s_start = MyNodeId() * S / NumberOfNodes(); long long s_end = (MyNodeId() + 1) * S / NumberOfNodes(); //cout << s_start << "..." << s_end << endl; RollingHash s_hash_part; for (int i = s_start; i < s_end; ++i) { s_hash_part.Add(s(i)); } s_hash_part.Put(0); } Send(0); if (MyNodeId() == 0) { RollingHash s_hash; //cout << "s_hash:" << s_hash.hash << endl; for (int node = 0; node < NumberOfNodes(); ++node) { Receive(node); RollingHash s_part; s_part.Get(node); s_hash.Join(s_part); //cout << "s_hash:" << s_hash.hash << endl; } for (int node = 0; node < NumberOfNodes(); ++node) { s_hash.Put(node); Send(node); } } Receive(0); RollingHash to_find; to_find.Get(0); //cout << "to_find: " << to_find.hash << endl; i64 counter = 0; i64 start_pos = MyNodeId() * (M - S) / NumberOfNodes() + S; /* cout << "start_pos-S: " << start_pos-S << " start_pos: " << start_pos << " m_hash.start_idx:" << m_hash.start_idx << " m_hash.end_idx:" << m_hash.end_idx << endl; */ i64 end_pos = (MyNodeId() + 1) * (M - S) / NumberOfNodes() + S; if (MyNodeId() == 0 && m_hash.hash == to_find.hash) { // cout << "found at " << start_pos << endl; ++counter; } for (int i = start_pos; i < end_pos; ++i) { m_hash.Shift(m(i-S), m(i)); // cout << "hash: " << hash.hash << endl; if (m_hash.hash == to_find.hash) { // cout << "found at " << i - S + 1 << endl; ++counter; } } PutLL(0, counter); Send(0); //cout << counter << endl; if (MyNodeId() == 0) { i64 sum = 0; for (int node = 0; node < NumberOfNodes(); ++node) { Receive(node); sum += GetLL(node); } cout << sum << endl; } } |