import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * Created with IntelliJ IDEA. * User: adam * Date: 06.05.14 * Time: 18:42 */ public class ilo { private final static String TAK = "TAK"; private final static String NIE = "NIE"; public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); long [] input = new long[t]; for(int i =0; i < t ; i++) { input[i] = Long.parseLong(br.readLine()); } List<Boolean> res = resolve(input); for(Boolean r:res) { System.out.println(r ? TAK : NIE); } } public static List<Boolean> resolve(long[] input) { List<Boolean> results = new ArrayList<Boolean>(); long max = Long.MIN_VALUE; for(Long l:input) { if(l > max) { max = l; } } List<Long> fbs = findFibonacci(max); for(Long l:input) { results.add(isIlo(l, fbs)); } return results; } public static List<Long> findFibonacci(long max) { List<Long> fbs = new ArrayList<Long>(); fbs.add(0L); fbs.add(1L); long curr = 1; while(true) { curr = fbs.get(fbs.size() - 2) +fbs.get(fbs.size() - 1); if(curr > max) { break; } fbs.add(curr); } return fbs; } private static boolean isIlo(long in, List<Long> fbs) { if(in == 0) { return true; } List<Long> dzieli = new ArrayList<Long>(); for(Long l:fbs) { if(l > in) { break; } if(l != 0L) { if(in % l == 0) { dzieli.add(l); } } } int index0, index1; index0 = index1 = 0; while(true) { long ilo = dzieli.get(index0)*dzieli.get(index1); if (ilo == in) { return true; } else if(ilo < in) { index1++; if(index1 >= dzieli.size()) { index0++; if(index0 >= dzieli.size()) { return false; } index1 = index0; } } else if(ilo > in) { index0++; if(index0 >= dzieli.size()) { return false; } else if(index0*index0 > in) { return false; } index1 = index0; } } } }
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; /** * Created with IntelliJ IDEA. * User: adam * Date: 06.05.14 * Time: 18:42 */ public class ilo { private final static String TAK = "TAK"; private final static String NIE = "NIE"; public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); long [] input = new long[t]; for(int i =0; i < t ; i++) { input[i] = Long.parseLong(br.readLine()); } List<Boolean> res = resolve(input); for(Boolean r:res) { System.out.println(r ? TAK : NIE); } } public static List<Boolean> resolve(long[] input) { List<Boolean> results = new ArrayList<Boolean>(); long max = Long.MIN_VALUE; for(Long l:input) { if(l > max) { max = l; } } List<Long> fbs = findFibonacci(max); for(Long l:input) { results.add(isIlo(l, fbs)); } return results; } public static List<Long> findFibonacci(long max) { List<Long> fbs = new ArrayList<Long>(); fbs.add(0L); fbs.add(1L); long curr = 1; while(true) { curr = fbs.get(fbs.size() - 2) +fbs.get(fbs.size() - 1); if(curr > max) { break; } fbs.add(curr); } return fbs; } private static boolean isIlo(long in, List<Long> fbs) { if(in == 0) { return true; } List<Long> dzieli = new ArrayList<Long>(); for(Long l:fbs) { if(l > in) { break; } if(l != 0L) { if(in % l == 0) { dzieli.add(l); } } } int index0, index1; index0 = index1 = 0; while(true) { long ilo = dzieli.get(index0)*dzieli.get(index1); if (ilo == in) { return true; } else if(ilo < in) { index1++; if(index1 >= dzieli.size()) { index0++; if(index0 >= dzieli.size()) { return false; } index1 = index0; } } else if(ilo > in) { index0++; if(index0 >= dzieli.size()) { return false; } else if(index0*index0 > in) { return false; } index1 = index0; } } } } |