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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
#define NDEBUG
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <unistd.h>
using namespace std;
#define TRACE(x) cerr<<"# "#x<<endl;
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<endl;
typedef unsigned long long ULL;
typedef long long LL;
const int INF = 0x7FFFFFFF;


#define USE_STATIC_MEMORY
#ifdef USE_STATIC_MEMORY
void* allocateMemory(unsigned size);
#endif

class IO {
        #define IO_DEBUG_ENABLED 0
    public:
        IO() {
            buffer = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(BUFFER_SIZE))
                        #endif
                    char[ BUFFER_SIZE ];
            bufferOut = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(BUFFER_SIZE))
                        #endif
                    char[ BUFFER_SIZE ];
        }
        ~IO() {
            /* delete [] buffer; */
            flush();
        }
        inline void readChar(char * const c) {
            readIntoBuffer();
            *c = buffer[ bufferIndex ];
            ++bufferIndex;
        }
        void readUInt(unsigned int * const out) {
            unsigned int res = 0;
            char c;
            skipWC();
            readChar(&c);
            while( (c >= '0') && (c <= '9') ) {
                res *= 10;
                res += c - '0';
                readChar(&c);
            }
            *out = res;
        }
        void readULL(unsigned long long * const out) {
            unsigned long long res = 0;
            char c;
            skipWC();
            readChar(&c);
            while( (c >= '0') && (c <= '9') ) {
                res *= 10;
                res += c - '0';
                readChar(&c);
            }
            *out = res;
        }
        void readInt(int * const out) {
            int res = 0;
                int neg = 0;
            char c;
            skipWC();
            readChar(&c);
            if(c == '-') {
                neg = 1;
                c = '0';
            }
            #if IO_DEBUG_ENABLED
            else if( (c >= '0') && (c <= '9') ) {
            } else {
                fprintf(stderr, "readInt() ERROR unexpected char '%c' (decimal %d)\n", c, static_cast<int>(c));
            }
            #endif
            while( (c >= '0') && (c <= '9') ) {
                res *= 10;
                res += c - '0';
                readChar(&c);
            }
            *out = (0 == neg) ? res : -res;
        }
        void readLL(long long * const out) {
            long long res = 0;
                int neg = 0;
            char c;
            skipWC();
            readChar(&c);
            if(c == '-') {
                neg = 1;
                c = '0';
            }
            #if IO_DEBUG_ENABLED
            else if( (c >= '0') && (c <= '9') ) {
            } else {
                fprintf(stderr, "readLL() ERROR unexpected char '%c' (decimal %d)\n", c, static_cast<int>(c));
            }
            #endif
            while( (c >= '0') && (c <= '9') ) {
                res *= 10;
                res += c - '0';
                readChar(&c);
            }
            *out = (0 == neg) ? res : -res;
        }
        void readLine(char * const dst) {
            char c;
            char *d = dst;
            readChar(&c);
            while(c > 13) { // while((c!=10) && (c!=13) && (c!=0))
                *d = c;
                ++d;
                readChar(&c);
            }
            if(c == 13) { // skip (forget) EOL characters
                readChar(&c);
                if(c != 10) {
                    --bufferIndex; // next char after '/r' is not '/n' - it is unexpected but try to accept as valid EOL
                    #if IO_DEBUG_ENABLED
                    fprintf(stderr, __FUNCTION__ "() ERROR unexpected char (ASCII 0x%x) (decimal %d)\n", static_cast<int>(c), static_cast<int>(c));
                    #endif
                }
            }
            *d = 0;
        }
        char* readString(char * const dst) {
            char c;
            char *d = dst;
            skipWC();
            readChar(&c);
            while((c > 13) && (c != ' ')) {	// Read only "printable" characters
                *d = c;
                ++d;
                readChar(&c);
            }
            *d = 0;
            return d;
        }
        void readString(char * const dst, bool * const isNextCharEOL) {
            readString(dst);
            char c = buffer[bufferIndex-1];
            if((c != 10) && (c != 13) && (c != 0))
                *isNextCharEOL = false;
            else
                *isNextCharEOL = true;
        }
        inline void flush() {
            if(bufferOutIndex > 0) {
                write(1, bufferOut, bufferOutIndex);
                bufferOutIndex = 0;
            }
        }
        inline void writeChar(char c) {
            if(bufferOutIndex >= BUFFER_SIZE) {
                flush();
            }
            bufferOut[ bufferOutIndex++ ] = c;
        }
        inline void writeString(const char *str) {
            for(const char *src = str; *src ;++src) {
                writeChar(*src);
            }
        }
        inline void writeU32(unsigned x) {
            char lbuf[10];
            if((bufferOutIndex+sizeof(lbuf)) >= BUFFER_SIZE) {
                flush();
            }
            if(x) {
                unsigned len=0;
                while(x) {
                    unsigned d = x / 10;
                    lbuf[ ++len ] = '0' + static_cast<char>(x - 10*d);
                    x = d;
                }
                while(len) {
                    bufferOut[ bufferOutIndex++ ] = lbuf[ len-- ];
                }
            } else {
                bufferOut[ bufferOutIndex++ ] = '0';
            }
        }
        inline void writeU64(ULL x) {
            char lbuf[20];
            if((bufferOutIndex+sizeof(lbuf)) >= BUFFER_SIZE) {
                flush();
            }
            if(x) {
                unsigned len=0;
                while(x) {
                    ULL d = x / 10ULL;
                    lbuf[ ++len ] = '0' + static_cast<char>(x - 10ULL*d);
                    x = d;
                }
                while(len) {
                    bufferOut[ bufferOutIndex++ ] = lbuf[ len-- ];
                }
            } else {
                bufferOut[ bufferOutIndex++ ] = '0';
            }
        }
    //protected:
        inline void readIntoBuffer() {
            if(bufferIndex >= bytesRead) {
                buffer[0] = 0; // make the first characted white-char => readChar() does not check bytesRead
                bytesRead = read(0, buffer, BUFFER_SIZE);
                bufferIndex = 0;
            }
        }
        inline void skipWC() {
            char *cPtr;
            do {
                readIntoBuffer();
                cPtr = buffer + bufferIndex;
                while( (bufferIndex < bytesRead) && ((*cPtr == 10) || (*cPtr == ' ') || (*cPtr == 13))) {
                    ++bufferIndex;
                    ++cPtr;
                }
            } while((bufferIndex >= bytesRead) && (bytesRead > 0));
        }
    //private:
        const static unsigned int BUFFER_SIZE = 4 * 1024;
        char *buffer = nullptr;
        int bufferIndex = 0;
        int bytesRead = 0;
        char *bufferOut = nullptr;
        unsigned int bufferOutIndex = 0;
} static io;


const ULL stringHashModulo = (static_cast<ULL>(1e9) + 9);

inline void stringHash(ULL &hash, const char *first, const char *last, int inc)
{
    const unsigned p = 31;
    const ULL m = stringHashModulo;
    ULL p_pow = 1;
    while(first != last) {
        hash = (hash + (static_cast<ULL>((*first) - 'a') + 1ULL) * p_pow) % m;
        p_pow = (p_pow * p) % m;
        first += inc;
    }
}

//inline void stringHash(ULL &hash, const char *first, const char *last, int inc, ULL &pow)
//{
//    const unsigned p = 31;
//    const ULL m = stringHashModulo;
//    ULL p_pow = 1;
//    while(first != last) {
//        hash = (hash + (static_cast<ULL>((*first) - 'a') + 1ULL) * p_pow) % m;
//        p_pow = (p_pow * p) % m;
//        first += inc;
//    }
//    pow = p_pow;
//}

//inline void stringHashDecay(ULL &hash, ULL p_pow, unsigned d)
//{
//    const unsigned p = 31;
//    const ULL m = stringHashModulo;
//    while(d--) {
//        hash = (hash + 1ULL * p_pow) % m;
//        p_pow = (p_pow * p) % m;
//    }
//}

inline void stringHashDjb2(ULL &hash, const char *first, const char *last, int inc)
{
    while(first != last) {
        hash = ((hash << 5) + hash) + static_cast<ULL>(*first); /* hash * 33 + c */
        first += inc;
    }
}

inline void stringHashDjb2Decay(ULL &hash, unsigned d)
{
    while(d--) {
        hash = ((hash << 5) + hash); /* hash * 33 + c */
    }
}

const ULL stringHashMineModulo = 1000000007ULL;
//#define USE_MINE_MODUL0
inline void stringHashMine(ULL &hash, const char *first, const char *last, int inc)
{
    while(first != last) {
        hash = ( (29 * hash) + static_cast<ULL>((*first) - ('a'-1)) )
                #ifdef USE_MINE_MODUL0
                % stringHashMineModulo
                #endif
                ;
        first += inc;
    }
}
inline void stringHashMineDecay(ULL &hash, unsigned d)
{
    while(d--) {
        hash = (29 * hash)
                #ifdef USE_MINE_MODUL0
                % stringHashMineModulo
                #endif
                ;
    }
}

const ULL DJB2_INIT_VALUE = 5381;

#if 1
constexpr unsigned BUF_NALL_LEN  = 2000*1000;
constexpr unsigned BUF_NHASH_LEN = 1800*1000;
constexpr unsigned HASH_BLOCK_LEN = 16*1000;

static_assert(BUF_NALL_LEN > BUF_NHASH_LEN, "");
static_assert(BUF_NHASH_LEN > HASH_BLOCK_LEN, "");
static_assert(BUF_NALL_LEN > (BUF_NHASH_LEN + 2*sizeof(ULL)*(2e7 / HASH_BLOCK_LEN + 1) ), "");

#else

constexpr unsigned BUF_NALL_LEN  = 4;
constexpr unsigned BUF_NHASH_LEN = 1000;
constexpr unsigned HASH_BLOCK_LEN = 3;

#endif

#ifdef USE_STATIC_MEMORY
void* allocateMemory(unsigned size)
{
    static char memory_pool[2*1024*1024];
    static unsigned pos = 0;
    void * addr = nullptr;
    if((pos + size) <= sizeof(memory_pool))
    {
        addr = (memory_pool + pos);
        pos += (size + ((8 - (size & 0x7)) & 0x7) );
    }
    return addr;

    static_assert(sizeof(memory_pool) > (1024 + (BUF_NALL_LEN + 2*IO::BUFFER_SIZE)), "");
};
#endif

#define USE_CHARS
#ifdef USE_CHARS
static unsigned chars['z' - 'a' + 1];
inline void addCharUnknown(const char c)
{
    chars[c - 'a'] ^= 1;
}
bool checkCharsUnknown(const unsigned n)
{
    unsigned sum = 0;
    for(unsigned i=0; i<(sizeof(chars)/sizeof(chars[0])); ++i) { sum += chars[i]; }
    return ((n & 1) == sum);
}
inline void addCharN(const char c)
{
    ++(chars[c - 'a']);
    //cerr << __FUNCTION__ << "('" << c << "') = " << chars[c - 'a'] << endl;
}
inline void subCharN(const char c)
{
    --(chars[c - 'a']);
    //cerr << __FUNCTION__ << "('" << c << "') = " << chars[c - 'a'] << endl;
}
bool checkCharsN(const unsigned n)
{
    unsigned sum = 0;
    for(unsigned i=0; i<(sizeof(chars)/sizeof(chars[0])); ++i) {
        sum |= chars[i];
    }
    //cerr << "checkChars: sum " << sum  << " n " << n << " n&1 " << (n&1) << "\n";
    return (0 == sum);  // omited middle char in addCharN()
}
#endif

bool checkNAll(const unsigned n
               #ifndef USE_CHARS
               , unsigned * const charXor
               #endif
               )
{
    char *const buf = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(BUF_NALL_LEN))
                        #endif
                            char[BUF_NALL_LEN];
	char c;
	for(unsigned i=0; i<n; ++i)
	{
		io.readChar(&c);
        #ifdef USE_CHARS
//notNeeded        if((2*i) < n) addCharN(c); else subCharN(c);
        #else
		*charXor ^= static_cast<unsigned>(c);
        #endif
		buf[i] = c;
	}
    #ifdef USE_CHARS
//notNeeded    if(n & 1) { subCharN(buf[n>>1]); }
    #endif

	char *first = buf;
	char *last  = buf + (n - 1);
	while(first < last)
	{
		if(*first != *last) return false;
		++first;
		--last;
	}

	return true;
}

bool checkNHash(const unsigned n
                #ifndef USE_CHARS
                , unsigned * const charXor
                #endif
                )
{
	const unsigned n_div_2 = n>>1;
	const unsigned hashesNum = (n_div_2 / HASH_BLOCK_LEN);
    ULL *hashes = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(sizeof(ULL)*hashesNum))
                        #endif
                            ULL[hashesNum];
    ULL *hashesDjb2 = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(sizeof(ULL)*hashesNum))
                        #endif
                            ULL[hashesNum];
    char * const buf = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(BUF_NHASH_LEN))
                        #endif
                            char[BUF_NHASH_LEN];
	char c;

	// prefix
	const unsigned prefixLen = n_div_2 -  hashesNum*HASH_BLOCK_LEN;
	for(unsigned i=0; i<prefixLen; ++i)
	{
		io.readChar(&c);
        #ifdef USE_CHARS
        addCharN(c);
        #else
		*charXor ^= static_cast<unsigned>(c);
        #endif
		buf[i] = c;
	}

	// first half hashes
	char *last = buf + BUF_NHASH_LEN;
	char *first = last - HASH_BLOCK_LEN;
	for(unsigned h=0; h<hashesNum; ++h)
	{
		for(unsigned i=0; i<HASH_BLOCK_LEN; ++i)
		{
			io.readChar(&c);
            #ifdef USE_CHARS
            addCharN(c);
            #else
			*charXor ^= static_cast<unsigned>(c);
            #endif
			first[i] = c;
		}

		ULL hash = 0;
		stringHash(hash, first, last, 1);
		hashes[h] = hash;
		ULL hashDjb2 = 5381;
		stringHashDjb2(hashDjb2, first, last, 1);
		hashesDjb2[h] = hashDjb2;
	}

	if(n & 1) {	// skip middle char
		io.readChar(&c);
        #ifdef USE_CHARS
        //addChar(c);   //skip middle char in N is known
        #else
		*charXor ^= static_cast<unsigned>(c);
        #endif
	}

	// second half hashes
	for(unsigned h=0; h<hashesNum; ++h)
	{
		for(unsigned i=0; i<HASH_BLOCK_LEN; ++i)
		{
			io.readChar(&c);
            #ifdef USE_CHARS
            subCharN(c);
            #else
			*charXor ^= static_cast<unsigned>(c);
            #endif
			first[i] = c;
		}

		ULL hash = 0;
		stringHash(hash, last-1, first-1, -1);
		ULL hashDjb2 = 5381;
		stringHashDjb2(hashDjb2, last-1, first-1, -1);

		if((hashes[hashesNum - h - 1] != hash) or (hashesDjb2[hashesNum - h - 1] != hashDjb2)) {
			return false;
		}
	}

	// check last with prefix
	for(unsigned i=prefixLen; i;)
	{
		io.readChar(&c);
        #ifdef USE_CHARS
        subCharN(c);
        #else
		*charXor ^= static_cast<unsigned>(c);
        #endif
		if(buf[--i] != c) {
			return false;
		}
	}

	return true;
}

bool checkUnknown(unsigned &n
                  #ifndef USE_CHARS
                  , unsigned * const charXor
                  #endif
                  )
{
    ULL *hashesDjb2 = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(sizeof(ULL)*(static_cast<unsigned>(2e7) / HASH_BLOCK_LEN + 1)))
                        #endif
                            ULL[static_cast<unsigned>(2e7) / HASH_BLOCK_LEN + 1];
    ULL *hashesMine = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(sizeof(ULL)*(static_cast<unsigned>(2e7) / HASH_BLOCK_LEN + 1)))
                        #endif
                            ULL[static_cast<unsigned>(2e7) / HASH_BLOCK_LEN + 1];
    char * const buf_start = new
                        #ifdef USE_STATIC_MEMORY
                        (allocateMemory(BUF_NHASH_LEN))
                        #endif
                        char[BUF_NHASH_LEN];

    char c;
    bool compareAll = true;
    unsigned charsRead = 0;
    unsigned hashesDjb2Num = 0;
    ULL hashDjb2Left, hashDjb2Right = DJB2_INIT_VALUE;
    ULL hashMineLeft, hashMineRight = 0;

    char *buf = buf_start;
    for(n = 0;;)
    {
        io.readChar(&c);
        if((c < 'a') or (c > 'z')) break;
        #ifdef USE_CHARS
        addCharUnknown(c);
        #else
        //cerr << "xoruje '" << c << "'\n";
        *charXor ^= static_cast<unsigned>(c);
        #endif
        buf[charsRead] = c;
        ++n;

        if(++charsRead >= HASH_BLOCK_LEN)
        {
            stringHashDjb2(hashDjb2Right, buf, buf + HASH_BLOCK_LEN, 1);
            hashDjb2Left = 0;
            stringHashDjb2(hashDjb2Left, buf + HASH_BLOCK_LEN - 1, buf - 1, -1);
            hashesDjb2[hashesDjb2Num] = hashDjb2Left;

            stringHashMine(hashMineRight, buf, buf + HASH_BLOCK_LEN, 1);
            hashMineLeft = 0;
            stringHashMine(hashMineLeft, buf + HASH_BLOCK_LEN - 1, buf - 1, -1);
            hashesMine[hashesDjb2Num] = hashMineLeft;

            ++hashesDjb2Num;
            charsRead = 0;

            if(n > (BUF_NHASH_LEN - HASH_BLOCK_LEN)) {
                buf = buf_start;
                compareAll = false;
            }
        }
    }

    if(compareAll)
    {
        char *first = buf_start;
        char *last  = buf_start + (n - 1);
        while(first < last)
        {
            if(*first != *last) return false;
            ++first;
            --last;
        }
        return true;
    }

    stringHashDjb2(hashDjb2Right, buf, buf + charsRead, 1);
    hashDjb2Left = DJB2_INIT_VALUE;
    stringHashDjb2(hashDjb2Left, buf + charsRead - 1, buf - 1, -1);

    stringHashMine(hashMineRight, buf, buf + charsRead, 1);
    hashMineLeft = 0;
    stringHashMine(hashMineLeft, buf + charsRead - 1, buf - 1, -1);

    while(hashesDjb2Num)
    {
        stringHashDjb2Decay(hashDjb2Left, HASH_BLOCK_LEN);
        hashDjb2Left += hashesDjb2[--hashesDjb2Num];
//cerr << "hashDjb2Left  " << hashDjb2Left << endl;

        stringHashMineDecay(hashMineLeft, HASH_BLOCK_LEN);
        hashMineLeft += hashesMine[hashesDjb2Num]
                            #ifdef USE_MINE_MODUL0
                            % stringHashMineModulo
                            #endif
                            ;
//cerr << "hashMineLeft  " << hashMineLeft << endl;
    }
//cerr << "hashDjb2Left  " << hashDjb2Left << endl << "hashDjb2Right " << hashDjb2Right << endl;
//cerr << "hashMineLeft  " << hashMineLeft << endl << "hashMineRight " << hashMineRight << endl;

//return true;
    return ((hashDjb2Left == hashDjb2Right) and (hashMineLeft == hashMineRight));
}


void experiment()
{
    io.writeString("--- experiment ---\n\n");
    char buf[500];

    const char alamakota[] = "alamakota";

    if(0)
    {
        const char *first = alamakota;
        const char *last = alamakota + 1;
        ULL hash1 = 0;
        stringHashDjb2(hash1, first, last, 1);
        io.writeString("hash(a) = "); io.writeU64(hash1); io.writeString("\n");

        ULL hash2 = 0;
        stringHashDjb2(hash2, first+1, last+1, 1);
        io.writeString("hash(l) = "); io.writeU64(hash2); io.writeString("\n");

        ULL hash3 = 0;
        stringHashDjb2(hash3, first, last+1, 1);
        io.writeString("hash(al) = "); io.writeU64(hash3); io.writeString("\n");

        ULL hash4 = hash1;
        stringHashDjb2Decay(hash4, 1);
        io.writeString("hash4 = "); io.writeU64(hash4); io.writeString(" hash(+) = "); io.writeU64(hash4 + hash2); io.writeString("\n\n");
    }

    if(0)
    {
        ULL hash1 = 0;
        stringHashDjb2(hash1, alamakota, alamakota+5, 1);
        io.writeString("hash(alama) = "); io.writeU64(hash1); io.writeString("\n");

        ULL hash2 = 0;
        stringHashDjb2(hash2, alamakota+5, alamakota+9, 1);
        io.writeString("hash(kota) = "); io.writeU64(hash2); io.writeString("\n");

        ULL hash3 = 0;
        stringHashDjb2(hash3, alamakota, alamakota+9, 1);
        io.writeString("hash(alamakota) = "); io.writeU64(hash3); io.writeString("\n");

        ULL hash4 = hash1;
        stringHashDjb2Decay(hash4, 4);
        io.writeString("hash4 = "); io.writeU64(hash4); io.writeString(" hash(+) = "); io.writeU64(hash4 + hash2); io.writeString("\n\n");
    }

    if(0)
    {
        ULL hash1 = 0;
        stringHashDjb2(hash1, alamakota, alamakota+2, 1);
        io.writeString("hash(al) = "); io.writeU64(hash1); io.writeString("\n");

        ULL hash2 = 0;
        stringHashDjb2(hash2, alamakota+1, alamakota+3, 1);
        io.writeString("hash(la) = "); io.writeU64(hash2); io.writeString("\n");
    }

    if(1)
    {
        const char abc[] = "abcdefghijklmnopqrst";
        ULL hash1 = 0;
        stringHashMine(hash1, abc, abc+2, 1);
        io.writeString("hash(ab) = "); io.writeU64(hash1); io.writeString("\n");

        ULL hash2 = 0;
        stringHashMine(hash2, abc+2, abc+4, 1);
        io.writeString("hash(cd) = "); io.writeU64(hash2); io.writeString("\n");

        ULL hash3 = 0;
        stringHashMine(hash3, abc, abc+4, 1);
        io.writeString("hash(abcd) = "); io.writeU64(hash3); io.writeString("\n");

        ULL hash4 = hash1;
        stringHashMineDecay(hash4, 2);
        io.writeString("hash4 = "); io.writeU64(hash4); io.writeString(" hash(+) = "); io.writeU64((hash4 + hash2)%stringHashMineModulo); io.writeString("\n\n");
    }

    if(1)
    {
        const char abc[] = "abc";
        const char cba[] = "abc";
        const char cab[] = "cab";
        ULL hash1 = 0;
        stringHashMine(hash1, abc, abc+sizeof(abc), 1);
        io.writeString("hash(abc) = "); io.writeU64(hash1); io.writeString("\n");

        ULL hash2 = 0;
        stringHashMine(hash2, cba, cba+sizeof(cba), 1);
        io.writeString("hash(cba) = "); io.writeU64(hash2); io.writeString("\n");

        ULL hash3 = 0;
        stringHashMine(hash3, cab, cab+sizeof(cab), 1);
        io.writeString("hash(cab) = "); io.writeU64(hash3); io.writeString("\n\n");
    }

    if(0)
    {
        const char abc[] = "abc";
        ULL hash1 = 0;
        stringHashMine(hash1, abc, abc+1, 1);
        io.writeString("hash( a ) = "); io.writeU64(hash1); io.writeString("\n");
        for(int i=0; i<50; ++i) {
            stringHashMineDecay(hash1, 1);
            io.writeString("hashDec() = "); io.writeU64(hash1); io.writeString("\n");
        }
    }
}


int main()
{
	bool result = true;
    unsigned n
            #ifndef USE_CHARS
            , charXor = 0
            #endif
            ;
    io.readUInt(&n);
    io.skipWC();

    const bool nUnknown = (0 == n);

//if(n > 2e8) { experiment(); return 0; } else

    if((n > 0) and (n <= BUF_NALL_LEN))
    {
        result = checkNAll(n
                           #ifndef USE_CHARS
                           , &charXor
                           #endif
                           );
    }
    else if(n > 0)
    {
        result = checkNHash(n
                            #ifndef USE_CHARS
                            , &charXor
                            #endif
                            );
    }
    else
    {
        result = checkUnknown(n
                              #ifndef USE_CHARS
                              , &charXor
                              #endif
                              );
    }

#ifdef USE_CHARS
    result = result && ((nUnknown) ? checkCharsUnknown(n) : checkCharsN(n));
#else
//DEBUG(result);
//DEBUG(charXor);
//DEBUG(n);
//cerr << "n jako znak: '" << static_cast<char>(charXor) <<"'" << endl;
    if(n & 1) {
        if((charXor < 'a') or (charXor > 'z')) {
            result = false;
        }
    } else {
        result = result && (0 == charXor);
    }
#endif

    io.writeString((result) ? "TAK\n" : "NIE\n");
}