More Divisors ZOJ - 2562(质因子组合)

    给一个最大为1e16的n,求[1,n]范围内因子数量最大的数字。

    类似CF27E , 直接分层搜索就可以了,每层的节点依次为质数的1-k次方。

    因为搜索的过程是求乘积的过程,不会进行很多次。

    

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
vector<LL>ve;
LL n,ans,k,sum;
void dfs(LL deep, LL temp, LL num_type)
{
    sum++;
    if(temp > n) return ;
    if(temp <= n && (ans < num_type || (ans == num_type && k > temp))) ans = num_type , k = temp;
    for(int i = 1 ; i < 63 ; i++)
    {
        if(n < temp * ve[deep]) break;
        dfs(deep + 1, temp *= ve[deep], num_type*(i+1));
    }
}
void get_prime()
{
    for(int i = 2 ; i < 100 ; i++)
    {
        bool flag = true;
        for(int j = 2 ; j < i ; j++)
        {
            if(i % j == 0)
            {
                flag = false;
                break;
            }
        }
        if(flag)
        {
            ve.push_back(i);
        }

    }
}
int main()
{
    get_prime();
    while(cin >> n)
    {
        sum = 0;
        ans = 1;
        dfs(0,1,1);
        cout << sum << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhaiqiming2010/article/details/80418607
ZOJ