Advertisement
Guest User

Untitled

a guest
Jun 28th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.17 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4. import java.awt.geom.*;
  5.  
  6. import static java.lang.Math.*;
  7.  
  8. public class Solution implements Runnable {
  9.  
  10.     public void solve() throws Exception {
  11.         int n = sc.nextInt();
  12.         ArrayList<Integer> divlist = new ArrayList<>();
  13.         for (int i = 1; i * i <= n; i++) {
  14.             if (n % i == 0) {
  15.                 divlist.add(i);
  16.                 if (i * i != n) {
  17.                     divlist.add(n / i);
  18.                 }
  19.             }
  20.         }
  21.        
  22.         int cnt = divlist.size();
  23.         int[] divisors = new int[cnt];
  24.         for (int i = 0; i < cnt; i++) {
  25.             divisors[i] = divlist.get(i);
  26.         }
  27.         Arrays.sort(divisors);
  28.        
  29.         int[][] dp = new int[cnt][cnt];
  30.         int[][] fromi = new int[cnt][cnt];
  31.         int[][] fromj = new int[cnt][cnt];
  32.         int[][] factj = new int[cnt][cnt];
  33.         for (int i = 0; i < cnt; i++) {
  34.             Arrays.fill(fromi[i], -1);
  35.             Arrays.fill(fromj[i], -1);
  36.         }
  37.         Arrays.fill(dp[0], 1);
  38.         Arrays.fill(factj[0], 0);
  39.        
  40.         for (int i = 1; i < cnt; i++) {
  41.             for (int j = 0; j < cnt; j++) {
  42.                 if (j > 0) {
  43.                     dp[i][j] = dp[i][j - 1];
  44.                     fromi[i][j] = fromi[i][j - 1];
  45.                     fromj[i][j] = fromj[i][j - 1];
  46.                     factj[i][j] = factj[i][j - 1];
  47.                 }
  48.                 if (divisors[i] % divisors[j] != 0) {
  49.                     continue;
  50.                 }
  51.                 int previ = Arrays.binarySearch(divisors, divisors[i] / divisors[j]);
  52.                 int prevj = j - 1;
  53.                 if (prevj >= 0 && prevj < j && dp[previ][prevj] != 0 && dp[previ][prevj] + 1 > dp[i][j]) {
  54.                     dp[i][j] = dp[previ][prevj] + 1;
  55.                     fromi[i][j] = previ;
  56.                     fromj[i][j] = prevj;
  57.                     factj[i][j] = j;
  58.                 }
  59.             }
  60.         }
  61.        
  62.         int bestj = 0;
  63.         for (int i = 0; i < cnt; i++) {
  64.             if (dp[cnt - 1][i] > dp[cnt - 1][bestj]) {
  65.                 bestj = i;
  66.             }
  67.         }
  68.        
  69.         int curi = cnt - 1;
  70.         int curj = bestj;
  71.         out.println(dp[curi][curj]);
  72.         do {
  73.             curj = factj[curi][curj];
  74.             out.print(divisors[curj] + " ");
  75.             int temp = fromi[curi][curj];
  76.             curj = fromj[curi][curj];
  77.             curi = temp;
  78.         } while (curi != -1);
  79.         out.println();
  80.     }
  81.    
  82.     final String filename = "proddiff";
  83.  
  84.     static Throwable uncaught;
  85.  
  86.     BufferedReader in;
  87.     FastScanner sc;
  88.     PrintWriter out;
  89.  
  90.     @Override
  91.     public void run() {
  92.         try {
  93.             in = new BufferedReader(new FileReader(filename + ".in"));
  94.             out = new PrintWriter(new FileWriter(filename + ".out"));
  95.             sc = new FastScanner(in);
  96.             solve();
  97.         } catch (Throwable uncaught) {
  98.             Solution.uncaught = uncaught;
  99.         } finally {
  100.             out.close();
  101.         }
  102.     }
  103.  
  104.     public static void main(String[] args) throws Throwable {
  105.         Thread thread = new Thread(null, new Solution(), "", (1 << 26));
  106.         thread.start();
  107.         thread.join();
  108.         if (Solution.uncaught != null) {
  109.             throw Solution.uncaught;
  110.         }
  111.     }
  112.  
  113. }
  114.  
  115. class FastScanner {
  116.  
  117.     BufferedReader in;
  118.     StringTokenizer st;
  119.  
  120.     public FastScanner(BufferedReader in) {
  121.         this.in = in;
  122.     }
  123.  
  124.     public String nextToken() throws Exception {
  125.         while (st == null || !st.hasMoreTokens()) {
  126.             st = new StringTokenizer(in.readLine());
  127.         }
  128.         return st.nextToken();
  129.     }
  130.  
  131.     public int nextInt() throws Exception {
  132.         return Integer.parseInt(nextToken());
  133.     }
  134.  
  135.     public long nextLong() throws Exception {
  136.         return Long.parseLong(nextToken());
  137.     }
  138.  
  139.     public double nextDouble() throws Exception {
  140.         return Double.parseDouble(nextToken());
  141.     }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement