#include<iostream> using namespace std; int main() { long long constexpr BASE1 = 31, BASE2 = 97; long long constexpr MOD = 1e9 + 7; int heh; cin >> heh; long long hash1 = 0; long long rhash1 = 0; long long hash2 = 0; long long rhash2 = 0; long long power1 = 1; long long power2 = 1; char in; while (cin >> in) { hash1 *= BASE1; hash2 *= BASE2; long long pom1 = in + 1; long long pom2 = in + 1; pom1 *= power1; pom2 *= power2; pom1 %= MOD; pom2 %= MOD; hash1 += in + 1; hash2 += in + 1; hash1 %= MOD; hash2 %= MOD; rhash1 += pom1; rhash2 += pom2; rhash1 %= MOD; rhash2 %= MOD; power1 *= BASE1; power2 *= BASE2; power1 %= MOD; power2 %= MOD; } if (hash1 != rhash1 or hash2 != rhash2) { cout << "NIE"; return 0; } cout << "TAK"; 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 | #include<iostream> using namespace std; int main() { long long constexpr BASE1 = 31, BASE2 = 97; long long constexpr MOD = 1e9 + 7; int heh; cin >> heh; long long hash1 = 0; long long rhash1 = 0; long long hash2 = 0; long long rhash2 = 0; long long power1 = 1; long long power2 = 1; char in; while (cin >> in) { hash1 *= BASE1; hash2 *= BASE2; long long pom1 = in + 1; long long pom2 = in + 1; pom1 *= power1; pom2 *= power2; pom1 %= MOD; pom2 %= MOD; hash1 += in + 1; hash2 += in + 1; hash1 %= MOD; hash2 %= MOD; rhash1 += pom1; rhash2 += pom2; rhash1 %= MOD; rhash2 %= MOD; power1 *= BASE1; power2 *= BASE2; power1 %= MOD; power2 %= MOD; } if (hash1 != rhash1 or hash2 != rhash2) { cout << "NIE"; return 0; } cout << "TAK"; return 0; } |