51nod 1060 最复杂的数(反素数)

分析
即为寻找反素数,讲解见acdreamer
具体操作为dfs,详情见代码
trick
注意temp\(\times\) 1ULL \(\times\)prime[k]会爆unsigned long long,有可能返回的是\(2^{64}-1\)取模后的值,所以要写成temp>n/prim[k]

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define F(i,a,b) for(int i=a;i<=b;++i)
#define R(i,a,b) for(int i=a;i<b;++i)
#define mem(a,b) memset(a,b,sizeof(a))

int t;
ll n;
ll prime[16]={0,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
ll bestnum,maxsum;
//当前枚举到的数;第k个素数;因子个数;质因子个数上限
void dfs(ll num,int k,ll sum,ll limit)
{
    if(sum>maxsum)
    {
        maxsum=sum;
        bestnum=num;
    }
    if((sum==maxsum)&&(bestnum>num)) bestnum=num;
    ll temp=num;
    if(k>15) return ;
    //if(sum==86016) printf("%I64d %I64d\n",num,prime[k] ); 
    for(ll i=1;i<=limit;++i)
    {
        //if(sum==86016) cout<<temp<<"*"<<prime[k]<<"="<<temp*1ULL*prime[k]<<endl;
        if(temp>n/prime[k]) break;
        temp*=prime[k];
        //if(temp==392432243895291584) printf("%I64d %d %I64d\n",sum,k,i );
        dfs(temp,k+1,sum*(i+1),i);
    }
}

int main()
{
    for(scanf("%d",&t);t--;)
    {
        scanf("%lld",&n);
        bestnum=1;maxsum=0;
        dfs(1,1,1,log(n)+1);
        printf("%I64d %I64d\n",bestnum,maxsum );
    }
    return 0;
}
posted @ 2017-08-05 09:10  遗风忘语  阅读(150)  评论(0编辑  收藏  举报