#include<cstdio> #define ILOSC_HASZY 4 unsigned long long hasz_zwykly, odw_hasz_zwykly; unsigned long long hasz_z_mod[ILOSC_HASZY], odw_hasz_z_mod[ILOSC_HASZY]; int main() { unsigned long long mod[ILOSC_HASZY] = {1010000021, 1010000029, 1010000051, 1010000053}; //1010000077, 1010000083, 1010000111, 1010000113, 1010000153, 1010000203}; unsigned long long pot[ILOSC_HASZY] = {3000017, 3000029, 3000047, 3000061};//, 3000073, 3000077, 3000089, 3000103, 3000131, 3000133}; unsigned long long pot_iter[ILOSC_HASZY]; for(int i = 0; i < ILOSC_HASZY; i++) { pot_iter[i] = 1; } unsigned long long p = 5000011, p_iter = 1; int n; char c; while((c = getchar()) != '\n') {} while(1) { c = getchar(); if(c == EOF || c < 'a' || c > 'z') { break; } hasz_zwykly = (hasz_zwykly * p + c); for(int i = 0; i < ILOSC_HASZY; i++) { hasz_z_mod[i] = (hasz_z_mod[i] * pot[i] + c) % mod[i]; } odw_hasz_zwykly = (odw_hasz_zwykly + p_iter * c); for(int i = 0; i < ILOSC_HASZY; i++) { odw_hasz_z_mod[i] = (odw_hasz_z_mod[i] + pot_iter[i] * c) % mod[i]; } p_iter = p_iter * p; for(int i = 0; i < ILOSC_HASZY; i++) { pot_iter[i] = (pot_iter[i] * pot[i]) % mod[i]; } } if(hasz_zwykly != odw_hasz_zwykly) { printf("NIE\n"); return 0; } for(int i = 0; i < ILOSC_HASZY; i++) { if(hasz_z_mod[i] != odw_hasz_z_mod[i]) { printf("NIE\n"); return 0; } } printf("TAK\n"); 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 50 51 52 53 54 55 56 57 58 59 60 61 | #include<cstdio> #define ILOSC_HASZY 4 unsigned long long hasz_zwykly, odw_hasz_zwykly; unsigned long long hasz_z_mod[ILOSC_HASZY], odw_hasz_z_mod[ILOSC_HASZY]; int main() { unsigned long long mod[ILOSC_HASZY] = {1010000021, 1010000029, 1010000051, 1010000053}; //1010000077, 1010000083, 1010000111, 1010000113, 1010000153, 1010000203}; unsigned long long pot[ILOSC_HASZY] = {3000017, 3000029, 3000047, 3000061};//, 3000073, 3000077, 3000089, 3000103, 3000131, 3000133}; unsigned long long pot_iter[ILOSC_HASZY]; for(int i = 0; i < ILOSC_HASZY; i++) { pot_iter[i] = 1; } unsigned long long p = 5000011, p_iter = 1; int n; char c; while((c = getchar()) != '\n') {} while(1) { c = getchar(); if(c == EOF || c < 'a' || c > 'z') { break; } hasz_zwykly = (hasz_zwykly * p + c); for(int i = 0; i < ILOSC_HASZY; i++) { hasz_z_mod[i] = (hasz_z_mod[i] * pot[i] + c) % mod[i]; } odw_hasz_zwykly = (odw_hasz_zwykly + p_iter * c); for(int i = 0; i < ILOSC_HASZY; i++) { odw_hasz_z_mod[i] = (odw_hasz_z_mod[i] + pot_iter[i] * c) % mod[i]; } p_iter = p_iter * p; for(int i = 0; i < ILOSC_HASZY; i++) { pot_iter[i] = (pot_iter[i] * pot[i]) % mod[i]; } } if(hasz_zwykly != odw_hasz_zwykly) { printf("NIE\n"); return 0; } for(int i = 0; i < ILOSC_HASZY; i++) { if(hasz_z_mod[i] != odw_hasz_z_mod[i]) { printf("NIE\n"); return 0; } } printf("TAK\n"); return 0; } |