#include<ctime>
#include<cmath>
#include<cstdio>
using namespace std;
long long a,b,t,i,k,s[20],w[20],ans,z,ll;
long long gcd(long long a,long long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
long long my_sqrt(__int128 x)
{
long long l=sqrtl(x);
while((__int128)l*l<x)
l++;
return l;
}
bool check(long long &x,long long &y,long long p,long long q)
{
long long w=my_sqrt((__int128)p*p-4*q);
x=(p-w)/2;
y=(p+w)/2;
if(w*w!=p*p-4*q||(p+w)%2==1)
return false;
else
return true;
}
void dfs(long long t,long long ll,long long lim,long long p,long long q)
{
long long i,j,t1,t2,z,las=-1;
if(ll<q/p+1)
ll=q/p+1;
if(t==lim-1)
{
for(i=4*q/p/p+1;;i++)
{
z=check(t1,t2,p*i,q*i);
if(t1<ll||t2>=ans)
return;
if(z)
{
ans=t2;
s[k-1]=t1;
s[k]=t2;
for(j=1;j<=k;j++)
w[j]=s[j];
return;
}
if(las!=t1)
las=t1;
else
break;
}
for(i=t1;i>=ll;i--)
{
t1=p*i-q;
t2=q*i;
if(t2/t1>=ans)
break;
if(t2%t1!=0)
continue;
t2=t2/t1;
ans=t2;
s[k-1]=i;
s[k]=t2;
for(j=1;j<=k;j++)
w[j]=s[j];
return;
}
return;
}
for(i=ll;(lim-t+1)*q>p*i&&i<ans;i++)
if(q<=p*i)
{
t1=p*i-q;
t2=q*i;
z=gcd(t1,t2);
t1=t1/z;
t2=t2/z;
s[t]=i;
dfs(t+1,i+1,lim,t1,t2);
}
}
main()
{
scanf("%lld%lld",&a,&b);
t=gcd(a,b);
a=a/t;
b=b/t;
if(a==1)
{
printf("1\n");
printf("%lld",b);
return 0;
}
for(i=2;;i++)
{
t=250000;
while(t<=2500000000)
{
k=i;
ans=t+1;
dfs(1,2,i,a,b);
if(ans!=t+1)
break;
t=t*10;
}
if(t<=2500000000)
break;
}
printf("%lld\n",k);
for(i=1;i<=k;i++)
printf("%lld ",w[i]);
printf("\n");
}剪贴板
剪贴板 tv1iqu6h
剪贴板信息
剪贴板信息
- 发布
- 收录
- 更新