Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class F implements Runnable {
- private MyScanner in;
- private PrintWriter out;
- boolean[] prime;
- List<Integer> primeList;
- List<Long> answ;
- int found;
- void sieve(int n) {
- prime = new boolean[n];
- Arrays.fill(prime, true);
- prime[0] = prime[1] = false;
- for (int i = 2; i * i < n; ++i) {
- if (prime[i]) {
- for (int j = i * i; j < n; j += i) {
- prime[j] = false;
- }
- }
- }
- }
- boolean isPrime(long n) {
- if (n < 2) {
- return false;
- }
- if (n == 2) {
- return true;
- }
- if ((n & 1) == 0) {
- return false;
- }
- for (int i = 3; i * i <= n; i += 2) {
- if (n % i == 0) {
- return false;
- }
- }
- return true;
- }
- void findAll(int n) {
- ArrayDeque<Long> l = new ArrayDeque<Long>();
- l.addLast(1L * n);
- l.addLast(1L);
- for (int a : primeList) {
- int sz = l.size() >> 1;
- for (int i = 0; i < sz; ++i) {
- long cn = l.removeFirst();
- long ans = l.removeFirst();
- if (cn == 1) {
- answ.add(ans);
- ++found;
- continue;
- }
- if (a + 1 > cn) {
- continue;
- }
- if (1L * a * a + a + 1 > cn) {
- long only = cn - 1;
- if (isPrime(only)) {
- answ.add(ans * only);
- ++found;
- }
- continue;
- }
- l.addLast(cn);
- l.addLast(ans);
- long cs = 1, cur = a;
- for (int k = 0;; ++k) {
- cs += cur;
- if (cs > cn) {
- break;
- }
- if (cn % cs == 0) {
- l.addLast(cn / cs);
- l.addLast(ans * cur);
- }
- cur *= a;
- }
- }
- }
- }
- void count() {
- int n = in.nextInt();
- found = 0;
- answ = new ArrayList<Long>();
- findAll(n);
- if (found == 0) {
- out.print(-1);
- } else {
- Collections.sort(answ);
- for (long i : answ) {
- out.print(i + " ");
- }
- }
- out.println();
- }
- private void solve() {
- sieve(100000);
- primeList = new ArrayList<Integer>();
- for (int i = 0; i < prime.length; ++i) {
- if (prime[i]) {
- primeList.add(i);
- }
- }
- int t = in.nextInt();
- for (int i = 0; i < t; ++i) {
- count();
- }
- }
- @Override
- public void run() {
- in = new MyScanner();
- out = new PrintWriter(System.out);
- solve();
- in.close();
- out.close();
- }
- public static void main(String[] args) {
- new F().run();
- }
- class MyScanner {
- private BufferedReader br;
- private StringTokenizer st;
- public MyScanner() {
- br = new BufferedReader(new InputStreamReader(System.in));
- }
- public MyScanner(String fileName) {
- try {
- br = new BufferedReader(new FileReader(fileName));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void close() {
- try {
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public boolean hasNext() {
- while (st == null || !st.hasMoreTokens()) {
- try {
- String s = br.readLine();
- if (s == null) {
- return false;
- }
- st = new StringTokenizer(s);
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- private String next() {
- while (st == null || !st.hasMoreTokens()) {
- try {
- String s = br.readLine();
- if (s == null) {
- return null;
- }
- st = new StringTokenizer(s);
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- return st.nextToken();
- }
- public String nextLine() {
- try {
- st = null;
- return br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement