#include <iostream> #include <vector> #include <algorithm> std::vector<int> fib; void create_fib_tab() { int i; int a, b; fib.resize(47); // up to 1836311903 a = 0; b = 1; fib[0] = a; fib[1] = b; for(i=2 ; i<fib.size() ; ++i) { fib[i] = a + b; a = b; b = fib[i]; //std::cout << fib[i] << std::endl;; } } bool has_fib(int val) { return std::find(fib.begin(), fib.end(), val) != fib.end(); } bool can_be(int n) { int i; if( n == 0 || n == 1 ) return true; if( has_fib(n) ) return true; // start from val "2" (0, 1, 1, 2, ...) for(i=3 ; i<(int)fib.size() ; ++i) { int a = fib[i]; if( (n % a) == 0 ) { int b = n / a; if( has_fib(b) ) { if( (long)a * (long)b == (long)n ) return true; } } } return false; } int main() { int t, i, n; create_fib_tab(); std::cin >> t; for(i=0 ; i<t ; ++i) { std::cin >> n; if( can_be(n) ) std::cout << "TAK" << std::endl; else std::cout << "NIE" << std::endl; } 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 | #include <iostream> #include <vector> #include <algorithm> std::vector<int> fib; void create_fib_tab() { int i; int a, b; fib.resize(47); // up to 1836311903 a = 0; b = 1; fib[0] = a; fib[1] = b; for(i=2 ; i<fib.size() ; ++i) { fib[i] = a + b; a = b; b = fib[i]; //std::cout << fib[i] << std::endl;; } } bool has_fib(int val) { return std::find(fib.begin(), fib.end(), val) != fib.end(); } bool can_be(int n) { int i; if( n == 0 || n == 1 ) return true; if( has_fib(n) ) return true; // start from val "2" (0, 1, 1, 2, ...) for(i=3 ; i<(int)fib.size() ; ++i) { int a = fib[i]; if( (n % a) == 0 ) { int b = n / a; if( has_fib(b) ) { if( (long)a * (long)b == (long)n ) return true; } } } return false; } int main() { int t, i, n; create_fib_tab(); std::cin >> t; for(i=0 ; i<t ; ++i) { std::cin >> n; if( can_be(n) ) std::cout << "TAK" << std::endl; else std::cout << "NIE" << std::endl; } return 0; } |