#include <stdio.h> #include <map> #include <cmath> #include <stdint.h> using namespace std; int get_int() { int ret = 0; int c = getchar_unlocked(); while(c < '0' || c > '9') { c = getchar_unlocked(); } while(c >= '0' && c <= '9') { ret = (ret<<3) + (ret<<1) + (c - '0'); c = getchar_unlocked(); } return ret; } map<int, bool> cache; map<int, bool>::iterator ci; bool is_square(int64_t n) { int64_t h = n & 0xF; if (h > 9) return false; if ( h != 2 && h != 3 && h != 5 && h != 6 && h != 7 && h != 8 ) { int64_t t = (int64_t) floor( sqrt((double) n) + 0.5 ); return t*t == n; } return false; } bool is_fib(int n) { int v1 = 5 * n * n; int v2 = v1 + 4; int v3 = v1 - 4; return is_square(v2) || is_square(v3); } inline bool check_n(int n) { if(is_fib(n)) { return true; } int dm1 = 0; int dm2 = 1; int d = 1; int t = n; do { d = dm1 + dm2; if(n % d == 0) { t = n / d; if(is_fib(t)) { return true; } } dm2 = dm1; dm1 = d; } while(t > d); return false; } bool check(int n) { ci = cache.find(n); if (ci != cache.end()) { return ci->second; } bool ret = check_n(n); cache[n] = ret; return ret; } int main() { int T = get_int(); while(T--) { int n = get_int(); bool can = check(n); printf("%s\n", can ? "TAK" : "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 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 | #include <stdio.h> #include <map> #include <cmath> #include <stdint.h> using namespace std; int get_int() { int ret = 0; int c = getchar_unlocked(); while(c < '0' || c > '9') { c = getchar_unlocked(); } while(c >= '0' && c <= '9') { ret = (ret<<3) + (ret<<1) + (c - '0'); c = getchar_unlocked(); } return ret; } map<int, bool> cache; map<int, bool>::iterator ci; bool is_square(int64_t n) { int64_t h = n & 0xF; if (h > 9) return false; if ( h != 2 && h != 3 && h != 5 && h != 6 && h != 7 && h != 8 ) { int64_t t = (int64_t) floor( sqrt((double) n) + 0.5 ); return t*t == n; } return false; } bool is_fib(int n) { int v1 = 5 * n * n; int v2 = v1 + 4; int v3 = v1 - 4; return is_square(v2) || is_square(v3); } inline bool check_n(int n) { if(is_fib(n)) { return true; } int dm1 = 0; int dm2 = 1; int d = 1; int t = n; do { d = dm1 + dm2; if(n % d == 0) { t = n / d; if(is_fib(t)) { return true; } } dm2 = dm1; dm1 = d; } while(t > d); return false; } bool check(int n) { ci = cache.find(n); if (ci != cache.end()) { return ci->second; } bool ret = check_n(n); cache[n] = ret; return ret; } int main() { int T = get_int(); while(T--) { int n = get_int(); bool can = check(n); printf("%s\n", can ? "TAK" : "NIE"); } return 0; } |