#include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 2e7 + 5; const int INF = 1e9 + 7; const ll MOD = 1e9 + 696969; const ll P = 199; ll hashBegin = 0, hashEnd = 0; ll x; ll charToLong(char sign) { return (ll)(sign - 'a'); } ll powerModulo(ll pp, ll k, ll mod) { if (k == 0) { return 1; } if (k % 2 == 1) { return (powerModulo(pp, k - 1, mod) * pp) % mod; } ll h = powerModulo(pp, k / 2, mod); return (h * h) % mod; } void solve() { int n, i = 1, j = MAXN; char c; scanf("%d\n", &n); scanf("%c", &c); while (c != '\n' && c != EOF) { n = charToLong(c); hashBegin += n * powerModulo(P, i, MOD); hashEnd += n * powerModulo(P, j, MOD); hashBegin %= MOD; hashEnd %= MOD; i++; j--; scanf("%c", &c); } hashBegin *= powerModulo(P, MAXN - i + 1, MOD); hashBegin %= MOD; if (hashBegin == hashEnd) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); 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 59 60 61 62 63 64 65 66 67 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define MP make_pair #define F first #define S second typedef long long ll; typedef vector<int> vi; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 2e7 + 5; const int INF = 1e9 + 7; const ll MOD = 1e9 + 696969; const ll P = 199; ll hashBegin = 0, hashEnd = 0; ll x; ll charToLong(char sign) { return (ll)(sign - 'a'); } ll powerModulo(ll pp, ll k, ll mod) { if (k == 0) { return 1; } if (k % 2 == 1) { return (powerModulo(pp, k - 1, mod) * pp) % mod; } ll h = powerModulo(pp, k / 2, mod); return (h * h) % mod; } void solve() { int n, i = 1, j = MAXN; char c; scanf("%d\n", &n); scanf("%c", &c); while (c != '\n' && c != EOF) { n = charToLong(c); hashBegin += n * powerModulo(P, i, MOD); hashEnd += n * powerModulo(P, j, MOD); hashBegin %= MOD; hashEnd %= MOD; i++; j--; scanf("%c", &c); } hashBegin *= powerModulo(P, MAXN - i + 1, MOD); hashBegin %= MOD; if (hashBegin == hashEnd) { cout << "TAK\n"; } else { cout << "NIE\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); return 0; } |