#include<iostream> using namespace std; int x; char c; const int base = 2137, mod = ((int)(1e9)) + 9; long long hash1, hash2,it; long long fastMod(long long a, long long b){ if(a >= b) return a % b; else return a; } long long fastPow(long long a, long long b){ long long w = 1; while(b > 0){ if(b % 2 == 1){ w *= a; w = fastMod(w,mod); } a*=a; a = fastMod(a,mod); b /= 2; } return w; } int main(){ ios_base::sync_with_stdio( 0 ); cin.tie( 0 ); cin>>x; it = 0; while(cin>>c){ if(it == 0){ hash1 = int(c); hash2 = int(c); it++; } else { hash1 += int(c) * fastMod(fastPow(base, it),mod); hash1 = fastMod(hash1, mod); hash2 *= base; hash2 = fastMod(hash2, mod); hash2 += int(c); hash2 = fastMod(hash2, mod); it++; } } if( hash1 == hash2 ) cout<<"TAK"; else cout<<"NIE"; }
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<iostream> using namespace std; int x; char c; const int base = 2137, mod = ((int)(1e9)) + 9; long long hash1, hash2,it; long long fastMod(long long a, long long b){ if(a >= b) return a % b; else return a; } long long fastPow(long long a, long long b){ long long w = 1; while(b > 0){ if(b % 2 == 1){ w *= a; w = fastMod(w,mod); } a*=a; a = fastMod(a,mod); b /= 2; } return w; } int main(){ ios_base::sync_with_stdio( 0 ); cin.tie( 0 ); cin>>x; it = 0; while(cin>>c){ if(it == 0){ hash1 = int(c); hash2 = int(c); it++; } else { hash1 += int(c) * fastMod(fastPow(base, it),mod); hash1 = fastMod(hash1, mod); hash2 *= base; hash2 = fastMod(hash2, mod); hash2 += int(c); hash2 = fastMod(hash2, mod); it++; } } if( hash1 == hash2 ) cout<<"TAK"; else cout<<"NIE"; } |