题目:http://codeforces.com/problemset/problem/27/E
题意:给一个数,求一个最小的正整数,使得它的因子个数为
。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 5 using namespace std; 6 typedef unsigned long long ULL; 7 const ULL INF = ~0ULL; 8 9 int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53}; 10 11 int n; 12 ULL ans; 13 14 void dfs(int dept,ULL tmp,int num) 15 { 16 if(num > n) return; 17 if(num == n && ans > tmp) ans = tmp; 18 for(int i=1;i<=63;i++) 19 { 20 if(ans / p[dept] < tmp) break; 21 dfs(dept+1,tmp *= p[dept],num*(i+1)); 22 } 23 } 24 25 int main() 26 { 27 while(cin>>n) 28 { 29 ans = INF; 30 dfs(0,1,1); 31 cout<<ans<<endl; 32 } 33 return 0; 34 }
题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1562
题意:求以内的因子最多的那个数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 5 using namespace std; 6 typedef unsigned long long ULL; 7 const ULL INF = ~0ULL; 8 9 int p[16] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53}; 10 11 ULL ans,n; 12 int best; 13 14 void dfs(int dept,ULL tmp,int num) 15 { 16 if(dept >= 16) return; 17 //num记录的因子个数,如果遇到更小的,就更新 18 if(num > best) 19 { 20 best = num; 21 ans = tmp; 22 } 23 //当因子个数相同时,取值最小的 24 if(num == best && ans > tmp) ans = tmp; 25 for(int i=1;i<=63;i++) 26 { 27 if(n / p[dept] < tmp) break; 28 dfs(dept+1,tmp *= p[dept],num*(i+1)); 29 } 30 } 31 32 int main() 33 { 34 while(cin>>n) 35 { 36 ans = INF; 37 best = 0; 38 dfs(0,1,1); 39 cout<<ans<<endl; 40 } 41 return 0; 42 }