#include <cstdio>
#include <cctype>
typedef long long ll;
const int mod[3]=
{
2137000003,
2137000199,
2137006559
};
int n,hasz1[3],hasz2[3];
ll pot[3]={1ll,1ll,1ll},pod[3]={31ll,41ll,37ll};
int main() {
scanf("%d ",&n)?:0;
while (true) {
int z=getchar_unlocked();
if (z==EOF || isspace(z)) break;
#define i 0
hasz1[i]=(pod[i]*hasz1[i]+z-96)%mod[i];
hasz2[i]=(pot[i]*(z-96)+hasz2[i])%mod[i];
pot[i]=pod[i]*pot[i]%mod[i];
#undef i
#define i 1
hasz1[i]=(pod[i]*hasz1[i]+z-96)%mod[i];
hasz2[i]=(pot[i]*(z-96)+hasz2[i])%mod[i];
pot[i]=pod[i]*pot[i]%mod[i];
#undef i
#define i 2
hasz1[i]=(pod[i]*hasz1[i]+z-96)%mod[i];
hasz2[i]=(pot[i]*(z-96)+hasz2[i])%mod[i];
pot[i]=pod[i]*pot[i]%mod[i];
#undef i
}
for (int i=0; i<3; i++) {
if (hasz1[i]!=hasz2[i]) {
puts("NIE");
return 0;
}
}
puts("TAK");
}
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 | #include <cstdio> #include <cctype> typedef long long ll; const int mod[3]= { 2137000003, 2137000199, 2137006559 }; int n,hasz1[3],hasz2[3]; ll pot[3]={1ll,1ll,1ll},pod[3]={31ll,41ll,37ll}; int main() { scanf("%d ",&n)?:0; while (true) { int z=getchar_unlocked(); if (z==EOF || isspace(z)) break; #define i 0 hasz1[i]=(pod[i]*hasz1[i]+z-96)%mod[i]; hasz2[i]=(pot[i]*(z-96)+hasz2[i])%mod[i]; pot[i]=pod[i]*pot[i]%mod[i]; #undef i #define i 1 hasz1[i]=(pod[i]*hasz1[i]+z-96)%mod[i]; hasz2[i]=(pot[i]*(z-96)+hasz2[i])%mod[i]; pot[i]=pod[i]*pot[i]%mod[i]; #undef i #define i 2 hasz1[i]=(pod[i]*hasz1[i]+z-96)%mod[i]; hasz2[i]=(pot[i]*(z-96)+hasz2[i])%mod[i]; pot[i]=pod[i]*pot[i]%mod[i]; #undef i } for (int i=0; i<3; i++) { if (hasz1[i]!=hasz2[i]) { puts("NIE"); return 0; } } puts("TAK"); } |
English