Codeforces 27E Number With The Given Amount Of Divisors (求約數個數為n的最小數)

_TCgogogo_發表於2015-09-08


Number With The Given Amount Of Divisors
time limit per test:2 seconds
memory limit per test:256 megabytes

Given the number n, find the smallest positive integer which has exactlyn divisors. It is guaranteed that for the givenn the answer will not exceed 1018.

Input

The first line of the input contains integer n (1 ≤ n ≤ 1000).

Output

Output the smallest positive integer with exactlyn divisors.

Sample test(s)
Input
4
Output
6
Input
6
Output
12

題目連結:http://codeforces.com/problemset/problem/27/E

題目大意:給一個數字n,求一個最小的數使得它的約數個數為n

題目分析:答案不超過1e18,首先假設每個素數都是一次冪,則前17個素數相乘就超出範圍了,因此只需要取前16個素數,其次對於最小的素數2,2的63次方剛好是範圍內的臨界值,因此,只需要列舉DFS即可,三個引數pos表示當前素數,val表示當前數值,num表示當前數字約數的個數,注意幾個剪枝,當val *p[pos] > ans或者約數個數已經大於n時,直接break,因為素數表是從小到大的,又因為要求約數個數恰好為n,因此如果num*(i + 1)不是n的約數,也不會得到n,就不用繼續搜了

#include <cstdio>
#include <iostream>
#define ull unsigned long long
using namespace std;
ull n, ans;
int p[16] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};

void DFS(int pos, ull val, ull num)
{
    if(num > n || pos > 15)
        return;
    if(num == n)
    {
        ans = min(ans, val);
        return;
    }
    for(int i = 1; i <= 63; i++)
    {
        if(val > ans / p[pos] || num * (i + 1) > n)
            break;
        val *= p[pos];
        if(n % (num * (i + 1)) == 0)
            DFS(pos + 1, val, num * (i + 1));
    }
    return;
}

int main()
{
    cin >> n;
    ans = 1ull << 63;
    DFS(0, 1, 1);
    cout << ans << endl;
}




相關文章