#include <bits/stdc++.h>
using namespace std;
char a;
int p=31;
int h=31;
int mod=1e9+7;
int liczba;
int odw;
int n;
int j=20000000;
int hasz1,hasz2;
int pot(int a ,int b,int m)
{
if(b==0)
return 1;
if(b%2==0)
{
int s=pot(a,b/2,m);
return (1LL*s*s)%m;
}
return (1LL*a*pot(a,b-1,m))%m;
}
int main()
{
scanf("%d",&n);
liczba=pot(p,j,mod);
odw=pot(p,mod-2,mod);
while(scanf("%c",&a)!=EOF)
{
hasz1=(1LL*hasz1+1LL*(a-'a'+1)*h)%mod;
h=(1LL*h*p)%mod;
hasz2=(1LL*hasz2+1LL*(a-'a'+1)*liczba)%mod;
liczba=(1LL*liczba*odw)%mod;
j--;
}
hasz2=(1LL*hasz2*pot(pot(p,j,mod),mod-2,mod)+mod)%mod;
if(hasz1==hasz2)
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 | #include <bits/stdc++.h> using namespace std; char a; int p=31; int h=31; int mod=1e9+7; int liczba; int odw; int n; int j=20000000; int hasz1,hasz2; int pot(int a ,int b,int m) { if(b==0) return 1; if(b%2==0) { int s=pot(a,b/2,m); return (1LL*s*s)%m; } return (1LL*a*pot(a,b-1,m))%m; } int main() { scanf("%d",&n); liczba=pot(p,j,mod); odw=pot(p,mod-2,mod); while(scanf("%c",&a)!=EOF) { hasz1=(1LL*hasz1+1LL*(a-'a'+1)*h)%mod; h=(1LL*h*p)%mod; hasz2=(1LL*hasz2+1LL*(a-'a'+1)*liczba)%mod; liczba=(1LL*liczba*odw)%mod; j--; } hasz2=(1LL*hasz2*pot(pot(p,j,mod),mod-2,mod)+mod)%mod; if(hasz1==hasz2) printf("TAK\n"); else printf("NIE\n"); return 0; } |
English