码迷,mamicode.com
首页 > 其他好文 > 详细

恰好有N个因子的最小正整数M

时间:2015-07-30 23:17:25      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

#include <cstdio>
#include <algorithm>
#include <cmath>K

int tmp[1000], cnt, rec[1000];
int prime[30] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 39, 41, 43, 47};
double ans, LOG[30];

void DFS(int pos, int val, int MAX, double clog) {
    if (clog > ans) return;
    if (val == 1) {
        cnt = pos;
        ans = clog;
        for (int i = 1; i < pos; i++)
            rec[i] = tmp[i];
        return;
    }

    for (int i = 1; i * i <= val && i <= MAX; i++) {
        if (val % i == 0) {
            if (i != 1) {
                tmp[pos] = i;
                DFS(pos+1, val/i, i, clog + LOG[pos] * (i-1));
            }
            if (val / i <= MAX && val / i != i) {
                tmp[pos] = val/i;
                DFS(pos+1, i, val/i, clog + LOG[pos] * (val/i - 1));
            }
        }
    }
}

int main() {
    int N;
    for (int i = 0; i < 16; i++)
        LOG[i] = log((double)prime[i]);
    while (scanf("%d", &N) != EOF) {
        ans = 0x3f3f3f3f;
        DFS(1, N, N, 0);
        long long M = 1;
        for (int i = 1; i < cnt; i++)
            for (int j = 1; j < rec[i]; j++)
                M *= prime[i];
        printf("%lld\n", M);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

恰好有N个因子的最小正整数M

标签:

原文地址:http://blog.csdn.net/kl28978113/article/details/47159013

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!