#include <iostream> using namespace std; typedef long long ll; const int ILE = 2; ll MOD[] = {999999929, 999999937, 1000000021}; ll base[] = {5003, 5009, 5011}; ll hasz[ILE][2]; ll curr[ILE]; inline ll fastmod(ll a, ll mod) { if(a >= mod) return a % mod; return a; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for(int i = 0; i < ILE; i++) { curr[i] = 1LL; } char c; int ile = 0; while(true) { if(ile == n && n != 0) break; if(!(cin >> c)) break; for(int j = 0; j < ILE; j++) { hasz[j][0] = fastmod(hasz[j][0] * base[j] + c, MOD[j]); hasz[j][1] = fastmod(hasz[j][1] + c * curr[j], MOD[j]); curr[j] = fastmod(curr[j] * base[j], MOD[j]); } ile++; } bool ans = true; for(int i = 0; i < ILE; i++) if(hasz[i][0] != hasz[i][1]) ans = false; if(ans) cout << "TAK\n"; else cout << "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 | #include <iostream> using namespace std; typedef long long ll; const int ILE = 2; ll MOD[] = {999999929, 999999937, 1000000021}; ll base[] = {5003, 5009, 5011}; ll hasz[ILE][2]; ll curr[ILE]; inline ll fastmod(ll a, ll mod) { if(a >= mod) return a % mod; return a; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for(int i = 0; i < ILE; i++) { curr[i] = 1LL; } char c; int ile = 0; while(true) { if(ile == n && n != 0) break; if(!(cin >> c)) break; for(int j = 0; j < ILE; j++) { hasz[j][0] = fastmod(hasz[j][0] * base[j] + c, MOD[j]); hasz[j][1] = fastmod(hasz[j][1] + c * curr[j], MOD[j]); curr[j] = fastmod(curr[j] * base[j], MOD[j]); } ile++; } bool ans = true; for(int i = 0; i < ILE; i++) if(hasz[i][0] != hasz[i][1]) ans = false; if(ans) cout << "TAK\n"; else cout << "NIE\n"; return 0; } |