#include <iostream> #include <vector> using namespace std; bool isPrime(int x) { for(int i = 2; i * i <= x; i += 1) if(x % i == 0) return false; return true; } int getPrime(int start) { int where = start + rand() % int(5e7); while(not isPrime(where)) where += 1; return where; }; int main() { srand(time(0)); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> p(2, 0), b(2, 0), mod(2, 0), straight(2, 0), reversed(2, 0); b[0] = 29, b[1] = 31; p[0] = p[1] = 1; mod[0] = getPrime(int(1e8)), mod[1] = getPrime(int(2e8)); char c; while(cin >> c) { c -= 'a'; for(int it = 0; it < 2; it += 1) { straight[it] += (1LL * p[it] * c) % mod[it]; if(straight[it] >= mod[it]) straight[it] -= mod[it]; reversed[it] = (1LL * reversed[it] * b[it] + c) % mod[it]; p[it] = 1LL * p[it] * b[it] % mod[it]; } } bool ok = true; for(int i = 0; i < 2; i += 1) { if(straight[i] != reversed[i]) { ok = false; } } if(not ok) { cout << "NIE\n"; } else { cout << "TAK\n"; } }
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 | #include <iostream> #include <vector> using namespace std; bool isPrime(int x) { for(int i = 2; i * i <= x; i += 1) if(x % i == 0) return false; return true; } int getPrime(int start) { int where = start + rand() % int(5e7); while(not isPrime(where)) where += 1; return where; }; int main() { srand(time(0)); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> p(2, 0), b(2, 0), mod(2, 0), straight(2, 0), reversed(2, 0); b[0] = 29, b[1] = 31; p[0] = p[1] = 1; mod[0] = getPrime(int(1e8)), mod[1] = getPrime(int(2e8)); char c; while(cin >> c) { c -= 'a'; for(int it = 0; it < 2; it += 1) { straight[it] += (1LL * p[it] * c) % mod[it]; if(straight[it] >= mod[it]) straight[it] -= mod[it]; reversed[it] = (1LL * reversed[it] * b[it] + c) % mod[it]; p[it] = 1LL * p[it] * b[it] % mod[it]; } } bool ok = true; for(int i = 0; i < 2; i += 1) { if(straight[i] != reversed[i]) { ok = false; } } if(not ok) { cout << "NIE\n"; } else { cout << "TAK\n"; } } |