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;
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/NMfloat/article/details/46821047
文章标签: TOJ acm it
上一篇TOJ 4109 Cyuunibyou's problem
下一篇TOJ 4114 Evaluate Xor
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭