//#define DEBUG #include <cstdio> #include <vector> #include <algorithm> #ifdef DEBUG #define DEBUG_TEST 1 #else #define DEBUG_TEST 0 #endif #define tracef(fmt, ...) \ do { if (DEBUG_TEST) printf("%s:%d:%s(): " fmt "\n", __FILE__, \ __LINE__, __func__, ## __VA_ARGS__); } while (0) using namespace std; vector<int> generate_fibonacci() { vector<int> ret; ret.reserve(50); int prev = 1; for (int curr = prev; curr <= 1000000000;) { ret.push_back(curr); int next = curr + prev; prev = curr; curr = next; } return ret; } bool is_product_of_two_fib(int a) { tracef("a = %d", a); static const vector<int> fib = generate_fibonacci(); if (a == 0) { return true; } for (int i = 0; i < fib.size(); i++) { // a = p * q int p = a / fib[i]; int r = a % fib[i]; if (r != 0) { continue; } tracef("p = %d", p); if (p == 0) { return false; } if (p == 1) { return true; } if (binary_search(fib.begin(), fib.end(), p)) { tracef("binary_search found element!"); return true; } } tracef("End of is_product_of_two_fib!"); return false; } int main() { int t; scanf("%d", &t); while (t--) { int a; scanf("%d", &a); printf(is_product_of_two_fib(a) ? "TAK\n" : "NIE\n"); } 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 | //#define DEBUG #include <cstdio> #include <vector> #include <algorithm> #ifdef DEBUG #define DEBUG_TEST 1 #else #define DEBUG_TEST 0 #endif #define tracef(fmt, ...) \ do { if (DEBUG_TEST) printf("%s:%d:%s(): " fmt "\n", __FILE__, \ __LINE__, __func__, ## __VA_ARGS__); } while (0) using namespace std; vector<int> generate_fibonacci() { vector<int> ret; ret.reserve(50); int prev = 1; for (int curr = prev; curr <= 1000000000;) { ret.push_back(curr); int next = curr + prev; prev = curr; curr = next; } return ret; } bool is_product_of_two_fib(int a) { tracef("a = %d", a); static const vector<int> fib = generate_fibonacci(); if (a == 0) { return true; } for (int i = 0; i < fib.size(); i++) { // a = p * q int p = a / fib[i]; int r = a % fib[i]; if (r != 0) { continue; } tracef("p = %d", p); if (p == 0) { return false; } if (p == 1) { return true; } if (binary_search(fib.begin(), fib.end(), p)) { tracef("binary_search found element!"); return true; } } tracef("End of is_product_of_two_fib!"); return false; } int main() { int t; scanf("%d", &t); while (t--) { int a; scanf("%d", &a); printf(is_product_of_two_fib(a) ? "TAK\n" : "NIE\n"); } return 0; } |