#include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long p = 313; long long p2 = 317; long long modulo = static_cast<long long>(1e9) + 6969; long long modulo2 = static_cast <long long>(1e9) + 7; char c; long long h1 = 0; long long h2 = 0; long long h3 = 0; long long h4 = 0; long long pot = 1; long long pot2 = 1; while (cin >> c){ h1 = ((h1*p)%modulo + static_cast<int>(c))%modulo; h2 = (h2 + (static_cast <long long> (c)*pot)%modulo)%modulo; h3 = ((h3*p2)%modulo2 + static_cast<int>(c))%modulo2; h4 = (h4 + (static_cast<long long>(c)*pot2)%modulo2)%modulo2; pot = (pot*p)%modulo; pot2 = (pot2*p2)%modulo2; assert(h1 >= 0); assert(h2 >= 0); assert(h3 >= 0); assert(h4 >=0); } if (h1 == h2 && h3 == h4){ cout << "TAK\n"; } else{ cout << "NIE\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 | #include <bits/stdc++.h> using namespace std; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long p = 313; long long p2 = 317; long long modulo = static_cast<long long>(1e9) + 6969; long long modulo2 = static_cast <long long>(1e9) + 7; char c; long long h1 = 0; long long h2 = 0; long long h3 = 0; long long h4 = 0; long long pot = 1; long long pot2 = 1; while (cin >> c){ h1 = ((h1*p)%modulo + static_cast<int>(c))%modulo; h2 = (h2 + (static_cast <long long> (c)*pot)%modulo)%modulo; h3 = ((h3*p2)%modulo2 + static_cast<int>(c))%modulo2; h4 = (h4 + (static_cast<long long>(c)*pot2)%modulo2)%modulo2; pot = (pot*p)%modulo; pot2 = (pot2*p2)%modulo2; assert(h1 >= 0); assert(h2 >= 0); assert(h3 >= 0); assert(h4 >=0); } if (h1 == h2 && h3 == h4){ cout << "TAK\n"; } else{ cout << "NIE\n"; } } |