#include <vector> #include <iostream> using namespace std; using ll = unsigned long long int; constexpr ll mod1 = 1000000007; const vector<ll> modulos = {1000000007 ,1000000009,1000696969 }; const vector<ll> base = {313}; const ll startPower = 30000000; ll qpow1(ll a, int b) { if (b ==0) { return 1; } ll t = qpow1(a, b / 2); t *= t; t %= mod1; if (b&1) { return (t * a)%mod1; } else { return t; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<ll> hashes(3); vector<ll> backwardHash(3); vector<ll> curPow(3, base[0]); vector<ll> BackPow(3, 0); ll div1 = qpow1(base[0], modulos[0] - 2); for (int i = 0; i < 3; i++) { BackPow[i] = qpow1(base[0], startPower); } int count = 0; char c; while (cin >> c ) { count++; hashes[0] += c * curPow[0]; hashes[0] %= mod1; curPow[0] = (curPow[0] * base[0]) % mod1; backwardHash[0] += c * BackPow[0]; backwardHash[0] %= mod1; BackPow[0] = (BackPow[0] * div1) % mod1; } ll toRemove = qpow1(base[0], startPower - count); ll backWyn = (backwardHash[0] * qpow1(BackPow[0], mod1-2)) % mod1; if (backWyn==hashes[0]) { cout << "TAK" << endl; } else { cout << "NIE" <<endl; } 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 68 69 70 71 72 73 74 | #include <vector> #include <iostream> using namespace std; using ll = unsigned long long int; constexpr ll mod1 = 1000000007; const vector<ll> modulos = {1000000007 ,1000000009,1000696969 }; const vector<ll> base = {313}; const ll startPower = 30000000; ll qpow1(ll a, int b) { if (b ==0) { return 1; } ll t = qpow1(a, b / 2); t *= t; t %= mod1; if (b&1) { return (t * a)%mod1; } else { return t; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<ll> hashes(3); vector<ll> backwardHash(3); vector<ll> curPow(3, base[0]); vector<ll> BackPow(3, 0); ll div1 = qpow1(base[0], modulos[0] - 2); for (int i = 0; i < 3; i++) { BackPow[i] = qpow1(base[0], startPower); } int count = 0; char c; while (cin >> c ) { count++; hashes[0] += c * curPow[0]; hashes[0] %= mod1; curPow[0] = (curPow[0] * base[0]) % mod1; backwardHash[0] += c * BackPow[0]; backwardHash[0] %= mod1; BackPow[0] = (BackPow[0] * div1) % mod1; } ll toRemove = qpow1(base[0], startPower - count); ll backWyn = (backwardHash[0] * qpow1(BackPow[0], mod1-2)) % mod1; if (backWyn==hashes[0]) { cout << "TAK" << endl; } else { cout << "NIE" <<endl; } return 0; } |