#include <cstdio>
#include <cstring>
#include <algorithm>
#define clp(s,ss) memcpy(s,ss,sizeof(ss))
using namespace std;
const int N=500;
char pr[N];
int a[N],b[N],c1[N],c2[N],c3[N],d[N],f[N],s[N],ans[N];
void cls(int h[])
{
for(int i=1;i<=h[0];i++) h[i]=0;h[0]=0;
}
void init()
{
scanf("%s",pr); b[0]=strlen(pr);
for(int i=1;i<=b[0];i++) b[i]=pr[b[0]-i]-'0';
clp(f,b); a[0]=1;a[1]=1; d[0]=1;d[1]=3;
ans[0]=1;ans[1]=0;
}
void to_to(int h[],int len)
{
for(int i=1;i<=len+1;i++)
{
h[i+1]+=h[i]/10;
h[i]%=10;
}
int pos;
for(pos=len+2;h[pos]==0;pos--); h[0]=pos;
return ;
}
void add(int h1[],int h2[],int h3[])
{
int len=max(h2[0],h3[0]);
for(int i=1;i<=len;i++) h1[i]=h2[i]+h3[i];
to_to(h1,len);
}
void chu(int h[])
{
int sum=0; if(h[1]%2!=0) h[1]--;
for(int i=h[0];i>=1;i--)
{
sum=sum*10+h[i];
h[i]=sum/2;
sum%=2;
}
int i;
for(i=h[0]+2;h[i]==0;i--);h[0]=i;
}
bool pd(int h1[],int h2[])
{
if(h1[0]<h2[0]) return true;
if(h1[0]>h2[0]) return false;
for(int i=h1[0];i>=1;i--)
{
if(h1[i]>h2[i]) return false;
if(h1[i]<h2[i]) return true;
}
return true;
}
void add_(int h[],int num)
{
h[1]+=num;
to_to(h,h[0]);
}
void chen(int h3[],int h1[],int h2[])
{
cls(h3);
for(int i=1;i<=h1[0];i++)
for(int j=1;j<=h2[0];j++)
h3[i+j-1]+=h1[i]*h2[j];
to_to(h3,h1[0]+h2[0]+1);
}
bool solve(int h[])
{
chen(s,d,h);
chen(c2,h,h);
if(c2[0]>f[0]) return false;
add(s,s,c2);
chen(c3,c2,h);
if(c3[0]>f[0]) return false;
add(s,s,c3);
if(pd(s,f)) return true;
else return false;
}
int main()
{
init();
while(pd(a,b))
{
add(c1,a,b);chu(c1);
if(solve(c1)) {clp(ans,c1);clp(a,c1);add_(a,1);}
else {clp(b,c1);add_(b,-1);}
}
for(int i=ans[0];i>=1;i--) printf("%d",ans[i]);
return 0;
}
Vijos1375 大整数
最新推荐文章于 2019-09-14 18:46:32 发布