#include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef multiset<int> MI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ const int MAXN = 100000; const int MAXS = 20000000; string Temp; LL Pot[MAXN+5]; LL Hasz1, Hasz2; int To_Int(char a) { return (int)a - 96; } LL Potega(LL i) { if (i <= MAXN) return Pot[i]; if (i%2 == 0) { LL To = Potega(i/2); return (To * To) % MOD; } return (29 * Potega(i-1)) % MOD; } int main() { int n; scanf ("%d\n", &n); // ios_base::sync_with_stdio(false); // cin.tie(0); Pot[0] = 1; FOR (i, 1, MAXN) Pot[i] = (Pot[i-1] * 29) % MOD; char a; LL i = 1; LL j = MAXS + 5; LL Tera = 29; a = 'g'; while (true) { if (scanf("%c", &a) != 1) break; // if (i < 10) cout << (int)a << endl; if (a == 10) break; Temp += a; // printf ("%c : Pot[%d]\n", a, i); Hasz1 += (Tera * To_Int(a)) % MOD; Hasz1 %= MOD; Tera = (Tera * 29) % MOD; if (Temp.length() == MAXN) { LL Tera2 = Potega(j); FORD (k, Temp.length()-1, 0) { Hasz2 += (Tera2 * To_Int(Temp[k])) % MOD; Hasz2 %= MOD; Tera2 = (Tera2 * 29) % MOD; } Temp = ""; } i++; j--; } LL Tera2 = Potega(j+1); int Licz = 0; // printf ("* %d\n\n\n", j); FORD (k, Temp.length()-1, 0) { // printf ("%c : Pot[%d]\n", Temp[k], j+Licz); Licz++; Hasz2 += (Tera2 * To_Int(Temp[k])) % MOD; Hasz2 %= MOD; Tera2 = (Tera2 * 29) % MOD; } Temp = ""; Hasz1 = (Hasz1 * Potega(j)) % MOD; if (Hasz1 == Hasz2) cout << "TAK\n"; else cout << "NIE\n"; // cout << Hasz1 << "\n" << Hasz2; }
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef long double LD; typedef unsigned long long ULL; typedef vector<int> VI; typedef set<int> SI; typedef multiset<int> MI; typedef pair<int, int> PII; typedef vector<pair<int, int> > VPII; const int INF = 1000000001; const LD EPS = 1e-9; const int MOD = 1000000007; const LL LLINF = 1000000000000000001; //813437586 #define FOR(i, b, e) for(int i = b; i <= e; i++) #define FORD(i, b, e) for(int i = b; i >= e; i--) #define ALL(c) (c).begin(), (c).end() #define SIZE(x) ((int)(x).size()) #define VAR(v, n) auto v = (n) #define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define MP make_pair #define PB push_back #define ST first #define ND second #define GGL(x) "Case #" << x << ": " /*************************** END OF TEMPLATE ***************************/ const int MAXN = 100000; const int MAXS = 20000000; string Temp; LL Pot[MAXN+5]; LL Hasz1, Hasz2; int To_Int(char a) { return (int)a - 96; } LL Potega(LL i) { if (i <= MAXN) return Pot[i]; if (i%2 == 0) { LL To = Potega(i/2); return (To * To) % MOD; } return (29 * Potega(i-1)) % MOD; } int main() { int n; scanf ("%d\n", &n); // ios_base::sync_with_stdio(false); // cin.tie(0); Pot[0] = 1; FOR (i, 1, MAXN) Pot[i] = (Pot[i-1] * 29) % MOD; char a; LL i = 1; LL j = MAXS + 5; LL Tera = 29; a = 'g'; while (true) { if (scanf("%c", &a) != 1) break; // if (i < 10) cout << (int)a << endl; if (a == 10) break; Temp += a; // printf ("%c : Pot[%d]\n", a, i); Hasz1 += (Tera * To_Int(a)) % MOD; Hasz1 %= MOD; Tera = (Tera * 29) % MOD; if (Temp.length() == MAXN) { LL Tera2 = Potega(j); FORD (k, Temp.length()-1, 0) { Hasz2 += (Tera2 * To_Int(Temp[k])) % MOD; Hasz2 %= MOD; Tera2 = (Tera2 * 29) % MOD; } Temp = ""; } i++; j--; } LL Tera2 = Potega(j+1); int Licz = 0; // printf ("* %d\n\n\n", j); FORD (k, Temp.length()-1, 0) { // printf ("%c : Pot[%d]\n", Temp[k], j+Licz); Licz++; Hasz2 += (Tera2 * To_Int(Temp[k])) % MOD; Hasz2 %= MOD; Tera2 = (Tera2 * 29) % MOD; } Temp = ""; Hasz1 = (Hasz1 * Potega(j)) % MOD; if (Hasz1 == Hasz2) cout << "TAK\n"; else cout << "NIE\n"; // cout << Hasz1 << "\n" << Hasz2; } |