#include <cstdint> #include <iostream> using namespace std; namespace { using u32 = uint32_t; using u64 = uint64_t; template <u32 Mod, u32 Base> class ModHash { private: u32 value; u32 base_power(int exp) { if (exp == 0) return 1; if (exp == 1) return Base; auto tmp = base_power(exp / 2); u32 res = u64{tmp} * tmp % Mod; if (exp % 2 == 1) { res = u64{res} * Base % Mod; } return res; } public: ModHash(): value{0} { } ModHash(u32 v): value{v} { if (value >= Mod) value %= Mod; } ModHash& operator<<=(int len) { value = value * u64{base_power(len)} % Mod; return *this; } ModHash& operator+=(ModHash const& rhs) { value += rhs.value; if (value >= Mod) value -= Mod; return *this; } ModHash& operator+=(u32 rhs) { if (rhs < Mod) value += rhs; else value += rhs % Mod; if (value >= Mod) value -= Mod; return *this; } ModHash& operator*=(ModHash const& rhs) { value = u64{value} * rhs.value % Mod; return *this; } friend bool operator==(ModHash const& lhs, ModHash const& rhs) { return lhs.value == rhs.value; } }; template <u64 Base> class WrapHash { private: u64 value; u64 base_power(int exp) { if (exp == 0) return 1; if (exp == 1) return Base; auto tmp = base_power(exp / 2); auto res = tmp * tmp; if (exp % 2 == 1) { res *= Base; } return res; } public: WrapHash(): value{0} { } WrapHash(u32 v): value{v} { } WrapHash& operator<<=(int len) { value *= base_power(len); return *this; } WrapHash& operator+=(WrapHash const& rhs) { value += rhs.value; return *this; } WrapHash& operator+=(u32 rhs) { value += rhs; return *this; } WrapHash& operator*=(WrapHash const& rhs) { value *= rhs.value; return *this; } friend bool operator==(WrapHash const& lhs, WrapHash const& rhs) { return lhs.value == rhs.value; } }; template <typename... Hs> class MultiHash; template <> class MultiHash<> { public: MultiHash() { } MultiHash(u32) { } MultiHash& operator<<=(int) { return *this; } MultiHash& operator+=(MultiHash const&) { return *this; } MultiHash& operator+=(u32) { return *this; } MultiHash& operator*=(MultiHash const&) { return *this; } friend bool operator==(MultiHash const&, MultiHash const&) { return true; } }; template <typename H, typename... Hs> class MultiHash<H, Hs...>: private MultiHash<Hs...> { private: using Parent = MultiHash<Hs...>; H hash; Parent& parent() { return *this; } Parent const& parent() const { return *this; } public: MultiHash(): Parent{}, hash{} { } MultiHash(u32 v): Parent{v}, hash{v} { } MultiHash& operator<<=(int len) { parent() <<= len; hash <<= len; return *this; } MultiHash& operator+=(MultiHash const& rhs) { parent() += rhs.parent(); hash += rhs.hash; return *this; } MultiHash& operator+=(u32 rhs) { parent() += rhs; hash += rhs; return *this; } MultiHash& operator*=(MultiHash const& rhs) { parent() *= rhs.parent(); hash *= rhs.hash; return *this; } friend bool operator==(MultiHash const& lhs, MultiHash const& rhs) { return lhs.parent() == rhs.parent() && lhs.hash == rhs.hash; } }; using Hash = MultiHash< ModHash<1003162753, 1001>, ModHash<1023893771, 1003>, ModHash<1044645379, 1003>, WrapHash<1316196199> >; bool solve() { int n; cin >> n; Hash forward{}; Hash reverse{}; Hash shift{1}; char c; while (cin >> c) { forward <<= 1; forward += c; Hash tmp = shift; tmp *= c; reverse += tmp; shift <<= 1; } return forward == reverse; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); cout << (solve() ? "TAK" : "NIE") << 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 261 262 263 264 | #include <cstdint> #include <iostream> using namespace std; namespace { using u32 = uint32_t; using u64 = uint64_t; template <u32 Mod, u32 Base> class ModHash { private: u32 value; u32 base_power(int exp) { if (exp == 0) return 1; if (exp == 1) return Base; auto tmp = base_power(exp / 2); u32 res = u64{tmp} * tmp % Mod; if (exp % 2 == 1) { res = u64{res} * Base % Mod; } return res; } public: ModHash(): value{0} { } ModHash(u32 v): value{v} { if (value >= Mod) value %= Mod; } ModHash& operator<<=(int len) { value = value * u64{base_power(len)} % Mod; return *this; } ModHash& operator+=(ModHash const& rhs) { value += rhs.value; if (value >= Mod) value -= Mod; return *this; } ModHash& operator+=(u32 rhs) { if (rhs < Mod) value += rhs; else value += rhs % Mod; if (value >= Mod) value -= Mod; return *this; } ModHash& operator*=(ModHash const& rhs) { value = u64{value} * rhs.value % Mod; return *this; } friend bool operator==(ModHash const& lhs, ModHash const& rhs) { return lhs.value == rhs.value; } }; template <u64 Base> class WrapHash { private: u64 value; u64 base_power(int exp) { if (exp == 0) return 1; if (exp == 1) return Base; auto tmp = base_power(exp / 2); auto res = tmp * tmp; if (exp % 2 == 1) { res *= Base; } return res; } public: WrapHash(): value{0} { } WrapHash(u32 v): value{v} { } WrapHash& operator<<=(int len) { value *= base_power(len); return *this; } WrapHash& operator+=(WrapHash const& rhs) { value += rhs.value; return *this; } WrapHash& operator+=(u32 rhs) { value += rhs; return *this; } WrapHash& operator*=(WrapHash const& rhs) { value *= rhs.value; return *this; } friend bool operator==(WrapHash const& lhs, WrapHash const& rhs) { return lhs.value == rhs.value; } }; template <typename... Hs> class MultiHash; template <> class MultiHash<> { public: MultiHash() { } MultiHash(u32) { } MultiHash& operator<<=(int) { return *this; } MultiHash& operator+=(MultiHash const&) { return *this; } MultiHash& operator+=(u32) { return *this; } MultiHash& operator*=(MultiHash const&) { return *this; } friend bool operator==(MultiHash const&, MultiHash const&) { return true; } }; template <typename H, typename... Hs> class MultiHash<H, Hs...>: private MultiHash<Hs...> { private: using Parent = MultiHash<Hs...>; H hash; Parent& parent() { return *this; } Parent const& parent() const { return *this; } public: MultiHash(): Parent{}, hash{} { } MultiHash(u32 v): Parent{v}, hash{v} { } MultiHash& operator<<=(int len) { parent() <<= len; hash <<= len; return *this; } MultiHash& operator+=(MultiHash const& rhs) { parent() += rhs.parent(); hash += rhs.hash; return *this; } MultiHash& operator+=(u32 rhs) { parent() += rhs; hash += rhs; return *this; } MultiHash& operator*=(MultiHash const& rhs) { parent() *= rhs.parent(); hash *= rhs.hash; return *this; } friend bool operator==(MultiHash const& lhs, MultiHash const& rhs) { return lhs.parent() == rhs.parent() && lhs.hash == rhs.hash; } }; using Hash = MultiHash< ModHash<1003162753, 1001>, ModHash<1023893771, 1003>, ModHash<1044645379, 1003>, WrapHash<1316196199> >; bool solve() { int n; cin >> n; Hash forward{}; Hash reverse{}; Hash shift{1}; char c; while (cin >> c) { forward <<= 1; forward += c; Hash tmp = shift; tmp *= c; reverse += tmp; shift <<= 1; } return forward == reverse; } } int main() { iostream::sync_with_stdio(false); cin.tie(nullptr); cout << (solve() ? "TAK" : "NIE") << endl; return 0; } |