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
//Tadrion
#include <cstdio>
#include <vector>
#include <iostream>
#include <deque>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
using namespace std;
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define CLEAR(x) (memset(x,0,sizeof(x)))
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define VAR(v, n) __typeof(n) v = (n)
#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define FOREACH(i, c) for(VAR(i,(c).begin()); i != (c).end(); ++i)
#define DBG(v) cout<<#v<<" = "<<v<<endl;
#define IN(x,y) ((y).find(x)!=(y).end())
#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

char c;
bool bWynik = false;
int cnt=0;

#define HK 4

LL p[HK],pot[HK],d[HK],di[HK];
LL h1[HK],h2[HK];
deque<char> q;

LL KKK;
/*
Zrodlo kodu:

https://rosettacode.org/wiki/Modular_inverse#C.2B.2B

*/
LL mul_inv(LL a, LL b)
{
	LL b0 = b, t, q;
	LL x0 = 0, x1 = 1;
	if (b == 1) return 1;
	while (a > 1) {
		q = a / b;
		t = b, b = a % b, a = t;
		t = x0, x0 = x1 - q * x0, x1 = t;
	}
	if (x1 < 0) x1 += b0;
	return x1;
}


int main() {

    LL limit;

    p[0]=60983719LL;

    p[1]=459546743LL;

    p[2]=723871927LL;

    p[3]=125063329LL;

    FOR(i,0,HK-1)
    {
        d[i]=29+i;
        pot[i]=1;
        di[i]=mul_inv(d[i],p[i]);
    }

    //cin >> KKK;
    scanf("%lld",&KKK);
    //printf("%d\n",HK);

    if(KKK >0) limit = KKK/2 + 5;
    else limit = 100000001LL;

    if(1==KKK) { bWynik=true; goto KONIEC; }

    while(1)
    {
        if (scanf("%c", &c) != 1)
        {
            goto KONIEC;
        }
        cnt++;

        if(cnt > limit)
        {
            q.pop_back();
        }

        q.push_back(c-'a'+1);

        if(2==cnt)
        {
            REP(i,HK)
            {
                h1[i] = (LL)q.front();
                h2[i] = (LL)q.back();
            }
            q.pop_front();
        }
        else if(3==cnt)
        {
            REP(i,HK)
            {
                h2[i] = (((h2[i]-(LL)(q.front())+p[i])%p[i])+(LL)q.back())%p[i];
            }
        }
        else
        {
            if (cnt%2==0)
            {
                REP(i,HK)
                {
                    pot[i]=(pot[i]*d[i])%p[i];
                    h1[i] = ((h1[i]*d[i])%p[i] + (LL)q.front())%p[i];
                    h2[i] = (h2[i] + ((LL)q.back()*pot[i])%p[i])%p[i];
                }
                q.pop_front();
            }
            else
            {
                REP(i,HK)
                {
                    h2[i] = ((((h2[i] + p[i] - (LL)(q.front())) % p[i]) * di[i])%p[i] + (LL)(q.back()*pot[i])%p[i])%p[i];
                }
            }
        }

        //printf("cnt: %d\n",cnt);
        REP(i,HK)
        {
            //if(h1[i]!=h2[i])
//            printf("i: %d %I64d %I64d\n", i, h1[i], h2[i]);
            //h2[i] = (((h2[i] + p[i] - (q.front()) % p[i]) * di[i])%p[i] + (q.back()*poww(d,cnt/2 - 1))%p[i])%p[i];
        }

        //printf("%c",c);
    }

KONIEC:

    if(1==cnt) bWynik = true;
    else
    {
        bWynik = true;
        REP(i,HK)
        {
            if(h1[i]!=h2[i]) bWynik=false;
        }
    }


    printf("%s\n", bWynik ? "TAK" : "NIE");

  return 0;
}