#include <cstdio> #include <vector> using namespace std; using ll = long long; struct hasher { ll M; ll b, binv; hasher(ll M, ll b, ll binv): M(M), b(b), binv(binv) {} ll a1 = 0, a2 = 0, k = 1, kinv = 1; void add(int s) { a1 += (s * k); a1 %= M; a2 += (s * kinv); a2 %= M; k *= b; k %= M; kinv *= binv; kinv %= M; } bool check() { //printf("a1: %lld, a2: %lld\n", a1, a2); return (a2 * k) % M == (a1 * b) % M; } }; int main() { vector<hasher> hashers; hashers.emplace_back(1000000007, 433480829, 851063980); hashers.emplace_back(920419823, 94654271, 143600630); int n; scanf("%d\n", &n); while (true) { char ch = getchar(); if (ch == '\n') break; for(hasher&h:hashers)h.add(ch); } bool ok = true; for(hasher&h:hashers) if(!h.check())ok=false; if (ok) printf("TAK\n"); else printf("NIE\n"); }
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 | #include <cstdio> #include <vector> using namespace std; using ll = long long; struct hasher { ll M; ll b, binv; hasher(ll M, ll b, ll binv): M(M), b(b), binv(binv) {} ll a1 = 0, a2 = 0, k = 1, kinv = 1; void add(int s) { a1 += (s * k); a1 %= M; a2 += (s * kinv); a2 %= M; k *= b; k %= M; kinv *= binv; kinv %= M; } bool check() { //printf("a1: %lld, a2: %lld\n", a1, a2); return (a2 * k) % M == (a1 * b) % M; } }; int main() { vector<hasher> hashers; hashers.emplace_back(1000000007, 433480829, 851063980); hashers.emplace_back(920419823, 94654271, 143600630); int n; scanf("%d\n", &n); while (true) { char ch = getchar(); if (ch == '\n') break; for(hasher&h:hashers)h.add(ch); } bool ok = true; for(hasher&h:hashers) if(!h.check())ok=false; if (ok) printf("TAK\n"); else printf("NIE\n"); } |