首页 > 其他 > 详细

埃及分数(迭代加深搜索IDDFS)

时间:2019-02-07 18:45:34      阅读:349      评论:0      收藏:0      [点我收藏+]
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;

ll depth,a,b,flag,ans[100005],nans[100005];

inline ll gcd(ll a,ll b){ return b?gcd(b,a%b):a; }

inline void yuefen(ll &a,ll &b){ll eaa=gcd(a,b);a/=eaa;b/=eaa;}

inline void dfs(ll now,ll shen_fz,ll shen_fm,ll last_fm){
    yuefen(shen_fz,shen_fm);
    if(now==depth){
        if(shen_fz==1&&shen_fm>last_fm){
            if(flag&&shen_fm>=ans[depth])return;
            nans[depth]=shen_fm;
            flag=1;
            memcpy(ans,nans,sizeof(nans));
        }
        return;
    }
    for(ll i=last_fm+1;i<=ceil((double)(depth-now+1)*shen_fm/shen_fz);i++){
        nans[now]=i;
        dfs(now+1,shen_fz*i-shen_fm,shen_fm*i,i);
    }
}

int main(){
    scanf("%I64d%I64d",&a,&b);
    while(++depth){
        dfs(1,a,b,b/a-1);
        if(flag){
            for(ll i=1;i<=depth;i++)printf("%I64d ",ans[i]);
            return 0;
        }
    }
}

好吧算是比较易懂了

埃及分数(迭代加深搜索IDDFS)

原文:https://www.cnblogs.com/Y15BeTa/p/10355081.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!