ZOJ 2562 More Divisors 反素数 DFS
2015-08-02 22:05
399 查看
题解看代码系列。
和HDU4228类似。
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1562
和HDU4228类似。
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1562
//素数表 prime{0,2,3,5,7,11,13,17,19}; //如果一个数num = prime ^ (2 * n - 1) (基数的情况) 则它的因子数为 2 * n ; //如果一个数num = prime ^ (2 * n ) (偶数的情况) 则它的因子数为 2 * n + 1; (prime 是一个质素); //x = prime[1]^( k1 - 1 ) * prime[2] * ( k2 - 1 ) * prime[3] * ( k3 - 1 ) * ... * prime * ( kn - 1 ); //因子数为k , k = k1 * k2 * k3 * ... * kn; //找出比 N 小的 最小的 x 使得 k 最大; //k1 >= k2 >= k3 >= ... >= kn; #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; long long prime[20] = {0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53}; long long n; long long INF = (long long) 1 << 60; long long numk; long long totn ; long long fast_pow(int x,int k) { long long base = x; long long ans = 1; while(k) { if(k & 1) ans *= base; base *= base; k >>= 1; } return ans; } void DFS(int depth,long long num,int lim ,long long tot) { //depth 搜素深度 //num 因子数 //lim 是下个因子的最大数目 //tot 数的大小 //printf("depth : %d num : %lld tot : %lld\n",depth,num,tot); //printf("totn : %lld numk : %lld\n",totn,numk); if(tot > n) return ; if(tot <= 0) return ; if(num == numk && tot < totn) { totn = tot; } if(num > numk) { totn = tot; numk = num; } for(int i = 2 ; i <= lim; i++) { //枚举每层k值得大小; DFS(depth+1,num * i, i ,tot * fast_pow(prime[depth],i-1) ); } } void Deal_with() { while(~scanf("%lld",&n)) { numk = 0; totn = INF; //printf("totn : %lld numk : %lld\n",totn,numk); DFS(1,1,50,1); printf("%lld\n",totn); } } int main(void) { //freopen("a.in","r",stdin); Deal_with(); return 0; }
相关文章推荐
- MPICH2在windows7下的并行计算与拓展项目
- 勿忘初心,方得始终
- I/O流实现图片压缩
- 堆和栈是什么?它们在哪?
- leetcode--Clone Graph
- 【Nginx-反向代理服务器】基础知识(一)之事件处理机制
- CentOS 6.6 中设置Terminal快捷键
- iOS多线程编程之Grand Central Dispatch(GCD)介绍和使用
- Android初级第三次小结
- IOS开发初学--XCODE篇
- 数据结构基础 排序算法(一) 概念篇
- Rotate Image (LeetCode)
- Android初级第二次小结
- 初时MVC
- 双向循环链表
- 【CF 520D】Cubes
- 2015暑假集训
- leancloud js SDK 学习
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- Java中的内存泄漏分析说明