Advertisement
DrBoat

Untitled

Nov 2nd, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. import static java.util.Arrays.binarySearch;
  2. import static java.util.Arrays.deepToString;
  3. import static java.util.Arrays.sort;
  4.  
  5. import java.io.*;
  6. import java.util.*;ф
  7.  
  8. public class lcm_nn_2 {
  9.  
  10.     static final int MOD = 1000000007;
  11.  
  12.     static int[] getDivisors(int n) {
  13.         List<Integer> d = new ArrayList<Integer>();
  14.         for (long i = 1; i * i <= n; i++) {
  15.             if (n % i != 0) {
  16.                 continue;
  17.             }
  18.             d.add((int) i);
  19.             if (i * i != n) {
  20.                 d.add((int) (n / i));
  21.             }
  22.         }
  23.         int[] ret = new int[d.size()];
  24.         for (int i = 0; i < d.size(); i++) {
  25.             ret[i] = d.get(i);
  26.         }
  27.         sort(ret);
  28.         return ret;
  29.     }
  30.  
  31.     static int lcm(int a, int b) {
  32.         return a / gcd(a, b) * b;
  33.     }
  34.  
  35.     static int gcd(int a, int b) {
  36.         return b == 0 ? a : gcd(b, a % b);
  37.     }
  38.  
  39.     static void solve() throws Exception {
  40.         int n = nextInt();
  41.         int k = nextInt();
  42.         out.println(solve(n, k));
  43.     }
  44.  
  45.     static int solve(int n, int k) {
  46.         int[] d = getDivisors(n);
  47.         int[][] dp = new int[k + 1][d.length];
  48.         int[][] lcm = new int[d.length][d.length];
  49.         for (int i = 0; i < d.length; i++) {
  50.             for (int j = i; j < d.length; j++) {
  51.                 lcm[i][j] = lcm[j][i] = binarySearch(d, lcm(d[i], d[j]));
  52.             }
  53.         }
  54.         dp[0][0] = 1;
  55.         for (int i = 0; i < d.length; i++) {
  56.             for (int j = 0; j < k; j++) {
  57.                 for (int t = 0; t < d.length; t++) {
  58.                     int z = lcm[i][t];
  59.                     dp[j + 1][z] += dp[j][t];
  60.                     if (dp[j + 1][z] >= MOD) {
  61.                         dp[j + 1][z] -= MOD;
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         return dp[k][d.length - 1];
  67.     }
  68.  
  69.     static BufferedReader br;
  70.     static StringTokenizer st;
  71.     static PrintWriter out;
  72.  
  73.     static void debug(Object... a) {
  74.         System.err.println(deepToString(a));
  75.     }
  76.  
  77.     static int nextInt() throws IOException {
  78.         return Integer.parseInt(next());
  79.     }
  80.  
  81.     static String next() throws IOException {
  82.         while (st == null || !st.hasMoreTokens()) {
  83.             String line = br.readLine();
  84.             if (line == null) {
  85.                 return null;
  86.             }
  87.             st = new StringTokenizer(line);
  88.         }
  89.         return st.nextToken();
  90.     }
  91.  
  92.     static long nextLong() throws IOException {
  93.         return Long.parseLong(next());
  94.     }
  95.  
  96.     static double nextDouble() throws IOException {
  97.         return Double.parseDouble(next());
  98.     }
  99.  
  100.     public static void main(String[] args) {
  101.         try {
  102.             File file = new File("lcm.in");
  103.             InputStream input = System.in;
  104.             OutputStream output = System.out;
  105.             if (file.canRead()) {
  106.                 input = (new FileInputStream(file));
  107.                 output = (new PrintStream("lcm.out"));
  108.             }
  109.             br = new BufferedReader(new InputStreamReader(input));
  110.             out = new PrintWriter(output);
  111.             solve();
  112.             out.close();
  113.             br.close();
  114.         } catch (Throwable e) {
  115.             e.printStackTrace();
  116.             System.exit(1);
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement