#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
const int INF = 1000000009;
const LL LINF = 1000000000000000009LL;
#define FOR(i, b, e) for(int i = b; i <= e; ++i)
#define FORD(i, b, e) for(int i = b; i >= e; --i)
#define REP(i, n) FOR(i, 0, n-1)
#define REV(i, n) FORD(i, n-1, 0)
#define PB push_back
#define PP pop_back
#define MP make_pair
#define ST first
#define ND second
#define SIZE(c) (int)(c).size()
#define ALL(c) (c).begin(), (c).end()
#define DEBUG(s) s
const int N = 100005;
int n;
int tab[N];
char c;
inline LL code(char c)
{
return c-'a'+1;
}
struct hasz
{
LL x, m;
LL f, r;
LL potx;
hasz() {}
hasz(LL _x, LL _m) : x(_x), m(_m)
{
potx = 1;
f = 0;
r = 0;
}
};
vector<hasz> hasze;
void init()
{
hasze.PB(hasz(31, 109890109890109837ll));
}
//~ void test()
//~ {
//~ LL x = 31;
//~ LL p = 109890109890109837ll;
//~ LL potx = 1;
//~ FOR(i, 1, 100000000)
//~ {
//~ if(potx < 100000000000ll)
//~ cout<<i<<" "<<potx<<"\n";
//~ potx*=x;
//~ potx%=p;
//~ }
//~ }
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
//~ test();
//~ cout<<"rmc\n";
cin>>n;
init();
while(cin>>c)
{
for(auto &h1: hasze)
{
h1.f += code(c)*h1.potx; // m < 2^63/x
h1.f %= h1.m; // f mod m
h1.potx *= h1.x; //
h1.potx %= h1.m;
h1.r *= h1.x; // m < 2^63 / x
h1.r += code(c); //
h1.r %= h1.m;
}
}
bool res = true;
//~ for(auto h1: hasze)
//~ cout<<h1.f<<" "<<h1.r<<"\n";
for(auto h1: hasze)
{
if(h1.f != h1.r)
{
res = false;
break;
}
}
cout<<(res ? "TAK" : "NIE")<<"\n";
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; typedef vector<int> VI; const int INF = 1000000009; const LL LINF = 1000000000000000009LL; #define FOR(i, b, e) for(int i = b; i <= e; ++i) #define FORD(i, b, e) for(int i = b; i >= e; --i) #define REP(i, n) FOR(i, 0, n-1) #define REV(i, n) FORD(i, n-1, 0) #define PB push_back #define PP pop_back #define MP make_pair #define ST first #define ND second #define SIZE(c) (int)(c).size() #define ALL(c) (c).begin(), (c).end() #define DEBUG(s) s const int N = 100005; int n; int tab[N]; char c; inline LL code(char c) { return c-'a'+1; } struct hasz { LL x, m; LL f, r; LL potx; hasz() {} hasz(LL _x, LL _m) : x(_x), m(_m) { potx = 1; f = 0; r = 0; } }; vector<hasz> hasze; void init() { hasze.PB(hasz(31, 109890109890109837ll)); } //~ void test() //~ { //~ LL x = 31; //~ LL p = 109890109890109837ll; //~ LL potx = 1; //~ FOR(i, 1, 100000000) //~ { //~ if(potx < 100000000000ll) //~ cout<<i<<" "<<potx<<"\n"; //~ potx*=x; //~ potx%=p; //~ } //~ } int main() { ios::sync_with_stdio(0); cin.tie(0); //~ test(); //~ cout<<"rmc\n"; cin>>n; init(); while(cin>>c) { for(auto &h1: hasze) { h1.f += code(c)*h1.potx; // m < 2^63/x h1.f %= h1.m; // f mod m h1.potx *= h1.x; // h1.potx %= h1.m; h1.r *= h1.x; // m < 2^63 / x h1.r += code(c); // h1.r %= h1.m; } } bool res = true; //~ for(auto h1: hasze) //~ cout<<h1.f<<" "<<h1.r<<"\n"; for(auto h1: hasze) { if(h1.f != h1.r) { res = false; break; } } cout<<(res ? "TAK" : "NIE")<<"\n"; return 0; } |
English