1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include<stdio.h> #include<math.h> #include<string.h> long long a,b,ans[10000],a1[10000],num; long long gcd(long long k1,long long k2){ if(k2>k1){ long long remain=k1; k1=k2; k2=remain; } while(k1%k2!=0){ long long remain=k1%k2; k1=k2; k2=remain; } return k2; } int dfs(int step,int lim_step,long long remain_a,long long remain_b){ if(step==lim_step){ if(remain_a!=0||remain_b>1e7)return 0; if(a1[step]<ans[step]){ for(int i=1;i<=step;i++){ ans[i]=a1[i]; } } return 1; } else{ bool flag=0; int beg=ceil(1.0*remain_b/remain_a); int remain_c=beg; if(beg<=a1[step]){ beg=a1[step]+1; } long long end=remain_c*(num-step); if(end>1e7){ end=1e7; } for(int i=beg;i<=end;i++){ long long remain_a1=0,remain_b1=0; long long gcd1=gcd(i,remain_b); remain_b1=remain_b*i/gcd1; remain_a1=remain_a*(remain_b1/remain_b)-remain_b1/i; a1[step+1]=i; if(dfs(step+1,lim_step,remain_a1,remain_b1)==1){ flag=1; } } return flag; } } int main(){ scanf("%lld%lld",&a,&b); memset(ans,127,sizeof(ans)); for(num=1;;num++){ if(dfs(0,num,a,b)==1){ break; } } for(int i=1;i<=num;i++){ printf("%lld ",ans[i]); } return 0; }
|