#include <bits/stdc++.h> #include "message.h" #include "poszukiwania.h" #define REP(i,x) for(uint32 i = 0 ; i < (x) ; i++) #define DEBUG_MODE #ifdef DEBUG_MODE #define print(x) cout << #x << " = " << x << endl #define debug(x) x #else #define print(x) #define debug(x) #endif #define hash_map unordered_map #define hash_multimap unordered_multimap #define hash_set unordered_set #define hash_multiset unordered_multiset using namespace std; typedef short int int16; typedef unsigned short int uint16; typedef int int32; typedef unsigned int uint32; typedef unsigned long long int64; typedef unsigned long long uint64; typedef pair <int, int> PII; typedef pair <uint32, uint32> PUU; typedef pair <uint32, int> PUI; typedef pair <int, uint32> PIU; typedef pair <int64, int64> PLL; typedef pair <int64, int> PLI; typedef pair <int, int64> PIL; typedef pair <int, int16> PIS; typedef pair <int16, int> PSI; static const PLL base = PUU(11, 37); static const PUU mod = PUU(0xFFFFFFFB, 0xFFFFFFBF); uint64 FastPower(uint64 number, uint64 power, uint32 MOD) // to check { uint64 result = 1; uint64 value = number; while( power > 0 ) { if(power & 1) { result *= value; result %= MOD; } value *= value; value %= MOD; power /= 2; } // uint64 result = 1; // // for(uint64 i = 0; i < power; ++i) // { // result *= number; // result %= MOD; // } return result; } bool operator == (const PLL& a, const PLL& b) { return(a.first == b.first && a.second == b.second); } class Application { public: inline void Run(); private: // Methods inline void LoadData(); inline void Solve(); // Fields PLL pattern_hash_; PLL my_hash_; PLL my_bonus_; uint64 my_id_; uint64 instances_number_; uint64 result_; }; int main() { ios_base::sync_with_stdio(false); Application app; app.Run(); } inline void Application::LoadData() { instances_number_ = NumberOfNodes(); my_id_ = MyNodeId(); result_ = 0; } inline void Application::Solve() { if(SignalLength() < 1000) { if(MyNodeId() == 0) { pattern_hash_ = PLL(SignalAt(1), SignalAt(1)); for(uint32 i = 1; i < SignalLength(); ++i) { pattern_hash_.first = (pattern_hash_.first * base.first + SignalAt(i + 1)) % mod.first; pattern_hash_.second = (pattern_hash_.second * base.second + SignalAt(i + 1)) % mod.second; } // cout << "A" << pattern_hash_.first << " " << pattern_hash_.second << endl; } } else { uint64 pattern_begin = (my_id_ * SignalLength()) / instances_number_; uint64 pattern_end = ((my_id_ + 1) * SignalLength()) / instances_number_; uint64 size_ = pattern_end - pattern_begin; pattern_hash_ = PLL(SignalAt(pattern_begin + 1), SignalAt(pattern_begin + 1)); for(uint64 i = pattern_begin + 1; i < pattern_end; ++i) { pattern_hash_.first = (pattern_hash_.first * base.first + SignalAt(i + 1)) % mod.first; pattern_hash_.second = (pattern_hash_.second * base.second + SignalAt(i + 1)) % mod.second; } pattern_hash_.first = (pattern_hash_.first * FastPower(base.first, SignalLength() - pattern_end , mod.first)) % mod.first; pattern_hash_.second = (pattern_hash_.second * FastPower(base.second, SignalLength() - pattern_end, mod.second)) % mod.second; if(my_id_ != 0) { PutLL(0, (long long) pattern_hash_.first); PutLL(0, (long long) pattern_hash_.second); Send(0); } else { for(uint32 i = 1; i < instances_number_; ++i) { Receive(i); pattern_hash_.first = (pattern_hash_.first + GetLL(i)) % mod.first; pattern_hash_.second = (pattern_hash_.second + GetLL(i)) % mod.second; } for(uint32 i = 1; i < instances_number_; ++i) { PutLL(i, (long long) pattern_hash_.first); PutLL(i, (long long) pattern_hash_.second); Send(i); } } if(my_id_ != 0) { Receive(0); pattern_hash_.first = GetLL(0); pattern_hash_.second = GetLL(0); } else { print(pattern_hash_.first); print(pattern_hash_.second); } } //if(SeqLength() < 1000) { if(MyNodeId() == 0) { vector <PLL> hashes (SeqLength()); hashes[0] = PLL(SeqAt(1), SeqAt(1)); for(uint32 i = 1; i < SeqLength(); ++i) { hashes[i].first = (hashes[i - 1].first * base.first + SeqAt(i + 1)) % mod.first; hashes[i].second = (hashes[i - 1].second * base.second + SeqAt(i + 1))% mod.second; } uint32 begin = 1; uint32 end = SignalLength(); if(hashes[SignalLength() - 1] == pattern_hash_) ++ result_; PLL pow = PLL(FastPower(base.first, SignalLength(), mod.first), FastPower(base.second, SignalLength(), mod.second)); while(end < SeqLength()) { PUU th; th.first = (mod.first + hashes[end].first - ( ( hashes[begin - 1].first * pow.first ) % mod.first) ) % mod.first; th.second = (mod.second + hashes[end].second - ((hashes[begin - 1].second * pow.second) % mod.second) ) % mod.second; if(th == pattern_hash_) { // print(begin); ++ result_; } ++ begin; ++ end; } cout << result_ << "\n"; } } } inline void Application::Run() { LoadData(); Solve(); }
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 | #include <bits/stdc++.h> #include "message.h" #include "poszukiwania.h" #define REP(i,x) for(uint32 i = 0 ; i < (x) ; i++) #define DEBUG_MODE #ifdef DEBUG_MODE #define print(x) cout << #x << " = " << x << endl #define debug(x) x #else #define print(x) #define debug(x) #endif #define hash_map unordered_map #define hash_multimap unordered_multimap #define hash_set unordered_set #define hash_multiset unordered_multiset using namespace std; typedef short int int16; typedef unsigned short int uint16; typedef int int32; typedef unsigned int uint32; typedef unsigned long long int64; typedef unsigned long long uint64; typedef pair <int, int> PII; typedef pair <uint32, uint32> PUU; typedef pair <uint32, int> PUI; typedef pair <int, uint32> PIU; typedef pair <int64, int64> PLL; typedef pair <int64, int> PLI; typedef pair <int, int64> PIL; typedef pair <int, int16> PIS; typedef pair <int16, int> PSI; static const PLL base = PUU(11, 37); static const PUU mod = PUU(0xFFFFFFFB, 0xFFFFFFBF); uint64 FastPower(uint64 number, uint64 power, uint32 MOD) // to check { uint64 result = 1; uint64 value = number; while( power > 0 ) { if(power & 1) { result *= value; result %= MOD; } value *= value; value %= MOD; power /= 2; } // uint64 result = 1; // // for(uint64 i = 0; i < power; ++i) // { // result *= number; // result %= MOD; // } return result; } bool operator == (const PLL& a, const PLL& b) { return(a.first == b.first && a.second == b.second); } class Application { public: inline void Run(); private: // Methods inline void LoadData(); inline void Solve(); // Fields PLL pattern_hash_; PLL my_hash_; PLL my_bonus_; uint64 my_id_; uint64 instances_number_; uint64 result_; }; int main() { ios_base::sync_with_stdio(false); Application app; app.Run(); } inline void Application::LoadData() { instances_number_ = NumberOfNodes(); my_id_ = MyNodeId(); result_ = 0; } inline void Application::Solve() { if(SignalLength() < 1000) { if(MyNodeId() == 0) { pattern_hash_ = PLL(SignalAt(1), SignalAt(1)); for(uint32 i = 1; i < SignalLength(); ++i) { pattern_hash_.first = (pattern_hash_.first * base.first + SignalAt(i + 1)) % mod.first; pattern_hash_.second = (pattern_hash_.second * base.second + SignalAt(i + 1)) % mod.second; } // cout << "A" << pattern_hash_.first << " " << pattern_hash_.second << endl; } } else { uint64 pattern_begin = (my_id_ * SignalLength()) / instances_number_; uint64 pattern_end = ((my_id_ + 1) * SignalLength()) / instances_number_; uint64 size_ = pattern_end - pattern_begin; pattern_hash_ = PLL(SignalAt(pattern_begin + 1), SignalAt(pattern_begin + 1)); for(uint64 i = pattern_begin + 1; i < pattern_end; ++i) { pattern_hash_.first = (pattern_hash_.first * base.first + SignalAt(i + 1)) % mod.first; pattern_hash_.second = (pattern_hash_.second * base.second + SignalAt(i + 1)) % mod.second; } pattern_hash_.first = (pattern_hash_.first * FastPower(base.first, SignalLength() - pattern_end , mod.first)) % mod.first; pattern_hash_.second = (pattern_hash_.second * FastPower(base.second, SignalLength() - pattern_end, mod.second)) % mod.second; if(my_id_ != 0) { PutLL(0, (long long) pattern_hash_.first); PutLL(0, (long long) pattern_hash_.second); Send(0); } else { for(uint32 i = 1; i < instances_number_; ++i) { Receive(i); pattern_hash_.first = (pattern_hash_.first + GetLL(i)) % mod.first; pattern_hash_.second = (pattern_hash_.second + GetLL(i)) % mod.second; } for(uint32 i = 1; i < instances_number_; ++i) { PutLL(i, (long long) pattern_hash_.first); PutLL(i, (long long) pattern_hash_.second); Send(i); } } if(my_id_ != 0) { Receive(0); pattern_hash_.first = GetLL(0); pattern_hash_.second = GetLL(0); } else { print(pattern_hash_.first); print(pattern_hash_.second); } } //if(SeqLength() < 1000) { if(MyNodeId() == 0) { vector <PLL> hashes (SeqLength()); hashes[0] = PLL(SeqAt(1), SeqAt(1)); for(uint32 i = 1; i < SeqLength(); ++i) { hashes[i].first = (hashes[i - 1].first * base.first + SeqAt(i + 1)) % mod.first; hashes[i].second = (hashes[i - 1].second * base.second + SeqAt(i + 1))% mod.second; } uint32 begin = 1; uint32 end = SignalLength(); if(hashes[SignalLength() - 1] == pattern_hash_) ++ result_; PLL pow = PLL(FastPower(base.first, SignalLength(), mod.first), FastPower(base.second, SignalLength(), mod.second)); while(end < SeqLength()) { PUU th; th.first = (mod.first + hashes[end].first - ( ( hashes[begin - 1].first * pow.first ) % mod.first) ) % mod.first; th.second = (mod.second + hashes[end].second - ((hashes[begin - 1].second * pow.second) % mod.second) ) % mod.second; if(th == pattern_hash_) { // print(begin); ++ result_; } ++ begin; ++ end; } cout << result_ << "\n"; } } } inline void Application::Run() { LoadData(); Solve(); } |