#include <iostream> #include <vector> long long powmod(long long a, long long b, long long mod) { long long result = 1; while (b > 0) { if (b&1) result = (result*a)%mod; a = (a*a)%mod; b /= 2; } return result; } // standard muilt inv mod impl from https://rosettacode.org/wiki/Modular_inverse long long inv(long long a, long long b) { long long t, nt, r, nr, q, tmp; if (b < 0) b = -b; if (a < 0) a = b - (-a % b); t = 0; nt = 1; r = b; nr = a % b; while (nr != 0) { q = r/nr; tmp = nt; nt = t - q*nt; t = tmp; tmp = nr; nr = r - q*nr; r = tmp; } if (r > 1) return -1; /* No inverse */ if (t < 0) t += b; return t; } const long long MAXPOS = 20000005; class Hash { public: Hash(long long p, long long b, long long dir, long long pos) : p(p) , b(b) , dir(dir) , pos(pos) , value(0) {} void add(long long x) { value = (value + x * powmod(b, pos, p)) % p; pos += dir; } long long val(long long offset) const { return value * inv(powmod(b, offset, p), p) % p; } long long dir; long long pos; long long value; long long b; long long p; }; class UberHash { public: UberHash(std::vector<std::pair<long long, long long>> config) { for (const auto& c : config) { hashes.push_back({ Hash(c.first, c.second, 1ll, 0ll), Hash(c.first, c.second, -1ll, MAXPOS)}); } } void add(long long x) { for (auto& h : hashes) { h.first.add(x); h.second.add(x); } } bool equal(long long len) { bool result = true; for (auto& h : hashes) { result &= h.first.val(0) == h.second.val(MAXPOS-len+1); } return result; } void dbg(long long len) { std::clog << " :: DBG ::" << std::endl; for (auto& h : hashes) { std::clog << " :: " << h.first.val(0) << std::endl; std::clog << " :: " << h.second.val(MAXPOS-len+1) << std::endl; } } std::vector<std::pair<Hash, Hash>> hashes; }; int main() { int n; std::cin >> n; std::cin.ignore(); UberHash hash({ //{10000000000ll, 10}, {982440491, 179425817}, {982452241, 179426129}, {982448699, 179424719}, {982453687, 982453117}, {982450913, 179441761}, {982445873, 179450581}, {982446991, 179440453}, {982441403, 179426549}, {982441589, 179449769}, {982439399, 982437881}, {1400290777, 1400290681}, {1400302187, 1400291927}, {1400292767, 1400291771}, {1400298997, 1400298013}, {1400298877, 1400298773}, {1400298877, 1400298773}, {1400315947, 1400313839}, {1400315383, 10570633}, {1399252903, 10566463}, {1399253399, 10562017} }); long long len = 0; while (std::cin.peek() != '\n') { char c = std::cin.get(); if (c >= 'a' && c <= 'z') { ++len; hash.add(c-'a'+1); //hash.dbg(len); } } std::cout << (hash.equal(len) ? "TAK" : "NIE") << std::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 | #include <iostream> #include <vector> long long powmod(long long a, long long b, long long mod) { long long result = 1; while (b > 0) { if (b&1) result = (result*a)%mod; a = (a*a)%mod; b /= 2; } return result; } // standard muilt inv mod impl from https://rosettacode.org/wiki/Modular_inverse long long inv(long long a, long long b) { long long t, nt, r, nr, q, tmp; if (b < 0) b = -b; if (a < 0) a = b - (-a % b); t = 0; nt = 1; r = b; nr = a % b; while (nr != 0) { q = r/nr; tmp = nt; nt = t - q*nt; t = tmp; tmp = nr; nr = r - q*nr; r = tmp; } if (r > 1) return -1; /* No inverse */ if (t < 0) t += b; return t; } const long long MAXPOS = 20000005; class Hash { public: Hash(long long p, long long b, long long dir, long long pos) : p(p) , b(b) , dir(dir) , pos(pos) , value(0) {} void add(long long x) { value = (value + x * powmod(b, pos, p)) % p; pos += dir; } long long val(long long offset) const { return value * inv(powmod(b, offset, p), p) % p; } long long dir; long long pos; long long value; long long b; long long p; }; class UberHash { public: UberHash(std::vector<std::pair<long long, long long>> config) { for (const auto& c : config) { hashes.push_back({ Hash(c.first, c.second, 1ll, 0ll), Hash(c.first, c.second, -1ll, MAXPOS)}); } } void add(long long x) { for (auto& h : hashes) { h.first.add(x); h.second.add(x); } } bool equal(long long len) { bool result = true; for (auto& h : hashes) { result &= h.first.val(0) == h.second.val(MAXPOS-len+1); } return result; } void dbg(long long len) { std::clog << " :: DBG ::" << std::endl; for (auto& h : hashes) { std::clog << " :: " << h.first.val(0) << std::endl; std::clog << " :: " << h.second.val(MAXPOS-len+1) << std::endl; } } std::vector<std::pair<Hash, Hash>> hashes; }; int main() { int n; std::cin >> n; std::cin.ignore(); UberHash hash({ //{10000000000ll, 10}, {982440491, 179425817}, {982452241, 179426129}, {982448699, 179424719}, {982453687, 982453117}, {982450913, 179441761}, {982445873, 179450581}, {982446991, 179440453}, {982441403, 179426549}, {982441589, 179449769}, {982439399, 982437881}, {1400290777, 1400290681}, {1400302187, 1400291927}, {1400292767, 1400291771}, {1400298997, 1400298013}, {1400298877, 1400298773}, {1400298877, 1400298773}, {1400315947, 1400313839}, {1400315383, 10570633}, {1399252903, 10566463}, {1399253399, 10562017} }); long long len = 0; while (std::cin.peek() != '\n') { char c = std::cin.get(); if (c >= 'a' && c <= 'z') { ++len; hash.add(c-'a'+1); //hash.dbg(len); } } std::cout << (hash.equal(len) ? "TAK" : "NIE") << std::endl; return 0; } |