#include<cstdio> #include<iostream> #include<algorithm> #define M 1000000007 #define M2 1000000009 #define M3 1000696969 using namespace std; typedef long long ll; int main(void){ int n ; ll x; ll l=0,l2=0,l3=0,b=1,b2=1,b3=1,od=0,od2=0,od3=0; cin.tie(); ios_base::sync_with_stdio(0); cin >> n; char ch; while(cin >> ch){ x = ch-96; //cout << x << " "; b%=M; l+=(x*b)%M;b*=31; l%=M; od*=31; od+=x; od%=M; b2%=M2; l2+=(x*b2)%M2;b2*=31; l2%=M2; od2*=31; od2+=x; od2%=M2; b3%=M3; l3+=(x*b3)%M3;b3*=31; l3%=M3; od3*=31; od3+=x; od3%=M3; } if(l==od && l2==od2 && l3==od3) cout << "TAK"; else cout << "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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include<cstdio> #include<iostream> #include<algorithm> #define M 1000000007 #define M2 1000000009 #define M3 1000696969 using namespace std; typedef long long ll; int main(void){ int n ; ll x; ll l=0,l2=0,l3=0,b=1,b2=1,b3=1,od=0,od2=0,od3=0; cin.tie(); ios_base::sync_with_stdio(0); cin >> n; char ch; while(cin >> ch){ x = ch-96; //cout << x << " "; b%=M; l+=(x*b)%M;b*=31; l%=M; od*=31; od+=x; od%=M; b2%=M2; l2+=(x*b2)%M2;b2*=31; l2%=M2; od2*=31; od2+=x; od2%=M2; b3%=M3; l3+=(x*b3)%M3;b3*=31; l3%=M3; od3*=31; od3+=x; od3%=M3; } if(l==od && l2==od2 && l3==od3) cout << "TAK"; else cout << "NIE"; return 0; } |