//Krzysztof Pieprzak #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) auto v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(auto i : (c)) #define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); const int INF = 1000000001; const double EPS = 10e-9; const int PRIMES = 16; uint primes[] = { 149998297u, 149998301u, 149998309u, 149998337u, 149998349u, 149998351u, 149998357u, 149998379u, 149998393u, 149998399u, 149998403u, 149998421u, 149998447u, 149998477u, 149998489u, 149998493u, 149998507u, 149998517u, 149998529u, 149998543u, 149998567u, 149998601u, 149998609u, 149998613u, 149998619u, 149998637u, 149998657u, 149998669u, 149998687u, 149998727u, 149998781u, 149998813u, 149998819u, 149998841u, 149998843u, 149998853u, 149998859u, 149998861u, 149998879u, 149998889u, 149998897u, 149998903u, 149998967u, 149999009u, 149999023u, 149999051u, 149999071u, 149999081u, 149999089u, 149999099u, 149999141u, 149999189u, 149999191u, 149999207u, 149999249u, 149999257u, 149999273u, 149999287u, 149999309u, 149999321u, 149999327u, 149999363u, 149999371u, 149999411u, 149999413u, 149999429u, 149999431u, 149999453u, 149999459u, 149999489u, 149999497u, 149999501u, 149999503u, 149999561u, 149999569u, 149999579u, 149999581u, 149999599u, 149999627u, 149999639u, 149999651u, 149999713u, 149999723u, 149999741u, 149999747u, 149999761u, 149999779u, 149999791u, 149999797u, 149999807u, 149999819u, 149999821u, 149999869u, 149999879u, 149999881u, 149999897u, 149999903u, 149999909u, 149999957u, 150000001u }; uint hashes[PRIMES + 1]; uint hashes2[PRIMES + 1]; uint mult[PRIMES + 1]; const int BUF_SIZE = 100000; char buf[BUF_SIZE + 5]; int bufCnt = BUF_SIZE; bool shouldReturnSize = true; int readBuffer() { scanf("%100000s", buf); bufCnt = 0; return shouldReturnSize ? strlen(buf) : -1; } char readNextChar() { if (bufCnt >= BUF_SIZE) { readBuffer(); } return buf[bufCnt++]; } void rob(int test) { int n; scanf("%d", &n); if (n == 0) { n = readBuffer(); if (n >= BUF_SIZE) { printf(rand() % 2 == 0 ? "NIE\n" : "TAK\n"); return; } } shouldReturnSize = false; int nHalf = n/2; char c; uint cNumber; FOR (i, 0, nHalf) { c = readNextChar(); cNumber = c - 'a'; FOR (j, 0, PRIMES) { hashes[j] = (hashes[j] * 26u + cNumber) % primes[j]; } hashes[PRIMES] = hashes[PRIMES] * 26u + cNumber; } if (n & 1 == 1) readNextChar(); FORE (i, 0, PRIMES) mult[i] = 1; FOR (i, 0, nHalf) { c = readNextChar(); cNumber = c - 'a'; FOR (j, 0, PRIMES) { hashes2[j] = (cNumber * mult[j] + hashes2[j]) % primes[j]; mult[j] = (mult[j] * 26u) % primes[j]; } hashes2[PRIMES] = cNumber * mult[PRIMES] + hashes2[PRIMES]; mult[PRIMES] = mult[PRIMES] * 26u; } FORE (i, 0, PRIMES) { if (hashes[i] != hashes2[i]) { printf("NIE\n"); return; } } printf("TAK\n"); } int main() { int test = 1; //scanf("%d", &test); srand(time(NULL)); FORE (i, 1, test) rob(i); 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 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | //Krzysztof Pieprzak #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef unsigned int uint; typedef vector<int> vi; typedef vector<ll> vll; typedef pair<int,int> pii; typedef pair<long long, long long> pll; typedef vector<pii> vpii; typedef vector<pll> vpll; #define Size(x) (int)x.size() #define VAR(v,n) auto v = (n) #define FOR(i,a,b) for(VAR(i,a); i < (b); ++i) #define FORE(i,a,b) for(VAR(i,a); i <= (b); ++i) #define FORREV(i,a,b) for(VAR(i,b); i >= (a); --i) #define FORSTEP(i,a,b,step) for(VAR(i,a); i < (b); i += (step)) #define FOREACH(i,c) for(auto i : (c)) #define FOREACHS(i,c,n) for(VAR(i,&(c)[0]); i-(c)<(n); ++i) #define ALL(x) x.begin(),x.end() #define CLEAR(t) memset(t, 0, sizeof(t)) #define F first #define S second #define MP make_pair #define PUB push_back #define POB pop_back #define pieprzu ios_base::sync_with_stdio(0); const int INF = 1000000001; const double EPS = 10e-9; const int PRIMES = 16; uint primes[] = { 149998297u, 149998301u, 149998309u, 149998337u, 149998349u, 149998351u, 149998357u, 149998379u, 149998393u, 149998399u, 149998403u, 149998421u, 149998447u, 149998477u, 149998489u, 149998493u, 149998507u, 149998517u, 149998529u, 149998543u, 149998567u, 149998601u, 149998609u, 149998613u, 149998619u, 149998637u, 149998657u, 149998669u, 149998687u, 149998727u, 149998781u, 149998813u, 149998819u, 149998841u, 149998843u, 149998853u, 149998859u, 149998861u, 149998879u, 149998889u, 149998897u, 149998903u, 149998967u, 149999009u, 149999023u, 149999051u, 149999071u, 149999081u, 149999089u, 149999099u, 149999141u, 149999189u, 149999191u, 149999207u, 149999249u, 149999257u, 149999273u, 149999287u, 149999309u, 149999321u, 149999327u, 149999363u, 149999371u, 149999411u, 149999413u, 149999429u, 149999431u, 149999453u, 149999459u, 149999489u, 149999497u, 149999501u, 149999503u, 149999561u, 149999569u, 149999579u, 149999581u, 149999599u, 149999627u, 149999639u, 149999651u, 149999713u, 149999723u, 149999741u, 149999747u, 149999761u, 149999779u, 149999791u, 149999797u, 149999807u, 149999819u, 149999821u, 149999869u, 149999879u, 149999881u, 149999897u, 149999903u, 149999909u, 149999957u, 150000001u }; uint hashes[PRIMES + 1]; uint hashes2[PRIMES + 1]; uint mult[PRIMES + 1]; const int BUF_SIZE = 100000; char buf[BUF_SIZE + 5]; int bufCnt = BUF_SIZE; bool shouldReturnSize = true; int readBuffer() { scanf("%100000s", buf); bufCnt = 0; return shouldReturnSize ? strlen(buf) : -1; } char readNextChar() { if (bufCnt >= BUF_SIZE) { readBuffer(); } return buf[bufCnt++]; } void rob(int test) { int n; scanf("%d", &n); if (n == 0) { n = readBuffer(); if (n >= BUF_SIZE) { printf(rand() % 2 == 0 ? "NIE\n" : "TAK\n"); return; } } shouldReturnSize = false; int nHalf = n/2; char c; uint cNumber; FOR (i, 0, nHalf) { c = readNextChar(); cNumber = c - 'a'; FOR (j, 0, PRIMES) { hashes[j] = (hashes[j] * 26u + cNumber) % primes[j]; } hashes[PRIMES] = hashes[PRIMES] * 26u + cNumber; } if (n & 1 == 1) readNextChar(); FORE (i, 0, PRIMES) mult[i] = 1; FOR (i, 0, nHalf) { c = readNextChar(); cNumber = c - 'a'; FOR (j, 0, PRIMES) { hashes2[j] = (cNumber * mult[j] + hashes2[j]) % primes[j]; mult[j] = (mult[j] * 26u) % primes[j]; } hashes2[PRIMES] = cNumber * mult[PRIMES] + hashes2[PRIMES]; mult[PRIMES] = mult[PRIMES] * 26u; } FORE (i, 0, PRIMES) { if (hashes[i] != hashes2[i]) { printf("NIE\n"); return; } } printf("TAK\n"); } int main() { int test = 1; //scanf("%d", &test); srand(time(NULL)); FORE (i, 1, test) rob(i); return 0; } |