#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <time.h> #define MAXB 16 #define MAXV 26 #define MAXT 15 #define MAXS3 (3*MAXV) #define MAXS8 (8*MAXV) #define MAXS13 (13*MAXV) #define MAXR (MAXV*MAXV*MAXV*MAXV) //3.655.808 #define MAXH 4096 long long s3[MAXS3] = {0}; long long s8[MAXS8] = {0}; long long s13[MAXS13] = {0}; uint64_t* sr; long long s7[MAXH] = {0}; int mp = 13; int prime[13] = {3,5,7,11,13,17,19,23,29,31,37,41,43}; char buf[MAXB]; int getn(char* b) { int i; for (i=0;;i++) if (b[i]<'a' || b[i]>'z') return i; } void naive(char* b, int n) { for (int i=0;i<n;i++) if (b[i]!=b[n-1-i]) { printf("NIE\n"); return ; } printf("TAK\n"); } void shows3(int n) { printf("S3: "); for (int i=0;i<MAXS3;i++) if (s3[i]>0) printf(" %d = %d(%d), ", i, s3[i], s3[i]%(n-1)); printf("\n"); } void shows8(int n) { printf("S8: "); for (int i=0;i<MAXS8;i++) if (s8[i]>0) printf(" %d = %d(%d), ", i, s8[i], s8[i]%(n+6)); printf("\n"); } void shows7(int n) { printf("S7: "); for (int i=0;i<MAXH;i++) if (s7[i]>0) printf(" %d = %lld(%lld), ", i, s7[i], s7[i]%(n+5)); printf("\n"); } void shows13(int n) { printf("S13: "); for (int i=0;i<MAXS13;i++) if (s13[i]>0) printf(" %d = %d(%d), ", i, s13[i], s13[i]%(n+11)); printf("\n"); } int check3(int n) { int np = 0; for (int i=0;i<MAXS3;i++) if (s3[i]%(n-1)!=0) np++; return np<=(n%2); } int check8(int n) { int np = 0; for (int i=0;i<MAXS8;i++) if (s8[i]%(n+6)!=0) np++; return np<=((n+1)%2); } int check7(int n) { int np = 0; for (int i=0;i<MAXH;i++) if (s7[i]%(n+5)!=0) np++; return np<=(n%2); } int check13(int n) { int np = 0; for (int i=0;i<MAXS13;i++) if (s13[i]%(n+11)!=0) np++; return np<=(n%2); } void showr() { int i; for (i=0;i<MAXR;i++) if (sr[i]!=0) { int c4 = i % MAXV; int c3 = (i/MAXV) % MAXV; int c2 = (i/MAXV/MAXV) % MAXV; int c1 = (i/MAXV/MAXV/MAXV) % MAXV; int rix = c1 + MAXV*c2 + MAXV*MAXV*c3 + MAXV*MAXV*MAXV*c4; if (i==rix || sr[i]!=sr[rix]) printf("%c%c%c%c[%d] = %d (r[%d]:%d)\n", 'a' + c1, 'a' + c2, 'a' + c3, 'a' + c4, i, (int) sr[i], rix, sr[rix]); } } int checkr(int n) { int i, np = 0; for (i=0;i<MAXR;i++) if (sr[i]!=0) { int c4 = i % MAXV; int c3 = (i/MAXV) % MAXV; int c2 = (i/MAXV/MAXV) % MAXV; int c1 = (i/MAXV/MAXV/MAXV) % MAXV; int rix = c1 + MAXV*c2 + MAXV*MAXV*c3 + MAXV*MAXV*MAXV*c4; if (sr[i]!=sr[rix]) return 0; if (i==rix && sr[i]%2!=0) np++; } return np<=((n+1)%2); } void mix() { for (int i=0;i<mp;i++) { int ix1 = rand() % mp; int ix2 = rand() % mp; int v = prime[ix1]; prime[ix1] = prime[ix2]; prime[ix2] = v; } } int main() { int i, n, ix = 0, ts3 = 0, ts8 = 0, ts13 = 0, tr = 0, ts7 = 0; char tmp[MAXT]; sr = (uint64_t*) malloc(MAXR*sizeof(uint64_t)); for (i=0;i<MAXR;i++) sr[i] = 0; fgets(buf, MAXB, stdin); //srand(time(NULL)); mix(); for (;;) { if (!fgets(buf, MAXB, stdin)) break ; n = getn(buf); if (ix==0 && n<MAXB-5) { naive(buf, n); return 0; } for (i=0;i<n;i++) { tr += MAXR*(buf[i]-'a'); ts3 += (buf[i]-'a'); ts8 += (buf[i]-'a'); ts13 += (buf[i]-'a'); tr /= MAXV; if (i>=3) ts3 -= (buf[i-3]-'a'); else if (ix>0) ts3 -= (tmp[-(i-3)]-'a'); if (i>=8) ts8 -= (buf[i-8]-'a'); else if (ix>0) ts8 -= (tmp[-(i-8)]-'a'); if (i>=13) ts13 -= (buf[i-13]-'a'); else if (ix>0) ts13 -= (tmp[-(i-13)]-'a'); if (ix>0 || i>=7) s8[ts8] += ix+i; if (ix>0 || i>=12) s13[ts13] += ix+i; if (ix>0 || i>=2) s3[ts3] += ix+i-1; if (ix>0 || i>=3) sr[tr]++; if (ix>0 || i>=6) { ts7 = (buf[i]-'a')*prime[0] + (((i>=1)?buf[i-1]:tmp[-(i-1)])-'a')*prime[1] + (((i>=2)?buf[i-2]:tmp[-(i-2)])-'a')*prime[2] + (((i>=3)?buf[i-3]:tmp[-(i-3)])-'a')*prime[3] + (((i>=4)?buf[i-4]:tmp[-(i-4)])-'a')*prime[2] + (((i>=5)?buf[i-5]:tmp[-(i-5)])-'a')*prime[1] + (((i>=6)?buf[i-6]:tmp[-(i-6)])-'a')*prime[0]; s7[ts7 % MAXH] += ix+i; } } for (i=0;i<n&&i<MAXT-1;i++) tmp[i+1] = buf[n-1-i]; ix += n; } //printf("n=%d\n", ix); //shows3(ix); //shows8(ix); //shows7(ix); //shows13(ix); //showr(); if (!checkr(ix) || !check3(ix) || !check8(ix) || !check7(ix) || !check13(ix)) printf("NIE\n"); else printf("TAK\n"); }
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 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <time.h> #define MAXB 16 #define MAXV 26 #define MAXT 15 #define MAXS3 (3*MAXV) #define MAXS8 (8*MAXV) #define MAXS13 (13*MAXV) #define MAXR (MAXV*MAXV*MAXV*MAXV) //3.655.808 #define MAXH 4096 long long s3[MAXS3] = {0}; long long s8[MAXS8] = {0}; long long s13[MAXS13] = {0}; uint64_t* sr; long long s7[MAXH] = {0}; int mp = 13; int prime[13] = {3,5,7,11,13,17,19,23,29,31,37,41,43}; char buf[MAXB]; int getn(char* b) { int i; for (i=0;;i++) if (b[i]<'a' || b[i]>'z') return i; } void naive(char* b, int n) { for (int i=0;i<n;i++) if (b[i]!=b[n-1-i]) { printf("NIE\n"); return ; } printf("TAK\n"); } void shows3(int n) { printf("S3: "); for (int i=0;i<MAXS3;i++) if (s3[i]>0) printf(" %d = %d(%d), ", i, s3[i], s3[i]%(n-1)); printf("\n"); } void shows8(int n) { printf("S8: "); for (int i=0;i<MAXS8;i++) if (s8[i]>0) printf(" %d = %d(%d), ", i, s8[i], s8[i]%(n+6)); printf("\n"); } void shows7(int n) { printf("S7: "); for (int i=0;i<MAXH;i++) if (s7[i]>0) printf(" %d = %lld(%lld), ", i, s7[i], s7[i]%(n+5)); printf("\n"); } void shows13(int n) { printf("S13: "); for (int i=0;i<MAXS13;i++) if (s13[i]>0) printf(" %d = %d(%d), ", i, s13[i], s13[i]%(n+11)); printf("\n"); } int check3(int n) { int np = 0; for (int i=0;i<MAXS3;i++) if (s3[i]%(n-1)!=0) np++; return np<=(n%2); } int check8(int n) { int np = 0; for (int i=0;i<MAXS8;i++) if (s8[i]%(n+6)!=0) np++; return np<=((n+1)%2); } int check7(int n) { int np = 0; for (int i=0;i<MAXH;i++) if (s7[i]%(n+5)!=0) np++; return np<=(n%2); } int check13(int n) { int np = 0; for (int i=0;i<MAXS13;i++) if (s13[i]%(n+11)!=0) np++; return np<=(n%2); } void showr() { int i; for (i=0;i<MAXR;i++) if (sr[i]!=0) { int c4 = i % MAXV; int c3 = (i/MAXV) % MAXV; int c2 = (i/MAXV/MAXV) % MAXV; int c1 = (i/MAXV/MAXV/MAXV) % MAXV; int rix = c1 + MAXV*c2 + MAXV*MAXV*c3 + MAXV*MAXV*MAXV*c4; if (i==rix || sr[i]!=sr[rix]) printf("%c%c%c%c[%d] = %d (r[%d]:%d)\n", 'a' + c1, 'a' + c2, 'a' + c3, 'a' + c4, i, (int) sr[i], rix, sr[rix]); } } int checkr(int n) { int i, np = 0; for (i=0;i<MAXR;i++) if (sr[i]!=0) { int c4 = i % MAXV; int c3 = (i/MAXV) % MAXV; int c2 = (i/MAXV/MAXV) % MAXV; int c1 = (i/MAXV/MAXV/MAXV) % MAXV; int rix = c1 + MAXV*c2 + MAXV*MAXV*c3 + MAXV*MAXV*MAXV*c4; if (sr[i]!=sr[rix]) return 0; if (i==rix && sr[i]%2!=0) np++; } return np<=((n+1)%2); } void mix() { for (int i=0;i<mp;i++) { int ix1 = rand() % mp; int ix2 = rand() % mp; int v = prime[ix1]; prime[ix1] = prime[ix2]; prime[ix2] = v; } } int main() { int i, n, ix = 0, ts3 = 0, ts8 = 0, ts13 = 0, tr = 0, ts7 = 0; char tmp[MAXT]; sr = (uint64_t*) malloc(MAXR*sizeof(uint64_t)); for (i=0;i<MAXR;i++) sr[i] = 0; fgets(buf, MAXB, stdin); //srand(time(NULL)); mix(); for (;;) { if (!fgets(buf, MAXB, stdin)) break ; n = getn(buf); if (ix==0 && n<MAXB-5) { naive(buf, n); return 0; } for (i=0;i<n;i++) { tr += MAXR*(buf[i]-'a'); ts3 += (buf[i]-'a'); ts8 += (buf[i]-'a'); ts13 += (buf[i]-'a'); tr /= MAXV; if (i>=3) ts3 -= (buf[i-3]-'a'); else if (ix>0) ts3 -= (tmp[-(i-3)]-'a'); if (i>=8) ts8 -= (buf[i-8]-'a'); else if (ix>0) ts8 -= (tmp[-(i-8)]-'a'); if (i>=13) ts13 -= (buf[i-13]-'a'); else if (ix>0) ts13 -= (tmp[-(i-13)]-'a'); if (ix>0 || i>=7) s8[ts8] += ix+i; if (ix>0 || i>=12) s13[ts13] += ix+i; if (ix>0 || i>=2) s3[ts3] += ix+i-1; if (ix>0 || i>=3) sr[tr]++; if (ix>0 || i>=6) { ts7 = (buf[i]-'a')*prime[0] + (((i>=1)?buf[i-1]:tmp[-(i-1)])-'a')*prime[1] + (((i>=2)?buf[i-2]:tmp[-(i-2)])-'a')*prime[2] + (((i>=3)?buf[i-3]:tmp[-(i-3)])-'a')*prime[3] + (((i>=4)?buf[i-4]:tmp[-(i-4)])-'a')*prime[2] + (((i>=5)?buf[i-5]:tmp[-(i-5)])-'a')*prime[1] + (((i>=6)?buf[i-6]:tmp[-(i-6)])-'a')*prime[0]; s7[ts7 % MAXH] += ix+i; } } for (i=0;i<n&&i<MAXT-1;i++) tmp[i+1] = buf[n-1-i]; ix += n; } //printf("n=%d\n", ix); //shows3(ix); //shows8(ix); //shows7(ix); //shows13(ix); //showr(); if (!checkr(ix) || !check3(ix) || !check8(ix) || !check7(ix) || !check13(ix)) printf("NIE\n"); else printf("TAK\n"); } |