import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; public class ilo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int testsCount = in.nextInt(); List<Integer> numbers = new ArrayList<>(); List<String> answers = new ArrayList<>(testsCount); while (testsCount-- > 0) { numbers.add(in.nextInt()); answers.add("NIE"); } in.close(); List<Integer> copy = new ArrayList<>(numbers); Collections.sort(copy); HashSet<Integer> fib = new HashSet<>(); fib.add(0); int lastFib = 0; int currentFib = 1; int maxIter = copy.get(copy.size() - 1) / 2; for (int i = 1; i < maxIter && !copy.isEmpty();) { currentFib = i; i = lastFib + currentFib; lastFib = currentFib; fib.add(i); List<Integer> toRemove = new ArrayList<>(); for (Integer n : copy) { if (n % i == 0) { if (fib.contains(n / i)) { answers.set(numbers.indexOf(n), "TAK"); toRemove.add(n); } } } copy.removeAll(toRemove); toRemove.clear(); } for (String answer : answers) System.out.println(answer); } }
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 | import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Scanner; public class ilo { public static void main(String[] args) { Scanner in = new Scanner(System.in); int testsCount = in.nextInt(); List<Integer> numbers = new ArrayList<>(); List<String> answers = new ArrayList<>(testsCount); while (testsCount-- > 0) { numbers.add(in.nextInt()); answers.add("NIE"); } in.close(); List<Integer> copy = new ArrayList<>(numbers); Collections.sort(copy); HashSet<Integer> fib = new HashSet<>(); fib.add(0); int lastFib = 0; int currentFib = 1; int maxIter = copy.get(copy.size() - 1) / 2; for (int i = 1; i < maxIter && !copy.isEmpty();) { currentFib = i; i = lastFib + currentFib; lastFib = currentFib; fib.add(i); List<Integer> toRemove = new ArrayList<>(); for (Integer n : copy) { if (n % i == 0) { if (fib.contains(n / i)) { answers.set(numbers.indexOf(n), "TAK"); toRemove.add(n); } } } copy.removeAll(toRemove); toRemove.clear(); } for (String answer : answers) System.out.println(answer); } } |