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
//#include <bits/stdc++.h>
#include <vector>
#include <memory>
#include <stdint.h>
#include <stdio.h>
#include <iostream>

using namespace std;


#ifdef _WIN32
inline int getchar_unlocked() {
    return getchar();
}
#endif

struct Hsh1 {//: public Hash {
    uint64_t a, mod;
    uint64_t hf, hr, ap;
    Hsh1(uint32_t a, uint32_t mod) : a(a), mod(mod), hf(0), hr(0), ap(1) {}

    void add(char c) {
        hf = (hf * a + c) % mod;
        hr = (hr + ap * c) % mod;
        ap = (ap * a) % mod;
    }

    bool maybe() {
        return hf == hr;
    }
};

struct Hsh61 {//: public Hash {
    static constexpr unsigned long long mod = (1ull << 61) - 1;
    unsigned long long modmul61(unsigned long long a, unsigned long long b) {
        // code credit: https://codeforces.com/blog/entry/60442
        unsigned long long l1 = (uint32_t)a, h1 = a >> 32, l2 = (uint32_t)b, h2 = b >> 32;
        unsigned long long l = l1 * l2, m = l1 * h2 + l2 * h1, h = h1 * h2;
        unsigned long long ret = (l & mod) + (l >> 61) + (h << 3) + (m >> 29) + (m << 35 >> 3) + 1;
        ret = (ret & mod) + (ret >> 61);
        return ret - 1;
    }

    uint64_t a;
    uint64_t hf, hr, ap;
    Hsh61(uint64_t a) : a(a), hf(0), hr(0), ap(1) {}

    void add(char c) {
        hf = (modmul61(hf, a) + c);
        if (hf > mod)
            hf -= mod;
        hr = (hr + modmul61(ap, c));
        if (hr > mod)
            hr -= mod;
        ap = modmul61(ap, a);
    }

    bool maybe() {
        return hf == hr;
    }
};

int main() {
    std::ios_base::sync_with_stdio(0);
    //freopen("pal0.in", "r", stdin); // stdin from file
    int n;
    scanf("%d\n", &n);

    Hsh1 h0(928853711, 1578388199);
    Hsh61 h1(1505861389);
    Hsh61 h2(2330602151);
    Hsh61 h3(3626071741);
    for (;;) {
        char ch = getchar_unlocked();
        if (ch == EOF || ch == '\r' || ch == '\n')
            break;
        ch -= 'a';
        h0.add(ch);
        h1.add(ch);
        h2.add(ch);
        h3.add(ch);
    }

    if (!h0.maybe() || !h1.maybe() || !h2.maybe() || !h3.maybe()) {
        printf("NIE\n");
        return 0;
    }

    printf("TAK\n");
}