51Nod-1060-最复杂的数(反素数)

来源:互联网 发布:刺身 寄生虫 知乎 编辑:程序博客网 时间:2024/06/06 02:50


1060 最复杂的数
题目来源: Ural 1748
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
 收藏
 关注
把一个数的约数个数定义为该数的复杂程度,给出一个n,求1-n中复杂程度最高的那个数。
例如:12的约数为:1 2 3 4 6 12,共6个数,所以12的复杂程度是6。如果有多个数复杂度相等,输出最小的。
Input
第1行:一个数T,表示后面用作输入测试的数的数量。(1 <= T <= 100)第2 - T + 1行:T个数,表示需要计算的n。(1 <= n <= 10^18)
Output
共T行,每行2个数用空格分开,第1个数是答案,第2个数是约数的数量。
Input示例
5110100100010000
Output示例
1 16 460 12840 327560 64

对于每一个数,求其约数个数,可以用素数唯一分解定理求得。但是若想求出1~n的所有数中,某个数字的约数个数最多。就需将若干个素数相乘。

对于搜索的剪枝来说:

2^i *3^j * 5^k

如果要使得数字尽量小,那么必定i>=j>=k。因此可以约束搜索时,每个素数的个数

#include <stdio.h>#include <cstring>#include <algorithm>#include <iostream>#include <cmath>using namespace std;const int maxn = 16;const int prime[maxn] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,53 };typedef long long ll;ll a[maxn];ll n,ans,num;//当前数字 约数个数 第几个素数 之前素数的幂void dfs(ll x,ll res,ll ced,ll pre){    if(ced>=maxn)        return ;    else{        if(res>num)        {            num=res;            ans=x;        }        else if(res==num)        {            ans=min(ans,x);        }        for(ll i=1;i<=pre;i++)        {            if(x<=n/prime[ced])            {                x*=prime[ced];                dfs(x,res*(i+1),ced+1,i);            }        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        scanf("%lld",&n);        ans=0,num=0;        dfs(1,1,0,64);        printf("%lld %lld\n",ans,num);    }}





原创粉丝点击