1. 程式人生 > >埃及分數(叠代加深搜索IDDFS)

埃及分數(叠代加深搜索IDDFS)

sca long long () ble emc printf can ast long

#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)