#include <bits/stdc++.h> using namespace std; int n, c; long long h1, h2, p=2137, m=213721373; long long pot(long long a, long long b) { if(b==0) { return 1; } if(b==1) { return a%m; } if(b&1) { return pot((a%m)*(a%m)%m, b/2)%m*(a%m)%m; } return pot((a%m)*(a%m)%m, b/2)%m; } int main() { scanf("%d", &n); n=1; //printf("%c %c", char(97), char(122)); c=getchar(); while(c<97 || c>122) { c=getchar(); } while(c>=97 && c<=122) { c-=96; h1+=c*pot(p, n); h1%=m; h2+=c; h2%=m; h2*=p; h2%=m; c=getchar(); ++n; } if(h1==h2) { printf("TAK\n"); } else { printf("NIE\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 | #include <bits/stdc++.h> using namespace std; int n, c; long long h1, h2, p=2137, m=213721373; long long pot(long long a, long long b) { if(b==0) { return 1; } if(b==1) { return a%m; } if(b&1) { return pot((a%m)*(a%m)%m, b/2)%m*(a%m)%m; } return pot((a%m)*(a%m)%m, b/2)%m; } int main() { scanf("%d", &n); n=1; //printf("%c %c", char(97), char(122)); c=getchar(); while(c<97 || c>122) { c=getchar(); } while(c>=97 && c<=122) { c-=96; h1+=c*pot(p, n); h1%=m; h2+=c; h2%=m; h2*=p; h2%=m; c=getchar(); ++n; } if(h1==h2) { printf("TAK\n"); } else { printf("NIE\n"); } return 0; } |