CF #27E Number With The Given Amount Of Divisors 已知因子个数求最小

题意

  已知一个数X有K个因子,求最小的X。

解法

  搜索剪枝

  对于任意整数 

  其因子个数为: 

  意味着 

  因为需要最小的整数 N, 则我们使用小的质因子总是最优.

  所以我们枚举 

  

#include<stdio.h>

typedef long long LL;
const LL INF = 1000000000000000010ll; 
int p[12] = { 2,3,5,7,11,13,17,19,23,27,29,31}; 
int N; 
void dfs( LL &ans, int i, LL x, int n ){
    if( n > N ) return; // 基本减枝
    if( n == N && ans > x ) ans = x;
    for(int j = 1; j <= 60; j++){
        if( ans/p[i] < x ) break;  // 若 ans < x*p[i] , 则当前p[i]的 j次不可能是最优.就没必要往后面枚举了.
        dfs( ans, i+1, x*=p[i], n*(j+1) );    
    }    
}
int main(){ 
    while( scanf("%d",&N) != EOF){ 
        LL ans = INF;
        dfs( ans, 0, 1, 1 );
        printf("%I64d\n", ans );    
    }
    return 0;    
}

  

原文地址:https://www.cnblogs.com/yefeng1627/p/2993134.html