BZOJ1053(HAOI2007)反素数ant--暴搜

【链接】
bzoj1053

【解题报告】

一个数可以拆分为若干个质因子相乘的形式

x=pa11pa22pa33...pann

易知一个数的因数个数为 (a1+1)(a2+1)(a3+1)...(an+1)

因为小素数比大素数优。(因为因数个数只跟拆分的素数个数有关)。

所以只需要用前11个素数dfs就可以解决此题了。

#include<cstdio>
#include<cmath>
#define LL long long
using namespace std;
const int a[12]={2,3,5,7,11,13,17,19,23,29,31};
int n,ans,num;
inline char nc()
{
    static char buf[100000],*l,*r;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF; return *l++;
}
inline int Read()
{
    int res=0; char ch=nc();
    while (ch<'0'||ch>'9') ch=nc();
    while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=nc();
    return res;
}
void Dfs(int x,int now,int sum,int las)
{
    if (x>11)
     {
        if (sum>num) ans=now,num=sum;
        if (now<ans&&sum==num) ans=now;
        return;
     }
    for (int i=0,p=1; i<=las; i++,p*=a[x])
    {
        Dfs(x+1,now*p,sum*(i+1),i);
        if ((LL)now*p*a[x]>n) break;
    }
}
int main()
{
    freopen("1053.in","r",stdin);
    freopen("1053.out","w",stdout);
    n=Read(); ans=num=0;
    Dfs(0,1,1,log2(n));
    printf("%d",ans); 
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值