#include <iostream> using namespace std; typedef long long LL; #define FOR(i, b, e) for (LL i = b; i <= e; i++) #define FORD(i, b, e) for (LL i = b; i >= e; i--) #define SIZE(x) ((LL)(x).size()) #define BOOST ios_base::sync_with_stdio(false); cin.tie(0) #define MP make_pair #define PB push_back #define ST first #define ND second /*************************** END OF TEMPLATE ***************************/ const LL MOD = 1000000103; LL power(LL a, LL p) { LL r = 1; while(p > 0) { if(p%2 == 1) r = (r * a) % MOD; a = (a * a) % MOD; p /= 2; } return r; } const LL maxn = 200000000; LL startn = maxn + 42; const LL base = 31; LL n = 0; LL baser; int main() { BOOST; cin >> n; n = 0; char c; LL p = base; LL p2 = power(base, startn); baser = power(base, MOD - 2); LL h1 = 0, h2 = 0; while(cin >> c) { n++; LL a = c - 'a'; h1 = (h1 + p * a) % MOD; h2 = (h2 + p2 * a) % MOD; p = (p * base) % MOD; p2 = (p2 * baser) % MOD; } //cout << n << endl; h1 = (h1 * power(base, startn - n)) % MOD; //cout << h1 << ' ' << h2 << endl; if(h1 == h2) 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 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 75 76 | #include <iostream> using namespace std; typedef long long LL; #define FOR(i, b, e) for (LL i = b; i <= e; i++) #define FORD(i, b, e) for (LL i = b; i >= e; i--) #define SIZE(x) ((LL)(x).size()) #define BOOST ios_base::sync_with_stdio(false); cin.tie(0) #define MP make_pair #define PB push_back #define ST first #define ND second /*************************** END OF TEMPLATE ***************************/ const LL MOD = 1000000103; LL power(LL a, LL p) { LL r = 1; while(p > 0) { if(p%2 == 1) r = (r * a) % MOD; a = (a * a) % MOD; p /= 2; } return r; } const LL maxn = 200000000; LL startn = maxn + 42; const LL base = 31; LL n = 0; LL baser; int main() { BOOST; cin >> n; n = 0; char c; LL p = base; LL p2 = power(base, startn); baser = power(base, MOD - 2); LL h1 = 0, h2 = 0; while(cin >> c) { n++; LL a = c - 'a'; h1 = (h1 + p * a) % MOD; h2 = (h2 + p2 * a) % MOD; p = (p * base) % MOD; p2 = (p2 * baser) % MOD; } //cout << n << endl; h1 = (h1 * power(base, startn - n)) % MOD; //cout << h1 << ' ' << h2 << endl; if(h1 == h2) cout << "TAK\n"; else cout << "NIE\n"; } |