import java.io.FileInputStream; import java.util.Scanner; import java.util.SortedSet; import java.util.TreeSet; import java.util.stream.IntStream; import static java.lang.Math.*; public class ilo { private static final SortedSet<Integer> fibonacciNums = new TreeSet<>(); private static void generateFibonacciNums() { int[] nums = new int[3]; nums[1] = 1; while (nums[2] <= 1_000_000_000) { fibonacciNums.add(nums[2]); nums[2] = nums[0] + nums[1]; System.arraycopy(nums, 1, nums, 0, 2); // shift array 1 position down } //System.out.println("" + fibonacciNums.size() + " " + fibonacciNums); } public static void main(String[] args) throws Exception { generateFibonacciNums(); Scanner sc = new Scanner(args.length == 0 ? System.in : new FileInputStream(args[0])); IntStream.rangeClosed(1, sc.nextInt()).forEach(tc -> { int num = sc.nextInt(); SortedSet<Integer> fibonacciDivs = fibonacciNums.subSet( min(2, num), (int) round(sqrt(num)) + 1 ); boolean ok = fibonacciNums.contains(num) || fibonacciDivs.stream().anyMatch( n -> (num % n == 0) && fibonacciNums.contains(num / n) ); //System.out.print("" + num + ": "); //System.out.println(fibonacciDivs); System.out.println(ok ? "TAK" : "NIE"); }); //tc } }
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 | import java.io.FileInputStream; import java.util.Scanner; import java.util.SortedSet; import java.util.TreeSet; import java.util.stream.IntStream; import static java.lang.Math.*; public class ilo { private static final SortedSet<Integer> fibonacciNums = new TreeSet<>(); private static void generateFibonacciNums() { int[] nums = new int[3]; nums[1] = 1; while (nums[2] <= 1_000_000_000) { fibonacciNums.add(nums[2]); nums[2] = nums[0] + nums[1]; System.arraycopy(nums, 1, nums, 0, 2); // shift array 1 position down } //System.out.println("" + fibonacciNums.size() + " " + fibonacciNums); } public static void main(String[] args) throws Exception { generateFibonacciNums(); Scanner sc = new Scanner(args.length == 0 ? System.in : new FileInputStream(args[0])); IntStream.rangeClosed(1, sc.nextInt()).forEach(tc -> { int num = sc.nextInt(); SortedSet<Integer> fibonacciDivs = fibonacciNums.subSet( min(2, num), (int) round(sqrt(num)) + 1 ); boolean ok = fibonacciNums.contains(num) || fibonacciDivs.stream().anyMatch( n -> (num % n == 0) && fibonacciNums.contains(num / n) ); //System.out.print("" + num + ": "); //System.out.println(fibonacciDivs); System.out.println(ok ? "TAK" : "NIE"); }); //tc } } |