#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i ## __ = (n), i = (a); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i ## __ = (n), i = (a); i >= i ## __; --i) #define REP(i,n) FOR(i,0, n - 1) #define ALL(h) begin(h), end(h) #define EB emplace_back #define X first #define Y second #define V vector #define tpv typedef V< typedef long long LL; typedef pair<int, int> PII; tpv int> VI; tpv VI> VVI; tpv PII> VPII; tpv LL> VLL; constexpr char nl = '\n'; #define endl nl #define ris return *this #define tem template<class t tem, class u> inline void mini(t& a, u&& b) { if (b < a) a = b; } tem, class u> inline void maxi(t& a, u&& b) { if (b > a) a = b; } int ceil2(int h) { return h < 2 ? 1 : 1 << (sizeof(h) * 8 - __builtin_clz(h - 1)); } tem> struct Dump { t a, b; }; tem> auto dump(t&& h) -> Dump<decltype(begin(h))> { return {ALL(h)}; } tem> auto stub(t* h) -> decltype(cerr << *h, 0); tem> char stub(...); #define enif(o) tem> typename enable_if<sizeof stub<t>(0) o 1, debug&>::type operator<<(t h) #define dor > debug& operator<< struct debug { #ifdef DEBUG #define deb debug() ~debug() { cerr << nl; } enif(!=) { cerr << boolalpha << h; ris; } enif(==) { *this << '{'; for (auto a = begin(h), b = end(h); a != b;) *this << *a++ << &" "[a == b]; ris << '}'; } tem, class u dor(pair<t, u> p) { ris << '(' << p.X << ", " << p.Y << ')'; } tem dor(Dump<t> d) { *this << "{\n"; for (t a = d.a, c = a; a != d.b; ++a) *this << " " << distance(c, a) << ": " << *a << nl; ris << '}'; } #else operator int() { return 0; } #define deb 0 and debug() tem dor(t&&) { ris; } #endif }; #define imie(h...) #h ": " << (h) << " " #define LOG(h...) deb << imie(h) #define DOG(h...) deb << #h ": " << dump(h) << " " long long inv(long long a, long long m){ return 1<a ? m - inv(m%a,a)*m/a : 1; } struct Hasher { unsigned cum_hash = 0; unsigned cum_inv_hash = 0; int p, inv_p; unsigned mod; int curr_p, curr_inv_p; Hasher(int p_, int mod_) : p(p_), inv_p(inv(p_, mod_)), mod(mod_), curr_p(p), curr_inv_p(inv_p) { LOG(cum_hash) imie(cum_inv_hash); LOG(p) imie(inv_p) imie(mod); LOG(curr_p) imie(curr_inv_p); } void feed(int c) noexcept { cum_hash += LL(c) * curr_p % mod; if (cum_hash >= mod) cum_hash -= mod; cum_inv_hash += LL(c) * curr_inv_p % mod; if (cum_inv_hash >= mod) cum_inv_hash -= mod; curr_p = LL(curr_p) * p % mod; curr_inv_p = LL(curr_inv_p) * inv_p % mod; LOG(cum_hash) imie(cum_inv_hash); LOG(p) imie(inv_p) imie(mod); LOG(curr_p) imie(curr_inv_p); } bool are_equal() const noexcept { return cum_hash == LL(cum_inv_hash) * curr_p % mod; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); {int n; cin >> n;} #if 0 array<Hasher, 1> hash {{ {2, 10000001}, }}; #else array<Hasher, 2> hash {{ {11611597, 1023312607}, //{15901937, 1960418623}, {18801733, 1731721687}, }}; #endif char c; while (cin >> c) { deb << "-----------------------------------"; LOG(c); for (auto& h : hash) h.feed(c); } bool res = true; for (auto& h : hash) res &= h.are_equal(); puts(res ? "TAK" : "NIE"); 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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,a,n) for (auto i ## __ = (n), i = (a); i <= i ## __; ++i) #define FORD(i,a,n) for (auto i ## __ = (n), i = (a); i >= i ## __; --i) #define REP(i,n) FOR(i,0, n - 1) #define ALL(h) begin(h), end(h) #define EB emplace_back #define X first #define Y second #define V vector #define tpv typedef V< typedef long long LL; typedef pair<int, int> PII; tpv int> VI; tpv VI> VVI; tpv PII> VPII; tpv LL> VLL; constexpr char nl = '\n'; #define endl nl #define ris return *this #define tem template<class t tem, class u> inline void mini(t& a, u&& b) { if (b < a) a = b; } tem, class u> inline void maxi(t& a, u&& b) { if (b > a) a = b; } int ceil2(int h) { return h < 2 ? 1 : 1 << (sizeof(h) * 8 - __builtin_clz(h - 1)); } tem> struct Dump { t a, b; }; tem> auto dump(t&& h) -> Dump<decltype(begin(h))> { return {ALL(h)}; } tem> auto stub(t* h) -> decltype(cerr << *h, 0); tem> char stub(...); #define enif(o) tem> typename enable_if<sizeof stub<t>(0) o 1, debug&>::type operator<<(t h) #define dor > debug& operator<< struct debug { #ifdef DEBUG #define deb debug() ~debug() { cerr << nl; } enif(!=) { cerr << boolalpha << h; ris; } enif(==) { *this << '{'; for (auto a = begin(h), b = end(h); a != b;) *this << *a++ << &" "[a == b]; ris << '}'; } tem, class u dor(pair<t, u> p) { ris << '(' << p.X << ", " << p.Y << ')'; } tem dor(Dump<t> d) { *this << "{\n"; for (t a = d.a, c = a; a != d.b; ++a) *this << " " << distance(c, a) << ": " << *a << nl; ris << '}'; } #else operator int() { return 0; } #define deb 0 and debug() tem dor(t&&) { ris; } #endif }; #define imie(h...) #h ": " << (h) << " " #define LOG(h...) deb << imie(h) #define DOG(h...) deb << #h ": " << dump(h) << " " long long inv(long long a, long long m){ return 1<a ? m - inv(m%a,a)*m/a : 1; } struct Hasher { unsigned cum_hash = 0; unsigned cum_inv_hash = 0; int p, inv_p; unsigned mod; int curr_p, curr_inv_p; Hasher(int p_, int mod_) : p(p_), inv_p(inv(p_, mod_)), mod(mod_), curr_p(p), curr_inv_p(inv_p) { LOG(cum_hash) imie(cum_inv_hash); LOG(p) imie(inv_p) imie(mod); LOG(curr_p) imie(curr_inv_p); } void feed(int c) noexcept { cum_hash += LL(c) * curr_p % mod; if (cum_hash >= mod) cum_hash -= mod; cum_inv_hash += LL(c) * curr_inv_p % mod; if (cum_inv_hash >= mod) cum_inv_hash -= mod; curr_p = LL(curr_p) * p % mod; curr_inv_p = LL(curr_inv_p) * inv_p % mod; LOG(cum_hash) imie(cum_inv_hash); LOG(p) imie(inv_p) imie(mod); LOG(curr_p) imie(curr_inv_p); } bool are_equal() const noexcept { return cum_hash == LL(cum_inv_hash) * curr_p % mod; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); {int n; cin >> n;} #if 0 array<Hasher, 1> hash {{ {2, 10000001}, }}; #else array<Hasher, 2> hash {{ {11611597, 1023312607}, //{15901937, 1960418623}, {18801733, 1731721687}, }}; #endif char c; while (cin >> c) { deb << "-----------------------------------"; LOG(c); for (auto& h : hash) h.feed(c); } bool res = true; for (auto& h : hash) res &= h.are_equal(); puts(res ? "TAK" : "NIE"); return 0; } |