#include <bits/stdc++.h> #ifdef _WIN32 #define getchar_unlocked getchar #endif // _WIN32 #define ll long long using namespace std; const int M[5] = {1035961709, 1035965659, 1027703951, 1015254979, 1015275553}; const ll P[5] = {31, 29, 37, 41, 43}; int hasz[5][3]; ll potega[5] = {1,1,1,1,1}; inline void fin(int &y) { register char c = 0; y = 0; while(c < 33)c = getchar_unlocked(); do{ y = (y << 1) + (y << 3) + c - '0'; c = getchar_unlocked(); }while(c > 33); } int n; main() { fin(n); n = 0; char val = getchar_unlocked(); while(val >= 'a' && val <= 'z') { val -= 'a' - 1; for(int i = 0; i < 3; i++) { hasz[i][1] = (hasz[i][1] * P[i] + val) % M[i]; hasz[i][2] = (hasz[i][2] + potega[i] * val) % M[i]; potega[i] = (potega[i] * P[i]) % M[i]; } val = 'a'; n++; val = getchar_unlocked(); } int res = 1; for(int i = 0; i < 3; i++) if(hasz[i][1] != hasz[i][2])res = 0; puts(res ? "TAK" : "NIE"); }
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 | #include <bits/stdc++.h> #ifdef _WIN32 #define getchar_unlocked getchar #endif // _WIN32 #define ll long long using namespace std; const int M[5] = {1035961709, 1035965659, 1027703951, 1015254979, 1015275553}; const ll P[5] = {31, 29, 37, 41, 43}; int hasz[5][3]; ll potega[5] = {1,1,1,1,1}; inline void fin(int &y) { register char c = 0; y = 0; while(c < 33)c = getchar_unlocked(); do{ y = (y << 1) + (y << 3) + c - '0'; c = getchar_unlocked(); }while(c > 33); } int n; main() { fin(n); n = 0; char val = getchar_unlocked(); while(val >= 'a' && val <= 'z') { val -= 'a' - 1; for(int i = 0; i < 3; i++) { hasz[i][1] = (hasz[i][1] * P[i] + val) % M[i]; hasz[i][2] = (hasz[i][2] + potega[i] * val) % M[i]; potega[i] = (potega[i] * P[i]) % M[i]; } val = 'a'; n++; val = getchar_unlocked(); } int res = 1; for(int i = 0; i < 3; i++) if(hasz[i][1] != hasz[i][2])res = 0; puts(res ? "TAK" : "NIE"); } |