Advertisement
Guest User

Untitled

a guest
Aug 14th, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.77 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class F implements Runnable {
  5.     private MyScanner in;
  6.     private PrintWriter out;
  7.  
  8.     boolean[] prime;
  9.     List<Integer> primeList;
  10.     List<Long> answ;
  11.     int found;
  12.  
  13.     void sieve(int n) {
  14.         prime = new boolean[n];
  15.         Arrays.fill(prime, true);
  16.         prime[0] = prime[1] = false;
  17.         for (int i = 2; i * i < n; ++i) {
  18.             if (prime[i]) {
  19.                 for (int j = i * i; j < n; j += i) {
  20.                     prime[j] = false;
  21.                 }
  22.             }
  23.         }
  24.     }
  25.    
  26.     boolean isPrime(long n) {
  27.         if (n < 2) {
  28.             return false;
  29.         }
  30.         if (n == 2) {
  31.             return true;
  32.         }
  33.         if ((n & 1) == 0) {
  34.             return false;
  35.         }
  36.         for (int i = 3; i * i <= n; i += 2) {
  37.             if (n % i == 0) {
  38.                 return false;
  39.             }
  40.         }
  41.         return true;
  42.     }
  43.  
  44.     void findAll(int n) {
  45.         ArrayDeque<Long> l = new ArrayDeque<Long>();
  46.         l.addLast(1L * n);
  47.         l.addLast(1L);
  48.         for (int a : primeList) {
  49.             int sz = l.size() >> 1;
  50.             for (int i = 0; i < sz; ++i) {
  51.                 long cn = l.removeFirst();
  52.                 long ans = l.removeFirst();
  53.                 if (cn == 1) {
  54.                     answ.add(ans);
  55.                     ++found;
  56.                     continue;
  57.                 }
  58.                 if (a + 1 > cn) {
  59.                     continue;
  60.                 }
  61.                 if (1L * a * a + a + 1 > cn) {
  62.                     long only = cn - 1;
  63.                     if (isPrime(only)) {
  64.                         answ.add(ans * only);
  65.                         ++found;                       
  66.                     }
  67.                     continue;
  68.                 }
  69.                 l.addLast(cn);
  70.                 l.addLast(ans);
  71.                 long cs = 1, cur = a;
  72.                 for (int k = 0;; ++k) {
  73.                     cs += cur;
  74.                     if (cs > cn) {
  75.                         break;
  76.                     }
  77.                     if (cn % cs == 0) {
  78.                         l.addLast(cn / cs);
  79.                         l.addLast(ans * cur);
  80.                     }
  81.                     cur *= a;
  82.                 }
  83.             }
  84.  
  85.         }
  86.     }
  87.  
  88.     void count() {
  89.         int n = in.nextInt();
  90.         found = 0;
  91.         answ = new ArrayList<Long>();
  92.         findAll(n);
  93.         if (found == 0) {
  94.             out.print(-1);
  95.         } else {
  96.             Collections.sort(answ);
  97.             for (long i : answ) {
  98.                 out.print(i + " ");
  99.             }
  100.         }
  101.         out.println();
  102.     }
  103.  
  104.     private void solve() {
  105.         sieve(100000);
  106.         primeList = new ArrayList<Integer>();
  107.         for (int i = 0; i < prime.length; ++i) {
  108.             if (prime[i]) {
  109.                 primeList.add(i);
  110.             }
  111.         }
  112.         int t = in.nextInt();
  113.         for (int i = 0; i < t; ++i) {
  114.             count();
  115.         }
  116.     }
  117.  
  118.     @Override
  119.     public void run() {
  120.         in = new MyScanner();
  121.         out = new PrintWriter(System.out);
  122.         solve();
  123.         in.close();
  124.         out.close();
  125.     }
  126.  
  127.     public static void main(String[] args) {
  128.         new F().run();
  129.     }
  130.  
  131.     class MyScanner {
  132.         private BufferedReader br;
  133.         private StringTokenizer st;
  134.  
  135.         public MyScanner() {
  136.             br = new BufferedReader(new InputStreamReader(System.in));
  137.         }
  138.  
  139.         public MyScanner(String fileName) {
  140.             try {
  141.                 br = new BufferedReader(new FileReader(fileName));
  142.             } catch (FileNotFoundException e) {
  143.                 e.printStackTrace();
  144.             }
  145.         }
  146.  
  147.         public void close() {
  148.             try {
  149.                 br.close();
  150.             } catch (IOException e) {
  151.                 e.printStackTrace();
  152.             }
  153.         }
  154.  
  155.         public boolean hasNext() {
  156.             while (st == null || !st.hasMoreTokens()) {
  157.                 try {
  158.                     String s = br.readLine();
  159.                     if (s == null) {
  160.                         return false;
  161.                     }
  162.                     st = new StringTokenizer(s);
  163.                 } catch (IOException e) {
  164.                     e.printStackTrace();
  165.                     return false;
  166.                 }
  167.             }
  168.             return true;
  169.         }
  170.  
  171.         private String next() {
  172.             while (st == null || !st.hasMoreTokens()) {
  173.                 try {
  174.                     String s = br.readLine();
  175.                     if (s == null) {
  176.                         return null;
  177.                     }
  178.                     st = new StringTokenizer(s);
  179.                 } catch (IOException e) {
  180.                     e.printStackTrace();
  181.                     return null;
  182.                 }
  183.             }
  184.             return st.nextToken();
  185.         }
  186.  
  187.         public String nextLine() {
  188.             try {
  189.                 st = null;
  190.                 return br.readLine();
  191.             } catch (IOException e) {
  192.                 e.printStackTrace();
  193.                 return null;
  194.             }
  195.         }
  196.  
  197.         public int nextInt() {
  198.             return Integer.parseInt(next());
  199.         }
  200.  
  201.         public long nextLong() {
  202.             return Long.parseLong(next());
  203.         }
  204.  
  205.         public double nextDouble() {
  206.             return Double.parseDouble(next());
  207.         }
  208.     }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement