ZOJ 2562 More Divisors 反素数 DFS

来源:互联网 发布:交换机端口电压 编辑:程序博客网 时间:2024/05/22 07:51

题解看代码系列。

和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[n] * ( 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;}



0 0
原创粉丝点击