#include<iostream> using namespace std; int main() { ios_base::sync_with_stdio(0); unsigned long long P=1000000007; unsigned long long L[2]; unsigned long long R[2]; unsigned long long X[3] = {1610612741, 805206457, 402652189}; unsigned long long D[2]; int n; char x; for(int i=0;i<2;i++) { L[i]=R[i]=0; D[i]=1; } cin >>n; while(cin >>x) { if(x < 'a' || x> 'z') { continue; } for(int i=0;i<2;i++) { D[i]=(D[i]*X[i])%P; R[i]=(R[i]+((D[i]*x)%P))%P; L[i]=(L[i]+x)%P; L[i]=(L[i]*X[i])%P; } } bool yes = true; for(int i=0;i<2;i++) { if(L[i]!=R[i]) { yes = false; } } cout << (yes ? "TAK" : "NIE")<<endl; 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 | #include<iostream> using namespace std; int main() { ios_base::sync_with_stdio(0); unsigned long long P=1000000007; unsigned long long L[2]; unsigned long long R[2]; unsigned long long X[3] = {1610612741, 805206457, 402652189}; unsigned long long D[2]; int n; char x; for(int i=0;i<2;i++) { L[i]=R[i]=0; D[i]=1; } cin >>n; while(cin >>x) { if(x < 'a' || x> 'z') { continue; } for(int i=0;i<2;i++) { D[i]=(D[i]*X[i])%P; R[i]=(R[i]+((D[i]*x)%P))%P; L[i]=(L[i]+x)%P; L[i]=(L[i]*X[i])%P; } } bool yes = true; for(int i=0;i<2;i++) { if(L[i]!=R[i]) { yes = false; } } cout << (yes ? "TAK" : "NIE")<<endl; return 0; } |