TOJ 4115 Find the number

I forget mark value = minValue ,so I get WA one day.So sad a story.

The portal:http://acm.tju.edu.cn/toj/showp4115.html

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

double minValue;
long long Mod = 1e9 + 7;
int prime[14] = {3,5,7,11,13,17,19,23,29,31,37,41,43,47};
int best[14],temp[14];

void DFS(const int & depth,const int & now,const double & value){
    if(value >= minValue){
        return;
    }
    if(now == 1){
        if(value < minValue){
            minValue = value ;
            for(int i=0;i<14;i++){
                best[i] = temp[i];
            }
        }
        return ;
    }
    if(depth >= 14) return ;
    for(int i=1;i<=sqrt(now);i++){
        if(now % i == 0){
            int t = i;
            if(t > 1){
                temp[depth] = t - 1;
                DFS(depth+1,now/t,value+(t-1)*log(prime[depth]));
                temp[depth] = 0;
            }
            t = now / i;
            if(t > 1){
                temp[depth] = t - 1;
                DFS(depth+1,now/t,value+(t-1)*log(prime[depth]));
                temp[depth] = 0;
            }
        }
    }
}

long long Quick_pow(const int &x,int k){
    long long ret = 1,base = x;
    while(k){
        if(k&1){
            ret = (ret * base) % Mod;
        }
        base = (base * base) % Mod;
        k >>= 1;
    }
    return ret;
}

int main(void){
    //freopen("a.in","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF){
        memset(best,0,sizeof(best));
        memset(temp,0,sizeof(temp));
        minValue = (n-1)*log(3.0);
        best[0] = n - 1;
        DFS(0,n,0);
        long long ans = 1;
        for(int i=0;i<14;i++){
            ans = (ans * Quick_pow(prime[i],best[i])) % Mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值