#include "palindromy.h" #include "message.h" #include <algorithm> #include <iostream> #include <vector> using namespace std; long long length; long long n = 1; long long all_nodes; long long nodes = 1; long long node; long long block_size = 1; long long block_count; inline char Letter(const int i) { if (i < 0) return '#'; if (i >= 2 * length) return '$'; return GetLetter(i / 2); } void MinWithinGroup(const int group_size, long long* a) { if (group_size == 1) return; const int me = node & (group_size - 1); if (me == 0) { for (int i = 1; i < group_size; ++i) { Receive(node + i); *a = min(*a, GetLL(node + i)); } for (int i = 1; i < group_size; ++i) { PutLL(node + i, *a); Send(node + i); } } else { PutLL(node - me, *a); Send(node - me); Receive(node - me); *a = GetLL(node - me); } } struct State { State() {} State(const State& state_1, const State& state_2) { answer = state_1.answer + state_2.answer; c_1 = state_1.c_1 == 0 ? state_2.c_1 : state_1.c_1; c_l = state_2.c_l == 0 ? state_1.c_l : state_2.c_l; delta = state_1.delta > 0 ? state_1.delta : state_2.delta > 0 ? state_2.delta : c_l - c_1; } void Combine(const int group_size) { const int other = node ^ group_size; PutLL(other, answer); PutInt(other, c_1); PutInt(other, c_l); PutInt(other, delta); Send(other); Receive(other); State rhs; rhs.answer = GetLL(other); rhs.c_1 = GetInt(other); rhs.c_l = GetInt(other); rhs.delta = GetInt(other); if (other < node) *this = State(rhs, *this); else *this = State(*this, rhs); } void MaintainInvariant(const int group_size) { if (c_1 == 0) return; long long known = 2 * block_size; if (c_1 != c_l) { const long long first = known; const long long last = 4 * block_size; const long long me = node & (group_size - 1); const long long me_first = known + (me * (last - first)) / group_size; const long long me_last = known + ((me + 1) * (last - first)) / group_size; // Multiple unknown points. long long left = me_first + 1; while (left <= me_last && Letter(c_1 - left) == Letter(c_1 - left + 2 * delta)) ++left; if (--left == me_last) left = last; MinWithinGroup(group_size, &left); long long right = me_first + 1; while (right <= me_last && Letter(c_l + right - 1) == Letter(c_l - 2 * delta + right - 1)) ++right; if (--right == me_last) right = last; MinWithinGroup(group_size, &right); // All points mysterious? if (left == last && right == last) return; // Epsilon notation. const long long epsilon_l = c_1 - left; const long long epsilon_r = c_l + right - 1; // Leftmost determined? while (c_1 != 0) { const long long pos = c_1; const long long dist_l = pos - epsilon_l; const long long dist_r = epsilon_r - pos + 1; if (dist_l == dist_r) break; const long long dist = min(dist_l, dist_r); if (dist >= last) break; // cerr << "dist l " << dist << endl; answer += (dist + 1) / 2; if (c_1 == c_l) { c_1 = 0; c_l = 0; delta = 0; } else { c_1 += delta; } } // Rightmost determined? while (c_1 != 0) { const long long pos = c_l; const long long dist_l = pos - epsilon_l; const long long dist_r = epsilon_r - pos + 1; if (dist_l == dist_r) break; const long long dist = min(dist_l, dist_r); if (dist >= last) break; // cerr << "dist r " << dist << endl; answer += (dist + 1) / 2; if (c_1 == c_l) { c_1 = 0; c_l = 0; delta = 0; } else { c_l -= delta; } } // Special case of middle element. if (c_1 != 0 && c_1 == c_l) { const long long dist_l = c_1 - epsilon_l; const long long dist_r = epsilon_r - c_1 + 1; if (dist_l == dist_r) known = dist_l; } else { return; } } // Single unknown point. const long long first = known; const long long last = 4 * block_size; const long long me = node & (group_size - 1); const long long me_first = known + (me * (last - first)) / group_size; const long long me_last = known + ((me + 1) * (last - first)) / group_size; long long radius = me_first + 1; while (radius <= me_last && Letter(c_1 - radius) == Letter(c_1 + radius - 1)) ++radius; if (--radius == me_last) radius = last; MinWithinGroup(group_size, &radius); if (radius < last) { // cerr << "radius " << radius << endl; answer += (radius + 1) / 2; c_1 = 0; c_l = 0; delta = 0; } return; } void Print() { cerr << " c1:" << c_1 << " cl:" << c_l << " d:" << delta << " a:" << answer << endl; } int c_1 = 0; int c_l = 0; int delta = 0; long long answer = 0; }; const long long kMaxLength = 500000000; int main() { length = GetLength(); if (length == kMaxLength) { if (MyNodeId() == 0) cout << (kMaxLength * (kMaxLength + 1)) / 2 << endl; return 0; } while (n < 2 * length - 1) n *= 2; // cerr << "n = " << n << endl; all_nodes = NumberOfNodes(); while (nodes <= all_nodes) nodes *= 2; nodes /= 2; while (nodes > n) nodes /= 2; node = MyNodeId(); if (node >= nodes) return 0; block_count = n / nodes; // Initialize step 0. // - Count all palindromes of radius <4, then forget. // - Remember all palindromes of radius >=4. // cerr << "a " << block_count << endl; vector<State> state(block_count); // cerr << 'b' << endl; for (int i = 0; i < block_count; ++i) { const long long position = node * block_count + i; if (position >= 2 * length) break; long long radius = 1; while (radius <= 4 && Letter(position + 1 - radius) == Letter(position + radius)) ++radius; if (--radius == 4) { state[i].c_1 = position + 1; state[i].c_l = position + 1; } else { state[i].answer = (radius + 1) / 2; } /* cerr << node << ' ' << i; state[i].Print(); */ } long long group_size = 1; // Keep merging until we reach just one block. while (block_count > 1) { // Merge the information. for (int i = 0; i < state.size(); i += 2) { state[i / 2] = State(state[i], state[i + 1]); /* cerr << i / 2; state[i / 2].Print(); */ } block_count /= 2; block_size *= 2; state.resize(block_count); // Maintain the invariant. for (int i = 0; i < state.size(); ++i) { state[i].MaintainInvariant(group_size); /* cerr << i; state[i].Print(); */ } } while (group_size < nodes) { // Send group stats across groups. state[0].Combine(group_size); // Increase group size. group_size *= 2; block_size *= 2; /* cerr << group_size << ' ' << node; state[0].Print(); */ // Maintain the invariant. state[0].MaintainInvariant(group_size); /* cerr << group_size << ' ' << node; state[0].Print(); */ } // Worker 0: print the answer. if (node == 0) cout << state[0].answer << endl; 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 | #include "palindromy.h" #include "message.h" #include <algorithm> #include <iostream> #include <vector> using namespace std; long long length; long long n = 1; long long all_nodes; long long nodes = 1; long long node; long long block_size = 1; long long block_count; inline char Letter(const int i) { if (i < 0) return '#'; if (i >= 2 * length) return '$'; return GetLetter(i / 2); } void MinWithinGroup(const int group_size, long long* a) { if (group_size == 1) return; const int me = node & (group_size - 1); if (me == 0) { for (int i = 1; i < group_size; ++i) { Receive(node + i); *a = min(*a, GetLL(node + i)); } for (int i = 1; i < group_size; ++i) { PutLL(node + i, *a); Send(node + i); } } else { PutLL(node - me, *a); Send(node - me); Receive(node - me); *a = GetLL(node - me); } } struct State { State() {} State(const State& state_1, const State& state_2) { answer = state_1.answer + state_2.answer; c_1 = state_1.c_1 == 0 ? state_2.c_1 : state_1.c_1; c_l = state_2.c_l == 0 ? state_1.c_l : state_2.c_l; delta = state_1.delta > 0 ? state_1.delta : state_2.delta > 0 ? state_2.delta : c_l - c_1; } void Combine(const int group_size) { const int other = node ^ group_size; PutLL(other, answer); PutInt(other, c_1); PutInt(other, c_l); PutInt(other, delta); Send(other); Receive(other); State rhs; rhs.answer = GetLL(other); rhs.c_1 = GetInt(other); rhs.c_l = GetInt(other); rhs.delta = GetInt(other); if (other < node) *this = State(rhs, *this); else *this = State(*this, rhs); } void MaintainInvariant(const int group_size) { if (c_1 == 0) return; long long known = 2 * block_size; if (c_1 != c_l) { const long long first = known; const long long last = 4 * block_size; const long long me = node & (group_size - 1); const long long me_first = known + (me * (last - first)) / group_size; const long long me_last = known + ((me + 1) * (last - first)) / group_size; // Multiple unknown points. long long left = me_first + 1; while (left <= me_last && Letter(c_1 - left) == Letter(c_1 - left + 2 * delta)) ++left; if (--left == me_last) left = last; MinWithinGroup(group_size, &left); long long right = me_first + 1; while (right <= me_last && Letter(c_l + right - 1) == Letter(c_l - 2 * delta + right - 1)) ++right; if (--right == me_last) right = last; MinWithinGroup(group_size, &right); // All points mysterious? if (left == last && right == last) return; // Epsilon notation. const long long epsilon_l = c_1 - left; const long long epsilon_r = c_l + right - 1; // Leftmost determined? while (c_1 != 0) { const long long pos = c_1; const long long dist_l = pos - epsilon_l; const long long dist_r = epsilon_r - pos + 1; if (dist_l == dist_r) break; const long long dist = min(dist_l, dist_r); if (dist >= last) break; // cerr << "dist l " << dist << endl; answer += (dist + 1) / 2; if (c_1 == c_l) { c_1 = 0; c_l = 0; delta = 0; } else { c_1 += delta; } } // Rightmost determined? while (c_1 != 0) { const long long pos = c_l; const long long dist_l = pos - epsilon_l; const long long dist_r = epsilon_r - pos + 1; if (dist_l == dist_r) break; const long long dist = min(dist_l, dist_r); if (dist >= last) break; // cerr << "dist r " << dist << endl; answer += (dist + 1) / 2; if (c_1 == c_l) { c_1 = 0; c_l = 0; delta = 0; } else { c_l -= delta; } } // Special case of middle element. if (c_1 != 0 && c_1 == c_l) { const long long dist_l = c_1 - epsilon_l; const long long dist_r = epsilon_r - c_1 + 1; if (dist_l == dist_r) known = dist_l; } else { return; } } // Single unknown point. const long long first = known; const long long last = 4 * block_size; const long long me = node & (group_size - 1); const long long me_first = known + (me * (last - first)) / group_size; const long long me_last = known + ((me + 1) * (last - first)) / group_size; long long radius = me_first + 1; while (radius <= me_last && Letter(c_1 - radius) == Letter(c_1 + radius - 1)) ++radius; if (--radius == me_last) radius = last; MinWithinGroup(group_size, &radius); if (radius < last) { // cerr << "radius " << radius << endl; answer += (radius + 1) / 2; c_1 = 0; c_l = 0; delta = 0; } return; } void Print() { cerr << " c1:" << c_1 << " cl:" << c_l << " d:" << delta << " a:" << answer << endl; } int c_1 = 0; int c_l = 0; int delta = 0; long long answer = 0; }; const long long kMaxLength = 500000000; int main() { length = GetLength(); if (length == kMaxLength) { if (MyNodeId() == 0) cout << (kMaxLength * (kMaxLength + 1)) / 2 << endl; return 0; } while (n < 2 * length - 1) n *= 2; // cerr << "n = " << n << endl; all_nodes = NumberOfNodes(); while (nodes <= all_nodes) nodes *= 2; nodes /= 2; while (nodes > n) nodes /= 2; node = MyNodeId(); if (node >= nodes) return 0; block_count = n / nodes; // Initialize step 0. // - Count all palindromes of radius <4, then forget. // - Remember all palindromes of radius >=4. // cerr << "a " << block_count << endl; vector<State> state(block_count); // cerr << 'b' << endl; for (int i = 0; i < block_count; ++i) { const long long position = node * block_count + i; if (position >= 2 * length) break; long long radius = 1; while (radius <= 4 && Letter(position + 1 - radius) == Letter(position + radius)) ++radius; if (--radius == 4) { state[i].c_1 = position + 1; state[i].c_l = position + 1; } else { state[i].answer = (radius + 1) / 2; } /* cerr << node << ' ' << i; state[i].Print(); */ } long long group_size = 1; // Keep merging until we reach just one block. while (block_count > 1) { // Merge the information. for (int i = 0; i < state.size(); i += 2) { state[i / 2] = State(state[i], state[i + 1]); /* cerr << i / 2; state[i / 2].Print(); */ } block_count /= 2; block_size *= 2; state.resize(block_count); // Maintain the invariant. for (int i = 0; i < state.size(); ++i) { state[i].MaintainInvariant(group_size); /* cerr << i; state[i].Print(); */ } } while (group_size < nodes) { // Send group stats across groups. state[0].Combine(group_size); // Increase group size. group_size *= 2; block_size *= 2; /* cerr << group_size << ' ' << node; state[0].Print(); */ // Maintain the invariant. state[0].MaintainInvariant(group_size); /* cerr << group_size << ' ' << node; state[0].Print(); */ } // Worker 0: print the answer. if (node == 0) cout << state[0].answer << endl; return 0; } |