一道ida的题目。
分子分母可以直接单独计算并约分。
其次是限定层数。
为了不TL,还得对分母进行缩放,看一下合理的取值范围。
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<list>
#include<vector>
#include<string>
#include<iostream>
#include<ctime>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
using namespace std;
#define ll long long
const int N=1e8+5;
ll x,y;
ll tot;
ll ans[N],a[N];
bool ida(ll now, ll x,ll y){
if (now==tot+1){
if (x!=0){
return false;
}
if (ans[tot]>a[tot]){
for (ll i = 1; i <= tot; ++i) {
ans[i]=a[i];
}
}
return true;
}
bool flag=false;
for (ll i = max((ll)(ceil(y/x)), a[now-1]+1); i <= ceil(y/x)*(tot-now+1); ++i) {
a[now]=i;
ll dx=x*i-y,dy=y*i;
ll g=__gcd(dx,dy);
dx/=g;
dy/=g;
if (ida(now+1, dx, dy)){
flag=true;
}
}
return flag;
}
int main() {
scanf("%lld %lld",&x, &y);
for (tot = 1;; ++tot) {
ans[tot]=0x3f3f3f3f;
if (ida(1,x,y)){
break;
}
}
for (ll i = 1; i <= tot; ++i) {
printf("%lld ",ans[i]);
}
return 0;
}