#include <cstdio> #include <utility> #include <vector> #include "message.h" #include "poszukiwania.h" typedef signed long long int sll; typedef unsigned long long int ull; // Renamed library functions: sll PatternLength() { return SignalLength(); } sll TextLength() { return SeqLength(); } sll Pattern(sll pos) { return SignalAt(pos + 1); } sll Text(sll pos) { return SeqAt(pos + 1); } // Information about nodes: // ------------------------ struct { int number_of_nodes; int my_id; int answering_node; void Init() { number_of_nodes = NumberOfNodes(); my_id = MyNodeId(); answering_node = number_of_nodes - 1; } } NodeInfo; // Strange Value: // -------------- class Value { public: static Value Zero() { return Value(0llu); } static Value One() { return Value(1llu); } static Value Base() { Value base; base.values_[0] = bases[0]; base.values_[1] = bases[1]; base.values_[2] = bases[2]; base.values_mod_[0] = bases[0]; base.values_mod_[1] = bases[1]; base.values_mod_[2] = bases[2]; return base; } Value() {} // Not necessarily is equal to Zero(). Value(ull value) : values_{value, value, value}, values_mod_{value, value, value} {} Value(const Value& value) = default; Value(Value&& value) = default; Value& operator=(const Value& value) = default; Value& operator=(Value&& value) = default; Value& operator+=(const Value& value) { values_[0] += value.values_[0]; values_[1] += value.values_[1]; values_[2] += value.values_[2]; values_mod_[0] = (values_mod_[0] + value.values_mod_[0]) % mod[0]; values_mod_[1] = (values_mod_[1] + value.values_mod_[1]) % mod[1]; values_mod_[2] = (values_mod_[2] + value.values_mod_[2]) % mod[2]; return *this; } Value& operator-=(const Value& value) { values_[0] -= value.values_[0]; values_[1] -= value.values_[1]; values_[2] -= value.values_[2]; values_mod_[0] = (values_mod_[0] - value.values_mod_[0] + mod[0]) % mod[0]; values_mod_[1] = (values_mod_[1] - value.values_mod_[1] + mod[1]) % mod[1]; values_mod_[2] = (values_mod_[2] - value.values_mod_[2] + mod[2]) % mod[2]; return *this; } Value& operator*=(const Value& value) { values_[0] *= value.values_[0]; values_[1] *= value.values_[1]; values_[2] *= value.values_[2]; values_mod_[0] = (values_mod_[0] * value.values_mod_[0]) % mod[0]; values_mod_[1] = (values_mod_[1] * value.values_mod_[1]) % mod[1]; values_mod_[2] = (values_mod_[2] * value.values_mod_[2]) % mod[2]; return *this; } Value& operator<<=(int shift_width) { *this *= BaseToPower(shift_width); return *this; } Value& append(ull value) { values_[0] = (values_[0] * bases[0]) + value; values_[1] = (values_[1] * bases[1]) + value; values_[2] = (values_[2] * bases[2]) + value; values_mod_[0] = ((values_mod_[0] * bases[0]) + value) % mod[0]; values_mod_[1] = ((values_mod_[1] * bases[1]) + value) % mod[1]; values_mod_[2] = ((values_mod_[2] * bases[2]) + value) % mod[2]; return *this; } Value operator*(const Value& value) const { return Value(*this) *= value; } static Value BaseToPower(int exponent) { Value b = Base(); Value result = One(); while (exponent > 0) { if (exponent % 2 == 1) { result *= b; } exponent /= 2; b *= b; } return result; } bool operator==(const Value& value) const { return values_[0] == value.values_[0] and values_[1] == value.values_[1] and values_[2] == value.values_[2] and values_mod_[0] == value.values_mod_[0] and values_mod_[1] == value.values_mod_[1] and values_mod_[2] == value.values_mod_[2]; } void SendTo(int to_whom) const { PutLL(to_whom, values_[0]); PutLL(to_whom, values_[1]); PutLL(to_whom, values_[2]); PutLL(to_whom, values_mod_[0]); PutLL(to_whom, values_mod_[1]); PutLL(to_whom, values_mod_[2]); Send(to_whom); } static Value ReceiveFrom(int from_whom) { Value value; Receive(from_whom); value.values_[0] = GetLL(from_whom); value.values_[1] = GetLL(from_whom); value.values_[2] = GetLL(from_whom); value.values_mod_[0] = GetLL(from_whom); value.values_mod_[1] = GetLL(from_whom); value.values_mod_[2] = GetLL(from_whom); return value; } private: static constexpr int bases[3] = {1000000223, 1000000007, 1000001011}; static constexpr int mod[3] = {1500064009, 1600066409, 1600069153}; ull values_[3]; ull values_mod_[3]; }; // Information about division of sequences: // ---------------------------------------- struct { sll pattern_length; sll text_length; std::vector<std::pair<sll, sll>> pattern_ranges; std::vector<std::pair<sll, sll>> text_ranges; std::vector<Value> text_hashes; void Init() { pattern_length = PatternLength(); text_length = TextLength(); auto divide_range = [](sll range_length, std::vector<std::pair<sll, sll>>& ranges) { const sll range_width = (range_length + NodeInfo.number_of_nodes - 1) / NodeInfo.number_of_nodes; for (int node_id = 0; node_id < NodeInfo.number_of_nodes; node_id++) { const sll left = std::min(node_id * range_width, range_length); const sll right = std::min((node_id + 1) * range_width - 1, range_length - 1); ranges.emplace_back(left, right); } }; divide_range(pattern_length, pattern_ranges); divide_range(text_length, text_ranges); } } Division; struct { void ComputeAndSend() { Value hash = Value::Zero(); for (int i = Division.pattern_ranges[NodeInfo.my_id].first; i <= Division.pattern_ranges[NodeInfo.my_id].second; i++) { hash.append(Pattern(i)); } for (int i = 0; i < NodeInfo.number_of_nodes; i++) { hash.SendTo(i); } } Value hash; void ReceiveAll() { hash = Value::Zero(); for (int i = 0; i < NodeInfo.number_of_nodes; i++) { Value next_hash = Value::ReceiveFrom(i); hash <<= (Division.pattern_ranges[i].second - Division.pattern_ranges[i].first + 1); hash += next_hash; } } } PatternHash; struct { void ComputeAndSend() { Value hash = Value::Zero(); for (int i = Division.text_ranges[NodeInfo.my_id].first; i <= Division.text_ranges[NodeInfo.my_id].second; i++) { hash.append(Text(i)); } for (int i = 0; i < NodeInfo.number_of_nodes; i++) { hash.SendTo(i); } } void ReceiveAll() { for (int i = 0; i < NodeInfo.number_of_nodes; i++) { Division.text_hashes.emplace_back(Value::ReceiveFrom(i)); } } } TextHash; void LookForOccurrencesOnRange(sll a, sll b, sll& result) { if (a > b) { return; } if (b + Division.pattern_length > Division.text_length) { LookForOccurrencesOnRange(a, Division.text_length - Division.pattern_length, result); return; } const int id = NodeInfo.my_id; Value hash = Value::Zero(); sll last = a + Division.pattern_length; for (sll i = a; i <= Division.text_ranges[id].second and i < last; i++) { hash.append(Text(i)); } int last_id = id; for (int i = id + 1; i < NodeInfo.number_of_nodes; i++) { if (Division.text_ranges[i].second >= last) { break; } last_id = i; hash <<= (Division.text_ranges[i].second - Division.text_ranges[i].first + 1); hash += Division.text_hashes[i]; } for (sll i = Division.text_ranges[last_id].second + 1; i < last; i++) { hash.append(Text(i)); } if (hash == PatternHash.hash) { result++; } const Value whole_power = Value::BaseToPower(Division.pattern_length - 1); for (sll i = a + 1; i <= b; i++) { hash -= whole_power * Value(Text(i - 1)); hash.append(Text(i + Division.pattern_length - 1)); if (hash == PatternHash.hash) { result++; } } } void LookForOccurrences() { sll result = 0ll; LookForOccurrencesOnRange(Division.text_ranges[NodeInfo.my_id].first, Division.text_ranges[NodeInfo.my_id].second, result); PutLL(NodeInfo.answering_node, result); Send(NodeInfo.answering_node); } void Answer() { sll result = 0ll; for (int i = 0; i < NodeInfo.number_of_nodes; i++) { Receive(i); result += GetLL(i); } printf("%lld\n", result); } int main() { NodeInfo.Init(); Division.Init(); PatternHash.ComputeAndSend(); TextHash.ComputeAndSend(); PatternHash.ReceiveAll(); TextHash.ReceiveAll(); LookForOccurrences(); if (NodeInfo.my_id == NodeInfo.answering_node) { Answer(); } return 0; }
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 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | #include <cstdio> #include <utility> #include <vector> #include "message.h" #include "poszukiwania.h" typedef signed long long int sll; typedef unsigned long long int ull; // Renamed library functions: sll PatternLength() { return SignalLength(); } sll TextLength() { return SeqLength(); } sll Pattern(sll pos) { return SignalAt(pos + 1); } sll Text(sll pos) { return SeqAt(pos + 1); } // Information about nodes: // ------------------------ struct { int number_of_nodes; int my_id; int answering_node; void Init() { number_of_nodes = NumberOfNodes(); my_id = MyNodeId(); answering_node = number_of_nodes - 1; } } NodeInfo; // Strange Value: // -------------- class Value { public: static Value Zero() { return Value(0llu); } static Value One() { return Value(1llu); } static Value Base() { Value base; base.values_[0] = bases[0]; base.values_[1] = bases[1]; base.values_[2] = bases[2]; base.values_mod_[0] = bases[0]; base.values_mod_[1] = bases[1]; base.values_mod_[2] = bases[2]; return base; } Value() {} // Not necessarily is equal to Zero(). Value(ull value) : values_{value, value, value}, values_mod_{value, value, value} {} Value(const Value& value) = default; Value(Value&& value) = default; Value& operator=(const Value& value) = default; Value& operator=(Value&& value) = default; Value& operator+=(const Value& value) { values_[0] += value.values_[0]; values_[1] += value.values_[1]; values_[2] += value.values_[2]; values_mod_[0] = (values_mod_[0] + value.values_mod_[0]) % mod[0]; values_mod_[1] = (values_mod_[1] + value.values_mod_[1]) % mod[1]; values_mod_[2] = (values_mod_[2] + value.values_mod_[2]) % mod[2]; return *this; } Value& operator-=(const Value& value) { values_[0] -= value.values_[0]; values_[1] -= value.values_[1]; values_[2] -= value.values_[2]; values_mod_[0] = (values_mod_[0] - value.values_mod_[0] + mod[0]) % mod[0]; values_mod_[1] = (values_mod_[1] - value.values_mod_[1] + mod[1]) % mod[1]; values_mod_[2] = (values_mod_[2] - value.values_mod_[2] + mod[2]) % mod[2]; return *this; } Value& operator*=(const Value& value) { values_[0] *= value.values_[0]; values_[1] *= value.values_[1]; values_[2] *= value.values_[2]; values_mod_[0] = (values_mod_[0] * value.values_mod_[0]) % mod[0]; values_mod_[1] = (values_mod_[1] * value.values_mod_[1]) % mod[1]; values_mod_[2] = (values_mod_[2] * value.values_mod_[2]) % mod[2]; return *this; } Value& operator<<=(int shift_width) { *this *= BaseToPower(shift_width); return *this; } Value& append(ull value) { values_[0] = (values_[0] * bases[0]) + value; values_[1] = (values_[1] * bases[1]) + value; values_[2] = (values_[2] * bases[2]) + value; values_mod_[0] = ((values_mod_[0] * bases[0]) + value) % mod[0]; values_mod_[1] = ((values_mod_[1] * bases[1]) + value) % mod[1]; values_mod_[2] = ((values_mod_[2] * bases[2]) + value) % mod[2]; return *this; } Value operator*(const Value& value) const { return Value(*this) *= value; } static Value BaseToPower(int exponent) { Value b = Base(); Value result = One(); while (exponent > 0) { if (exponent % 2 == 1) { result *= b; } exponent /= 2; b *= b; } return result; } bool operator==(const Value& value) const { return values_[0] == value.values_[0] and values_[1] == value.values_[1] and values_[2] == value.values_[2] and values_mod_[0] == value.values_mod_[0] and values_mod_[1] == value.values_mod_[1] and values_mod_[2] == value.values_mod_[2]; } void SendTo(int to_whom) const { PutLL(to_whom, values_[0]); PutLL(to_whom, values_[1]); PutLL(to_whom, values_[2]); PutLL(to_whom, values_mod_[0]); PutLL(to_whom, values_mod_[1]); PutLL(to_whom, values_mod_[2]); Send(to_whom); } static Value ReceiveFrom(int from_whom) { Value value; Receive(from_whom); value.values_[0] = GetLL(from_whom); value.values_[1] = GetLL(from_whom); value.values_[2] = GetLL(from_whom); value.values_mod_[0] = GetLL(from_whom); value.values_mod_[1] = GetLL(from_whom); value.values_mod_[2] = GetLL(from_whom); return value; } private: static constexpr int bases[3] = {1000000223, 1000000007, 1000001011}; static constexpr int mod[3] = {1500064009, 1600066409, 1600069153}; ull values_[3]; ull values_mod_[3]; }; // Information about division of sequences: // ---------------------------------------- struct { sll pattern_length; sll text_length; std::vector<std::pair<sll, sll>> pattern_ranges; std::vector<std::pair<sll, sll>> text_ranges; std::vector<Value> text_hashes; void Init() { pattern_length = PatternLength(); text_length = TextLength(); auto divide_range = [](sll range_length, std::vector<std::pair<sll, sll>>& ranges) { const sll range_width = (range_length + NodeInfo.number_of_nodes - 1) / NodeInfo.number_of_nodes; for (int node_id = 0; node_id < NodeInfo.number_of_nodes; node_id++) { const sll left = std::min(node_id * range_width, range_length); const sll right = std::min((node_id + 1) * range_width - 1, range_length - 1); ranges.emplace_back(left, right); } }; divide_range(pattern_length, pattern_ranges); divide_range(text_length, text_ranges); } } Division; struct { void ComputeAndSend() { Value hash = Value::Zero(); for (int i = Division.pattern_ranges[NodeInfo.my_id].first; i <= Division.pattern_ranges[NodeInfo.my_id].second; i++) { hash.append(Pattern(i)); } for (int i = 0; i < NodeInfo.number_of_nodes; i++) { hash.SendTo(i); } } Value hash; void ReceiveAll() { hash = Value::Zero(); for (int i = 0; i < NodeInfo.number_of_nodes; i++) { Value next_hash = Value::ReceiveFrom(i); hash <<= (Division.pattern_ranges[i].second - Division.pattern_ranges[i].first + 1); hash += next_hash; } } } PatternHash; struct { void ComputeAndSend() { Value hash = Value::Zero(); for (int i = Division.text_ranges[NodeInfo.my_id].first; i <= Division.text_ranges[NodeInfo.my_id].second; i++) { hash.append(Text(i)); } for (int i = 0; i < NodeInfo.number_of_nodes; i++) { hash.SendTo(i); } } void ReceiveAll() { for (int i = 0; i < NodeInfo.number_of_nodes; i++) { Division.text_hashes.emplace_back(Value::ReceiveFrom(i)); } } } TextHash; void LookForOccurrencesOnRange(sll a, sll b, sll& result) { if (a > b) { return; } if (b + Division.pattern_length > Division.text_length) { LookForOccurrencesOnRange(a, Division.text_length - Division.pattern_length, result); return; } const int id = NodeInfo.my_id; Value hash = Value::Zero(); sll last = a + Division.pattern_length; for (sll i = a; i <= Division.text_ranges[id].second and i < last; i++) { hash.append(Text(i)); } int last_id = id; for (int i = id + 1; i < NodeInfo.number_of_nodes; i++) { if (Division.text_ranges[i].second >= last) { break; } last_id = i; hash <<= (Division.text_ranges[i].second - Division.text_ranges[i].first + 1); hash += Division.text_hashes[i]; } for (sll i = Division.text_ranges[last_id].second + 1; i < last; i++) { hash.append(Text(i)); } if (hash == PatternHash.hash) { result++; } const Value whole_power = Value::BaseToPower(Division.pattern_length - 1); for (sll i = a + 1; i <= b; i++) { hash -= whole_power * Value(Text(i - 1)); hash.append(Text(i + Division.pattern_length - 1)); if (hash == PatternHash.hash) { result++; } } } void LookForOccurrences() { sll result = 0ll; LookForOccurrencesOnRange(Division.text_ranges[NodeInfo.my_id].first, Division.text_ranges[NodeInfo.my_id].second, result); PutLL(NodeInfo.answering_node, result); Send(NodeInfo.answering_node); } void Answer() { sll result = 0ll; for (int i = 0; i < NodeInfo.number_of_nodes; i++) { Receive(i); result += GetLL(i); } printf("%lld\n", result); } int main() { NodeInfo.Init(); Division.Init(); PatternHash.ComputeAndSend(); TextHash.ComputeAndSend(); PatternHash.ReceiveAll(); TextHash.ReceiveAll(); LookForOccurrences(); if (NodeInfo.my_id == NodeInfo.answering_node) { Answer(); } return 0; } |