求最小的因子个数为n个正整数
typedef unsigned long long ULL;
const ULL INF = ~0ULL;
const int MAXP = 16;
int prime[MAXP] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
int n;
ULL ans;
void dfs(int dept, ULL tmp, int num, int pre)
{
if (num > n)
{
return;
}
if (num == n && ans > tmp)
{
ans = tmp;
}
for (int i = 1; i <= pre; i++)
{
if (ans / prime[dept] < tmp)
{
break;
}
dfs(dept + 1, tmp *= prime[dept], num * (i + 1), i);
}
}
int main()
{
while (cin >> n)
{
ans = INF;
dfs(0, 1, 1, 15);
cout << ans << endl;
}
return 0;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
求n以内的因子最多的数(不止一个则取最小)
typedef long long ll;
const int MAXP = 16;
const int prime[MAXP] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53};
ll n, res, ans;
void dfs(ll cur, ll num, int key, ll pre)
{
if (key >= MAXP)
{
return ;
}
else
{
if (num > ans)
{
res = cur;
ans = num;
}
else if (num == ans)
{
res = min(cur, res);
}
ll i;
for ( i = 1; i <= pre; i++)
{
if (cur <= n / prime[key])
{
cur *= prime[key];
dfs(cur, num * (i + 1), key + 1, i);
}
else
{
break;
}
}
}
}
void solve()
{
res = 1;
ans = 1;
dfs(1, 1, 0, 15);
cout << res << ' ' << ans << endl;
}
int main(int argc, const char * argv[])
{
int T;
cin >> T;
while (T--)
{
cin >> n;
solve();
}
return 0;
}