#include <cstdio> const long long POWER = 33ll; const long long P = 9576890767ll; // duza liczba pierwsza int main() { long long h1 = 0ll, h2 = 0ll, mul = 1ll; long long x; char c; scanf("%lld\n", &x); while (scanf("%c", &c) > 0 && c != '\n') { h1 = h1 + mul * c; if (h1 > P) { h1 %= P; } h2 = h2 * POWER + c; if (h2 > P) { h2 %= P; } mul = mul * POWER; if (mul > P) { mul %= P; } } printf("%s", h1 == h2 ? "TAK" : "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 | #include <cstdio> const long long POWER = 33ll; const long long P = 9576890767ll; // duza liczba pierwsza int main() { long long h1 = 0ll, h2 = 0ll, mul = 1ll; long long x; char c; scanf("%lld\n", &x); while (scanf("%c", &c) > 0 && c != '\n') { h1 = h1 + mul * c; if (h1 > P) { h1 %= P; } h2 = h2 * POWER + c; if (h2 > P) { h2 %= P; } mul = mul * POWER; if (mul > P) { mul %= P; } } printf("%s", h1 == h2 ? "TAK" : "NIE"); return 0; } |