import java.util.Arrays; import java.util.Scanner; public class ilo { int[] fib; int fibSize; void createFibonacci() { fib = new int[48]; fib[0] = 0; fib[1] = 1; int i = 2; int v = 0; fibSize = 2; do { v = fib[i - 2] + fib[i - 1]; fib[i++] = v; fibSize++; } while (v <= 1000*1000*1000); } boolean checkIsFibValue(int value) { return Arrays.binarySearch(fib, 0, fibSize, value) >= 0; } public boolean checkValue(int value) { if (value == 1) { return true; } for (int i = 3; i < fibSize; i++) { int fi = fib[i]; int rest = value % fi; if (rest == 0) { int div = value / fi; return checkIsFibValue(div); } } return false; } public void run() { createFibonacci(); Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner("5 5 4 12 11 10"); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int value = sc.nextInt(); boolean ok = checkValue(value); System.out.println(ok ? "TAK" : "NIE"); } } public static void main(String[] args) { new ilo().run(); } }
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 | import java.util.Arrays; import java.util.Scanner; public class ilo { int[] fib; int fibSize; void createFibonacci() { fib = new int[48]; fib[0] = 0; fib[1] = 1; int i = 2; int v = 0; fibSize = 2; do { v = fib[i - 2] + fib[i - 1]; fib[i++] = v; fibSize++; } while (v <= 1000*1000*1000); } boolean checkIsFibValue(int value) { return Arrays.binarySearch(fib, 0, fibSize, value) >= 0; } public boolean checkValue(int value) { if (value == 1) { return true; } for (int i = 3; i < fibSize; i++) { int fi = fib[i]; int rest = value % fi; if (rest == 0) { int div = value / fi; return checkIsFibValue(div); } } return false; } public void run() { createFibonacci(); Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner("5 5 4 12 11 10"); int n = sc.nextInt(); for (int i = 0; i < n; i++) { int value = sc.nextInt(); boolean ok = checkValue(value); System.out.println(ok ? "TAK" : "NIE"); } } public static void main(String[] args) { new ilo().run(); } } |