从下午开始写的,除了样例全部TLE,不知道剪枝哪里出了问题
#include<bits/stdc++.h>
#define int long long
using namespace std;
int gcd(int x,int y)
{
if(!y) return x;
return gcd(y,x%y);
}
struct fs{
int fz,fm;
}ys;
fs operator +(fs x,fs y)
{
x.fz*=y.fm;y.fz*=x.fm;
x.fm=y.fm=x.fm*y.fm;
fs z;
z.fm=x.fm;z.fz=x.fz+y.fz;
int h=gcd(z.fm,z.fz);
z.fm/=h;z.fz/=h;
return z;
}
bool operator ==(fs x,fs y)
{
if(x.fz*y.fm==y.fz*x.fm) return true;
else return false;
}
bool operator >(fs x,fs y)
{
if(x.fz*y.fm>y.fz*x.fm) return true;
else return false;
}
bool operator >=(fs x,fs y)
{
if(x.fz*y.fm>=y.fz*x.fm) return true;
else return false;
}
fs operator *(fs x,int y)
{
x.fz*=y;
int h=gcd(x.fm,x.fz);
x.fm/=h;x.fz/=h;
return x;
}
int d;
int q[1000005];
int ans=0x7f7f7f7f7f7f7f7f;
int flag;
int st[1000005],top;
void dfs(int t,fs x,int h)
{
// cout<<x.fz<<"/"<<x.fm<<" "<<ys.fz<<"/"<<ys.fm<<endl;
if(x==ys)
{
flag=1;
if(ans>st[top])
{
ans=st[top];
for(int i=1;i<=top;i++)
q[i]=st[i];
}
return;
}
if(t>d) return;
fs s;
for(int i=h+1;i<=1e7;i++)
{
s.fz=1;
s.fm=i;
if((s+x)>ys) continue;
if(i>=ans) return;
if(ys>=((s*(d-t+1))+x)) return;
st[++top]=i;
dfs(t+1,x+s,s.fm);
// if(flag) return;
top--;
}
}
signed main()
{
cin>>ys.fz>>ys.fm;
for(d=1;!flag;d++)
{
// cout<<d<<endl;
dfs(1,{0,1},0);
}
for(int i=1;i<d-1;i++)
printf("%d ",q[i]);
return 0;
}