CF--27E(反素数)

2014-09-30 22:29:59

(参考)推荐博客:http://blog.csdn.net/ACdreamers/article/details/25049767

思路:做到poj2886,顺便刷了这题来学一下反素数。如何遍历一个数的所有因子呢?大致思路:首先看这个数的唯一素数分解式,如:n = p1^k1 * p2 * k2 * ... * pm^km,那么根据每个素因子pi,构建出DFS树的一层,如图,然后DFS,详情的话看我推荐的博客把~

 1 /*************************************************************************
 2     > File Name: e.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Tue 30 Sep 2014 09:45:55 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 #define lpos (pos << 1)
18 #define rpos (pos << 1|1)
19 #define getmid(l,r) (l + (r - l) / 2)
20 typedef long long ll;
21 const ll INF = (ll)1 << 62;
22 
23 int n;
24 int prime[20] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53};
25 ll ans;
26 
27 void Dfs(int dep,int num,ll val){
28     if(num > n) return;
29     if(num == n && val < ans){
30         ans = val;
31         return;
32     }
33     for(int i = 1; i <= 60; ++i){
34         if(ans / prime[dep] < val) break; //剪枝
35         Dfs(dep + 1,num * (i + 1),val *= prime[dep]);
36     }
37 }
38 
39 int main(){
40     //ios::sync_with_stdio(false);
41     cin >> n;
42     ans = INF;
43     Dfs(0,1,1);
44     cout << ans << endl;
45     return 0;
46 }

 

posted @ 2014-09-30 22:34  Naturain  阅读(129)  评论(0)    收藏  举报