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
#include <iostream>

using namespace std;

int main(){
    int n; cin>>n;
    long long suma = 0;
    long long sumb = 0;
    long long x=1299821;
    long long rx =864711250;
    long long M=920419901;
    long long xx=1;
    long long k=1;
    long long rk=1;

    long long suma2 = 0;
    long long sumb2 = 0;
    long long M2=920419919;
    long long x2= 83530263;
    long long rx2=128566115;
    long long xx2=1;
    long long k2=1;
    long long rk2=1;

    while(1){
        char ch;
        if(!(cin>>ch)) break;
        //cout << "wczytuję " << (int)ch << " " << endl;
        
        suma += int(ch)*k; 
        suma %= M;
        k *= x;
        k %= M;

        sumb += int(ch)*rk;
        sumb %= M;
        rk *= rx;
        rk %= M;

        xx*=x;
        xx %= M;

        suma2 += int(ch)*k2; 
        suma2 %= M2;
        k2 *= x2;
        k2 %= M2;

        sumb2 += int(ch)*rk2;
        sumb2 %= M2;
        rk2 *= rx2;
        rk2 %= M2;

        xx2*=x2;
        xx2 %= M2;

        //cout<<suma<<" "<<sumb<<endl;
    }
    xx *=rx;
    xx %= M;
    xx2 *=rx2;
    xx2 %= M2;
    //cout<<xx<<" "<<suma<<" "<<sumb<<endl;
    if(((xx*sumb)%M == suma) && ((xx2*sumb2)%M2 == suma2)) cout<<"TAK"<<endl;
    else cout<<"NIE"<<endl;
}