Codeforces 27E. Number With The Given Amount Of Divisors (暴力)

本文介绍了一个针对 CodeForces 平台上的问题 27E 的解决方案。通过使用深度优先搜索(DFS)来枚举所有可能的因数组合,并寻找最小的满足条件的乘积。此算法适用于较小的数据范围。

题目链接:http://codeforces.com/problemset/problem/27/E

暴力

 1 //#pragma comment(linker, "/STACK:102400000, 102400000")
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <vector>
 8 #include <cmath>
 9 #include <ctime>
10 #include <list>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long LL;
15 typedef pair <int, int> P;
16 const int N = 1e5 + 5;
17 int a[20];
18 LL ans, p[] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, limit = 1e18;
19 
20 void dfs(int num, int len) {
21     if(num == 1) {
22         LL res = 1;
23         for(int i = 1; i <= len; ++i) {
24             for(int j = 1; j < a[i]; ++j) {
25                 if(limit / p[i] <= res) {
26                     return ;
27                 }
28                 res *= p[i];
29             }
30         }
31         ans = min(ans, res);
32         return ;
33     }
34     for(int i = 2; i <= num; ++i) {
35         if(num % i == 0) {
36             a[len + 1] = i;
37             dfs(num / i, len + 1);
38         }
39     }
40 }
41 
42 int main()
43 {
44     int n;
45     cin >> n;
46     if(n == 1) {
47         cout << 1 << endl;
48         return 0;
49     } else if(n == 2) {
50         cout << 2 << endl;
51         return 0;
52     }
53     ans = limit;
54     for(int i = 1; i <= n; ++i) {
55         if(n % i == 0) {
56             a[1] = i;
57             dfs(n/i, 1);
58         }
59     }
60     cout << ans << endl;
61     return 0;
62 }

 

转载于:https://www.cnblogs.com/Recoder/p/5934638.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值