Number With The Given Amount Of Divisors
time limit per test:2 seconds
memory limit per test:256 megabytes
题目链接: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,就不用继续搜了
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;
}