#include<bits/stdc++.h> #define ull unsigned long long #define pb push_back #define MP make_pair using namespace std; const ull MOD_1 = 1e9 + 7; const ull MOD_2 = 1e9 + 9; const ull p_1 = 137; const ull p_2 = 1009; ull pot(ull num, ull pow, ull MOD){ if(pow == 0) return 1; ull aux = pot(num, pow/2, MOD); if(pow % 2 == 0) return (aux * aux) % MOD; return (aux * aux * num) % MOD; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ull h_1_b = 0, h_1_e = 0, h_2_b = 0, h_2_e = 0; ull n; cin >> n; char z; ull ob_1 = 1, ob_2 = 1; while(cin >> z){ ull w = z; h_1_b += (w * ob_1) % MOD_1; ///cout << "Dodaje do h_1_b " << w << " * " << pot(p_1, n, MOD_1) << endl; h_2_b += (w * ob_2) % MOD_2; h_1_b %= MOD_1; h_2_b %= MOD_2; h_1_e = (h_1_e * p_1 + w) % MOD_1; h_2_e = (h_2_e * p_2 + w) % MOD_2; ob_1 = (ob_1 * p_1) % MOD_1; ob_2 = (ob_2 * p_2) % MOD_2; //cout << h_1_b << " vs " << h_1_e << endl; //cout << h_2_b << " vs " << h_2_e << endl << endl; } if(h_1_b == h_1_e && h_2_b == h_2_e) cout << "TAK"; else cout << "NIE"; 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 | #include<bits/stdc++.h> #define ull unsigned long long #define pb push_back #define MP make_pair using namespace std; const ull MOD_1 = 1e9 + 7; const ull MOD_2 = 1e9 + 9; const ull p_1 = 137; const ull p_2 = 1009; ull pot(ull num, ull pow, ull MOD){ if(pow == 0) return 1; ull aux = pot(num, pow/2, MOD); if(pow % 2 == 0) return (aux * aux) % MOD; return (aux * aux * num) % MOD; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ull h_1_b = 0, h_1_e = 0, h_2_b = 0, h_2_e = 0; ull n; cin >> n; char z; ull ob_1 = 1, ob_2 = 1; while(cin >> z){ ull w = z; h_1_b += (w * ob_1) % MOD_1; ///cout << "Dodaje do h_1_b " << w << " * " << pot(p_1, n, MOD_1) << endl; h_2_b += (w * ob_2) % MOD_2; h_1_b %= MOD_1; h_2_b %= MOD_2; h_1_e = (h_1_e * p_1 + w) % MOD_1; h_2_e = (h_2_e * p_2 + w) % MOD_2; ob_1 = (ob_1 * p_1) % MOD_1; ob_2 = (ob_2 * p_2) % MOD_2; //cout << h_1_b << " vs " << h_1_e << endl; //cout << h_2_b << " vs " << h_2_e << endl << endl; } if(h_1_b == h_1_e && h_2_b == h_2_e) cout << "TAK"; else cout << "NIE"; return 0; } |