#include "palindromy.h" #include "message.h" #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int _n=n, i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; const int INF = 1000000000; const LL INFLL = LL(INF) * LL(INF); template<class T> inline int size(const T&x) { return x.size(); } LL NODE; LL NNODES; template<class T> void Put(int node, const T&x) { static_assert(is_pod<T>(), "Put pod"); const char *p = reinterpret_cast<const char*>(&x); for(size_t i=0;i<sizeof(T);++i) PutChar(node, p[i]); } template<class T> void Put(int node, const vector<T> &x) { PutLL(node, x.size()); for(const T&a : x) Put(node, a); } template<class T> void SendTo(int node, const T&x) { Put(node, x); Send(node); } template<class T> void Get(int node, T &x) { static_assert(is_pod<T>(), "Get pod"); char *p = reinterpret_cast<char*>(&x); for(size_t i=0;i<sizeof(T);++i) p[i] = GetChar(node); } template<class T> void Get(int node, vector<T> &x) { x.resize(GetLL(node)); for(T &a : x) Get(node, a); } template<class T> int ReceiveFrom(int node, T &x) { node = Receive(node); Get(node, x); return node; } ///////// template<class T> class Array { public: explicit Array() : a(0), b(0), v() {} explicit Array(long long _a, long long _b) : a(_a), b(_b), v(_b - _a) {} void resize(long long _a, long long _b) { a = _a; b = _b; v.assign(_b - _a, T()); } void assign(long long _a, long long _b, const T &x) { a = _a; b = _b; v.assign(_b - _a, x); } T &operator[](long long x) { return v[x-a]; } const T &operator[](long long x) const { return v[x-a]; } long long beginIdx() const { return a; } long long endIdx() const { return b; } typename vector<T>::const_iterator begin() const { return v.begin(); } typename vector<T>::iterator begin() { return v.begin(); } typename vector<T>::const_iterator end() const { return v.end(); } typename vector<T>::iterator end() { return v.end(); } private: long long a, b; vector<T> v; }; constexpr int MODULUS = std::numeric_limits<int>::max(); int ModPower(int a, LL b) { b %= MODULUS-1; if(b<0) b += (MODULUS-1); int res = 1; while(b) { if(b&1) res = (LL(res)*a)%MODULUS; b >>= 1; a = (LL(a)*a)%MODULUS; } return res; } const int NUM_BASES = 3; const int BASES[NUM_BASES] = { 1051994564, 404438832, 870807049}; struct Hash { int val[NUM_BASES]; LL len; Hash() : val(), len(0) {} void appendChar(char c) { REP(i,NUM_BASES) { val[i] = (LL(val[i]) * BASES[i] + LL(c)) % MODULUS; } ++len; } void appendHash(const Hash &h); void SubtractPrefix(const Hash &h); }; void Hash::appendHash(const Hash &h) { REP(i,NUM_BASES) { val[i] = (LL(val[i]) * ModPower(BASES[i], h.len) + h.val[i]) % MODULUS; } len += h.len; } void Hash::SubtractPrefix(const Hash &h) { len -= h.len; REP(i,NUM_BASES) { val[i] = ((LL(val[i]) - LL(h.val[i]) * ModPower(BASES[i], len)) % MODULUS + MODULUS) % MODULUS; } } inline bool operator==(const Hash &a, const Hash &b) { if(a.len != b.len) return false; REP(i,NUM_BASES) if(a.val[i] != b.val[i]) return false; return true; } constexpr LL BLOCK = 30000; static_assert(BLOCK%2==0, "even block"); constexpr LL MASTER = 0; LL N; LL numBlocks; LL myAlphaBlock; LL myBetaBlock; LL myAlpha; LL myBeta; LL longPalin; Array<char> data; Array<int> palinRadius; vector<Hash> prefixHashes; vector<Hash> revSuffixHashes; char ReadChar(LL x) { if (x < 0) return '^'; if (x >= N) return '$'; return GetLetter(x/2); } void Initialize() { NNODES = NumberOfNodes(); NODE = MyNodeId(); N = 2 * GetLength(); numBlocks = (N+BLOCK-1) / BLOCK; NNODES = min(NNODES, numBlocks); if (NODE>=NNODES) exit(0); myAlphaBlock = NODE * numBlocks / NNODES; myBetaBlock = (NODE+1) * numBlocks / NNODES; myAlpha = myAlphaBlock * BLOCK; myBeta = myBetaBlock * BLOCK; } void GetData() { longPalin = 2 * (myBeta - myAlpha); data.resize(myAlpha - longPalin, myBeta + longPalin); for (LL i = data.beginIdx(); i < data.endIdx(); i += 2) { data[i] = ReadChar(i); data[i+1] = data[i]; } } Hash ComputeLocalForwardHash(LL alpha, LL beta) { Hash h; for (LL i=alpha;i<beta;++i) h.appendChar(data[i]); return h; } Hash ComputeLocalBackwardHash(LL alpha, LL beta) { Hash h; for (LL i=beta-1;i>=alpha;--i) h.appendChar(data[i]); return h; } void SendOutMyHashes() { vector<Hash> forwardHashes; vector<Hash> backwardHashes; LL myBlocks = myBetaBlock - myAlphaBlock; forwardHashes.reserve(myBlocks); backwardHashes.reserve(myBlocks); for (LL block = myAlphaBlock; block < myBetaBlock; ++block) { forwardHashes.push_back(ComputeLocalForwardHash(block * BLOCK, (block+1) * BLOCK)); backwardHashes.push_back(ComputeLocalBackwardHash(block * BLOCK, (block+1) * BLOCK)); } assert(size(forwardHashes) == myBlocks); assert(size(backwardHashes) == myBlocks); REP(node, NNODES) { Put(node, myBlocks); for (const auto &h : forwardHashes) { assert(h.len == BLOCK); REP(i, NUM_BASES) Put(node, h.val[i]); } for (const auto &h : backwardHashes) { assert(h.len == BLOCK); REP(i, NUM_BASES) Put(node, h.val[i]); } Send(node); } } void ReceiveHashes() { prefixHashes.reserve(numBlocks+1); revSuffixHashes.reserve(numBlocks+1); prefixHashes.push_back(Hash()); REP(node, NNODES) { Receive(node); LL hisBlocks; Get(node, hisBlocks); for(LL i=0;i<hisBlocks;++i) { Hash h; h.len = BLOCK; REP(j, NUM_BASES) Get(node, h.val[j]); prefixHashes.push_back(h); } for(LL i=0;i<hisBlocks;++i) { Hash h; h.len = BLOCK; REP(j, NUM_BASES) Get(node, h.val[j]); revSuffixHashes.push_back(h); } } revSuffixHashes.push_back(Hash()); assert(size(prefixHashes) == numBlocks+1); assert(size(revSuffixHashes) == numBlocks+1); for (LL i = 1; i <= numBlocks; ++i) { Hash h = prefixHashes[i-1]; h.appendHash(prefixHashes[i]); prefixHashes[i] = h; } for (LL i = numBlocks-1; i>=0; --i) { Hash h = revSuffixHashes[i+1]; h.appendHash(revSuffixHashes[i]); revSuffixHashes[i] = h; } } void ComputeShortPalin() { palinRadius.resize(myAlpha - longPalin, myBeta); LL currentLongStart = palinRadius.beginIdx(); LL currentLongEnd = currentLongStart; for (LL x = myAlpha - longPalin; x < myBeta; ++x) { LL radius = 0; if (x < currentLongEnd) { radius = palinRadius[currentLongStart + currentLongEnd - x]; if (x + radius > currentLongEnd) radius = currentLongEnd - x; } while (x-radius > myAlpha - longPalin && x+radius < myBeta + longPalin && data[x-radius-1] == data[x+radius]) ++radius; palinRadius[x] = radius; if(x+radius > currentLongEnd) { currentLongStart = x - radius; currentLongEnd = x + radius; } } } Hash ComputeBigPrefix(LL x) { LL b = x/BLOCK; Hash h = prefixHashes[b]; for (LL i=b*BLOCK;i<x;++i) { h.appendChar(ReadChar(i)); } return h; } Hash ComputeBigRevSuffix(LL x) { LL b = (x+BLOCK-1)/BLOCK; Hash h = revSuffixHashes[b]; for (LL i = b*BLOCK-1; i>=x; --i) { h.appendChar(ReadChar(i)); } return h; } Hash ComputeBigHash(LL alpha, LL beta) { Hash h = ComputeBigPrefix(beta); h.SubtractPrefix(ComputeBigPrefix(alpha)); return h; } Hash ComputeBigRevHash(LL alpha, LL beta) { Hash h = ComputeBigRevSuffix(alpha); h.SubtractPrefix(ComputeBigRevSuffix(beta)); return h; } bool RadiusOK(LL x, LL radius) { if(x-radius < 0 || x+radius > numBlocks*BLOCK) return false; return ComputeBigHash(x, x+radius) == ComputeBigRevHash(x-radius, x); } LL ComputeBigRadius(LL x) { LL alpha = 0; LL beta = 1; while(RadiusOK(x, beta)) { alpha = beta; beta *= 2; } while(beta-alpha>1) { LL gamma = (alpha+beta)/2; if(RadiusOK(x, gamma)) alpha=gamma; else beta=gamma; } return alpha; } bool PeriodOK(const LL a, const LL b, const LL period) { assert(b-a >= period); if(a < 0 || b > numBlocks*BLOCK) return false; bool res= ComputeBigHash(a, b-period) == ComputeBigHash(a+period, b); return res; } void ExtendPeriodLeft(LL &a, const LL b, const LL period) { LL alpha = 0; LL beta = 1; while(PeriodOK(a-beta, b, period)) { alpha=beta; beta *= 2; } while(beta-alpha>1) { LL gamma = (alpha+beta)/2; if(PeriodOK(a-gamma, b, period)) alpha=gamma; else beta=gamma; } a -= alpha; } void ExtendPeriodRight(const LL a, LL &b, const LL period) { LL alpha = 0; LL beta = 1; while(PeriodOK(a, b+beta, period)) { alpha=beta; beta *= 2; } while(beta-alpha>1) { LL gamma = (alpha+beta)/2; if(PeriodOK(a, b+gamma, period)) alpha=gamma; else beta=gamma; } b += alpha; } void ComputeLongPalin() { LL numLong = 0; LL firstLong = 0; LL step = 0; LL lastLong = 0; for (LL x = myAlpha; x < myBeta; ++x) { if (palinRadius[x] < longPalin) continue; if (numLong==0) { firstLong = x; } else if (numLong==1) { step = x - firstLong; lastLong = x; } else { assert(step == x - lastLong); lastLong = x; } ++numLong; } if (numLong == 0) return; else if (numLong == 1) { palinRadius[firstLong] = ComputeBigRadius(firstLong); } else { LL period = 2 * step; LL periodStart = firstLong - longPalin; LL periodEnd = lastLong + longPalin; ExtendPeriodLeft(periodStart, periodEnd, period); ExtendPeriodRight(periodStart, periodEnd, period); for (LL x = firstLong; x <= lastLong; x += step) { if (x - periodStart < periodEnd - x) { palinRadius[x] = x-periodStart; } else if (x - periodStart > periodEnd - x) { palinRadius[x] = periodEnd-x; } else { palinRadius[x] = ComputeBigRadius(x); } } } } void SendAnswer() { LL answer = 0; for (LL x = myAlpha; x < myBeta && x < N; ++x) { // 0 -> 0 // 1 -> 1 a|a // 2 -> 1 aa|aa // 3 -> 2 aab|baa answer += (palinRadius[x]+1)/2; } SendTo(MASTER, answer); } void CollectAnswers() { LL total = 0; REP(node, NNODES) { LL x; ReceiveFrom(node, x); total += x; } cout << total << '\n'; } int main() { Initialize(); GetData(); SendOutMyHashes(); ReceiveHashes(); ComputeShortPalin(); ComputeLongPalin(); SendAnswer(); if (NODE == MASTER) CollectAnswers(); }
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | #include "palindromy.h" #include "message.h" #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int _n=n, i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; typedef long long LL; const int INF = 1000000000; const LL INFLL = LL(INF) * LL(INF); template<class T> inline int size(const T&x) { return x.size(); } LL NODE; LL NNODES; template<class T> void Put(int node, const T&x) { static_assert(is_pod<T>(), "Put pod"); const char *p = reinterpret_cast<const char*>(&x); for(size_t i=0;i<sizeof(T);++i) PutChar(node, p[i]); } template<class T> void Put(int node, const vector<T> &x) { PutLL(node, x.size()); for(const T&a : x) Put(node, a); } template<class T> void SendTo(int node, const T&x) { Put(node, x); Send(node); } template<class T> void Get(int node, T &x) { static_assert(is_pod<T>(), "Get pod"); char *p = reinterpret_cast<char*>(&x); for(size_t i=0;i<sizeof(T);++i) p[i] = GetChar(node); } template<class T> void Get(int node, vector<T> &x) { x.resize(GetLL(node)); for(T &a : x) Get(node, a); } template<class T> int ReceiveFrom(int node, T &x) { node = Receive(node); Get(node, x); return node; } ///////// template<class T> class Array { public: explicit Array() : a(0), b(0), v() {} explicit Array(long long _a, long long _b) : a(_a), b(_b), v(_b - _a) {} void resize(long long _a, long long _b) { a = _a; b = _b; v.assign(_b - _a, T()); } void assign(long long _a, long long _b, const T &x) { a = _a; b = _b; v.assign(_b - _a, x); } T &operator[](long long x) { return v[x-a]; } const T &operator[](long long x) const { return v[x-a]; } long long beginIdx() const { return a; } long long endIdx() const { return b; } typename vector<T>::const_iterator begin() const { return v.begin(); } typename vector<T>::iterator begin() { return v.begin(); } typename vector<T>::const_iterator end() const { return v.end(); } typename vector<T>::iterator end() { return v.end(); } private: long long a, b; vector<T> v; }; constexpr int MODULUS = std::numeric_limits<int>::max(); int ModPower(int a, LL b) { b %= MODULUS-1; if(b<0) b += (MODULUS-1); int res = 1; while(b) { if(b&1) res = (LL(res)*a)%MODULUS; b >>= 1; a = (LL(a)*a)%MODULUS; } return res; } const int NUM_BASES = 3; const int BASES[NUM_BASES] = { 1051994564, 404438832, 870807049}; struct Hash { int val[NUM_BASES]; LL len; Hash() : val(), len(0) {} void appendChar(char c) { REP(i,NUM_BASES) { val[i] = (LL(val[i]) * BASES[i] + LL(c)) % MODULUS; } ++len; } void appendHash(const Hash &h); void SubtractPrefix(const Hash &h); }; void Hash::appendHash(const Hash &h) { REP(i,NUM_BASES) { val[i] = (LL(val[i]) * ModPower(BASES[i], h.len) + h.val[i]) % MODULUS; } len += h.len; } void Hash::SubtractPrefix(const Hash &h) { len -= h.len; REP(i,NUM_BASES) { val[i] = ((LL(val[i]) - LL(h.val[i]) * ModPower(BASES[i], len)) % MODULUS + MODULUS) % MODULUS; } } inline bool operator==(const Hash &a, const Hash &b) { if(a.len != b.len) return false; REP(i,NUM_BASES) if(a.val[i] != b.val[i]) return false; return true; } constexpr LL BLOCK = 30000; static_assert(BLOCK%2==0, "even block"); constexpr LL MASTER = 0; LL N; LL numBlocks; LL myAlphaBlock; LL myBetaBlock; LL myAlpha; LL myBeta; LL longPalin; Array<char> data; Array<int> palinRadius; vector<Hash> prefixHashes; vector<Hash> revSuffixHashes; char ReadChar(LL x) { if (x < 0) return '^'; if (x >= N) return '$'; return GetLetter(x/2); } void Initialize() { NNODES = NumberOfNodes(); NODE = MyNodeId(); N = 2 * GetLength(); numBlocks = (N+BLOCK-1) / BLOCK; NNODES = min(NNODES, numBlocks); if (NODE>=NNODES) exit(0); myAlphaBlock = NODE * numBlocks / NNODES; myBetaBlock = (NODE+1) * numBlocks / NNODES; myAlpha = myAlphaBlock * BLOCK; myBeta = myBetaBlock * BLOCK; } void GetData() { longPalin = 2 * (myBeta - myAlpha); data.resize(myAlpha - longPalin, myBeta + longPalin); for (LL i = data.beginIdx(); i < data.endIdx(); i += 2) { data[i] = ReadChar(i); data[i+1] = data[i]; } } Hash ComputeLocalForwardHash(LL alpha, LL beta) { Hash h; for (LL i=alpha;i<beta;++i) h.appendChar(data[i]); return h; } Hash ComputeLocalBackwardHash(LL alpha, LL beta) { Hash h; for (LL i=beta-1;i>=alpha;--i) h.appendChar(data[i]); return h; } void SendOutMyHashes() { vector<Hash> forwardHashes; vector<Hash> backwardHashes; LL myBlocks = myBetaBlock - myAlphaBlock; forwardHashes.reserve(myBlocks); backwardHashes.reserve(myBlocks); for (LL block = myAlphaBlock; block < myBetaBlock; ++block) { forwardHashes.push_back(ComputeLocalForwardHash(block * BLOCK, (block+1) * BLOCK)); backwardHashes.push_back(ComputeLocalBackwardHash(block * BLOCK, (block+1) * BLOCK)); } assert(size(forwardHashes) == myBlocks); assert(size(backwardHashes) == myBlocks); REP(node, NNODES) { Put(node, myBlocks); for (const auto &h : forwardHashes) { assert(h.len == BLOCK); REP(i, NUM_BASES) Put(node, h.val[i]); } for (const auto &h : backwardHashes) { assert(h.len == BLOCK); REP(i, NUM_BASES) Put(node, h.val[i]); } Send(node); } } void ReceiveHashes() { prefixHashes.reserve(numBlocks+1); revSuffixHashes.reserve(numBlocks+1); prefixHashes.push_back(Hash()); REP(node, NNODES) { Receive(node); LL hisBlocks; Get(node, hisBlocks); for(LL i=0;i<hisBlocks;++i) { Hash h; h.len = BLOCK; REP(j, NUM_BASES) Get(node, h.val[j]); prefixHashes.push_back(h); } for(LL i=0;i<hisBlocks;++i) { Hash h; h.len = BLOCK; REP(j, NUM_BASES) Get(node, h.val[j]); revSuffixHashes.push_back(h); } } revSuffixHashes.push_back(Hash()); assert(size(prefixHashes) == numBlocks+1); assert(size(revSuffixHashes) == numBlocks+1); for (LL i = 1; i <= numBlocks; ++i) { Hash h = prefixHashes[i-1]; h.appendHash(prefixHashes[i]); prefixHashes[i] = h; } for (LL i = numBlocks-1; i>=0; --i) { Hash h = revSuffixHashes[i+1]; h.appendHash(revSuffixHashes[i]); revSuffixHashes[i] = h; } } void ComputeShortPalin() { palinRadius.resize(myAlpha - longPalin, myBeta); LL currentLongStart = palinRadius.beginIdx(); LL currentLongEnd = currentLongStart; for (LL x = myAlpha - longPalin; x < myBeta; ++x) { LL radius = 0; if (x < currentLongEnd) { radius = palinRadius[currentLongStart + currentLongEnd - x]; if (x + radius > currentLongEnd) radius = currentLongEnd - x; } while (x-radius > myAlpha - longPalin && x+radius < myBeta + longPalin && data[x-radius-1] == data[x+radius]) ++radius; palinRadius[x] = radius; if(x+radius > currentLongEnd) { currentLongStart = x - radius; currentLongEnd = x + radius; } } } Hash ComputeBigPrefix(LL x) { LL b = x/BLOCK; Hash h = prefixHashes[b]; for (LL i=b*BLOCK;i<x;++i) { h.appendChar(ReadChar(i)); } return h; } Hash ComputeBigRevSuffix(LL x) { LL b = (x+BLOCK-1)/BLOCK; Hash h = revSuffixHashes[b]; for (LL i = b*BLOCK-1; i>=x; --i) { h.appendChar(ReadChar(i)); } return h; } Hash ComputeBigHash(LL alpha, LL beta) { Hash h = ComputeBigPrefix(beta); h.SubtractPrefix(ComputeBigPrefix(alpha)); return h; } Hash ComputeBigRevHash(LL alpha, LL beta) { Hash h = ComputeBigRevSuffix(alpha); h.SubtractPrefix(ComputeBigRevSuffix(beta)); return h; } bool RadiusOK(LL x, LL radius) { if(x-radius < 0 || x+radius > numBlocks*BLOCK) return false; return ComputeBigHash(x, x+radius) == ComputeBigRevHash(x-radius, x); } LL ComputeBigRadius(LL x) { LL alpha = 0; LL beta = 1; while(RadiusOK(x, beta)) { alpha = beta; beta *= 2; } while(beta-alpha>1) { LL gamma = (alpha+beta)/2; if(RadiusOK(x, gamma)) alpha=gamma; else beta=gamma; } return alpha; } bool PeriodOK(const LL a, const LL b, const LL period) { assert(b-a >= period); if(a < 0 || b > numBlocks*BLOCK) return false; bool res= ComputeBigHash(a, b-period) == ComputeBigHash(a+period, b); return res; } void ExtendPeriodLeft(LL &a, const LL b, const LL period) { LL alpha = 0; LL beta = 1; while(PeriodOK(a-beta, b, period)) { alpha=beta; beta *= 2; } while(beta-alpha>1) { LL gamma = (alpha+beta)/2; if(PeriodOK(a-gamma, b, period)) alpha=gamma; else beta=gamma; } a -= alpha; } void ExtendPeriodRight(const LL a, LL &b, const LL period) { LL alpha = 0; LL beta = 1; while(PeriodOK(a, b+beta, period)) { alpha=beta; beta *= 2; } while(beta-alpha>1) { LL gamma = (alpha+beta)/2; if(PeriodOK(a, b+gamma, period)) alpha=gamma; else beta=gamma; } b += alpha; } void ComputeLongPalin() { LL numLong = 0; LL firstLong = 0; LL step = 0; LL lastLong = 0; for (LL x = myAlpha; x < myBeta; ++x) { if (palinRadius[x] < longPalin) continue; if (numLong==0) { firstLong = x; } else if (numLong==1) { step = x - firstLong; lastLong = x; } else { assert(step == x - lastLong); lastLong = x; } ++numLong; } if (numLong == 0) return; else if (numLong == 1) { palinRadius[firstLong] = ComputeBigRadius(firstLong); } else { LL period = 2 * step; LL periodStart = firstLong - longPalin; LL periodEnd = lastLong + longPalin; ExtendPeriodLeft(periodStart, periodEnd, period); ExtendPeriodRight(periodStart, periodEnd, period); for (LL x = firstLong; x <= lastLong; x += step) { if (x - periodStart < periodEnd - x) { palinRadius[x] = x-periodStart; } else if (x - periodStart > periodEnd - x) { palinRadius[x] = periodEnd-x; } else { palinRadius[x] = ComputeBigRadius(x); } } } } void SendAnswer() { LL answer = 0; for (LL x = myAlpha; x < myBeta && x < N; ++x) { // 0 -> 0 // 1 -> 1 a|a // 2 -> 1 aa|aa // 3 -> 2 aab|baa answer += (palinRadius[x]+1)/2; } SendTo(MASTER, answer); } void CollectAnswers() { LL total = 0; REP(node, NNODES) { LL x; ReceiveFrom(node, x); total += x; } cout << total << '\n'; } int main() { Initialize(); GetData(); SendOutMyHashes(); ReceiveHashes(); ComputeShortPalin(); ComputeLongPalin(); SendAnswer(); if (NODE == MASTER) CollectAnswers(); } |