bzoj 1225 dfs + 一点点数论

思路:有一个公式  如果 x = a1 ^ b1 * a2 ^ b2 * ...... * an ^ bn 其中ai为质数,那么总共的因子个数为 (b1 + 1) * (b2 + 1) *....* (bn + 1),

可得最多只和前16个质数有关,那么我们dfs暴力枚举每个质数的指数个数,注意因为值很大,所以比较过程用了log,后面上个大数输出。

 
import java.math.BigInteger;
import java.util.*;
 
 
public class Main {
    static int n;
    static int prime[] = {0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
    static int ret[] = new int[17];
    static int tmp[] = new int[17];
    static double lg[] = new double[17];
    static double mn = 100000000000000000.0;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
         
        n = in.nextInt();
         
        for(int i = 1; i <= 16; i++) {
            lg[i] = Math.log(prime[i]);
        }
         
        dfs(0.0, n, 1, 1000000);
         
        BigInteger ans = BigInteger.valueOf(1);
        for(int i = 1; i <= 16; i++) {
            for(int j = 1; j <= ret[i]; j++) {
                ans = ans.multiply(BigInteger.valueOf(prime[i]));
            }
        }
        System.out.println(ans);
        in.close();
    }
     
    static void dfs(double x, int y, int z, int pre) {
        if(x >= mn) return;
        if(y == 1) {
            mn = x;
            for(int i = 1; i <= 16; i++) {
                ret[i] = 0;
                if(i <= z - 1) {
                    ret[i] = tmp[i];
                }
            }
            return;
        }
        if(z > 16) return;
        for(int i = 0; (i + 1) * (i + 1) <= y; i++) {
            if(y % (i + 1) == 0) {
                if(i != 0 && i <= pre) {
                    tmp[z] = i;
                    dfs(x + lg[z] * i, y / (i + 1), z + 1, i);
                }
                 
                if((i + 1) * (i + 1) != y && y / (i + 1) - 1 <= pre) {
                    tmp[z] = y / (i + 1) - 1;
                    dfs(x + lg[z] * (y / (i + 1) - 1), i + 1, z + 1, y /(i + 1) - 1);
                }
            }
        }
    }
}

 

posted @ 2018-06-12 20:48  NotNight  阅读(164)  评论(0编辑  收藏  举报