Original question:
The standard interpretation of the binary number 1010 is 8 + 2 = 10. An alternate-to-view the sequence "1010" is to use Fibonacci numbers as bases instead of powers of. For this problem, the terms of the Fibonacci sequence is:
1, 2, 3, 5, 8, 13, 21, ...
Where each clause is the sum of the preceding terms (note this there is only one 1 in the sequence as defined here). Using this scheme, the sequence "1010" could is interpreted as 1 5+0 3+1 2+0 1 = 7.
This representation is called a fibinary number.
Note that there are not always a unique fibinary representation of every number. For example the number could is represented as either 8 + 2 (10010) or as 5 + 3 + 2 (1110). To make the Fibinary
Representations unique, larger Fibonacci terms must always be used whenever possible (i.e. disallow 2 adjacent 1 ' s). Applying this rule to the number, means, would is represented as 8+2 (10010).
Write a program, that takes, valid fibinary numbers and prints the sum in fibinary form.
Input
The input file contains several test cases with a blank line between the consecutive.
Each test case consists in the lines with fibinary numbers. These numbers'll has at most digits.
Output
For the all test case, print the sum of the the and the both input numbers in fibinary form. It must is a blank line between the consecutive outputs.
Sample Input
10010
1
10000
1000
10000
10000
Sample Output
10100
100000
100100
English:
Give you two Fibonacci sequences, so you can ask for the number and the result is also expressed in Fibonacci sequences, requiring no more than two 1 neighbors.
#include <bits/stdc++.h> using namespace std; const int maxn=1000;/* Precision Digits */* (required) class and basic function definitions, usage similar to unsigned (non-negative) */class Bign {friend istream& operator>> (
istream&,bign&);/* Input Operation Fu Youyuan */friend ostream& operator<< (ostream&,const bign&);/* Output Operation 符友元 */ Friend Bign operator+ (const bign&,const bign&);/* Plus arithmetic Fu Youyuan * * Friend Bign operator* (const bign& const bign&);/* Multiplication sign Operation Fu Youyuan */friend Bign operator* (const bign&,int);/* High precision times low precision multiplication friend */friend Bign Opera tor-(const bign&,const bign&);/* Minus Operation Fu Youyuan */friend Bign operator/(const bign&,const bign&);/* Division Operation Fu Youyuan */friend Bign operator% (const bign&,const bign&);/* Modulo operation Fu Youyuan * * friend bool operator< (const BIGN&A Mp;,const bign&);/* logic is less than Fu Youyuan */friend bool Operator> (const bign&,const bign&);/* logic greater than Fu Youyuan */Fri end bool Operator<= (const bign&,const bign&);/* logic is less than or equal to Fu Youyuan */friend BOOL Operator>= (const bign&,const bign&);/* logic greater than or equal to Fu Youyuan */friend bool operator== (const bign&,const bign&); * Logic et Fu Youyuan * * friend bool operator!= (const bign&,const bign&);/* Logic varies Fu Youyuan */private:int LEN,S[MAXN]
;
Public:bign () {memset (s,0,sizeof (s)); len=1;}
Bign operator= (const char* num) {int i=0,ol;
Ol=len=strlen (num);
while (num[i++]== ' 0 ' &&len>1) len--;
Memset (s,0,sizeof (s));
for (i=0;i<len;i++) s[i]=num[ol-i-1]-' 0 ';
return *this;
} bign operator= (int num) {char S[MAXN];
sprintf (S, "%d", num);
*this=s;
return *this;
} bign (int num) {*this=num;}
Bign (const char* num) {*this=num;}
String str () const {int i;
String Res= ""; for (I=0;I≪len;i++) Res=char (s[i]+ ' 0 ') +res;
if (res== "") res= "0";
return res;
}
};
/* (optional) basic logical operator overloading */bool operator< (const bign& a,const bign& b) {int i;
if (A.len!=b.len) return a.len<b.len;
for (i=a.len-1;i>=0;i--) if (A.s[i]!=b.s[i]) return a.s[i]<b.s[i];
return false; } bool Operator> (const bign& a,const bign& b) {return b<a;} bool operator<= (const bign& A,const BIGN&A mp b) {return! (
A>B);} BOOL Operator>= (const bign& a,const bign& b) {return!
A<B);} BOOL Operator!= (const bign& a,const bign& b) {return a<b| |
A>b;} BOOL operator== (const bign& a,const bign& b) {return! a<b| |
A>B);}
/* (optional) addition operator overloading */bign operator+ (const bign& a,const bign& b) {int i,max= (a.len>b.len?a.len:b.len), t,c;
Bign sum;
sum.len=0; for (i=0,c=0;c| |
i<max;i++) {t=c; if (I<a.len) T+=a.s[i];
if (I<b.len) t+=b.s[i];
sum.s[sum.len++]=t%10;
C=T/10;
} return sum;
}/* (optional) multiplication operator overloading (high precision multiply high precision) */bign operator* (const bign& a,const bign& b) {int i,j;
Bign Res; for (i=0;i<a.len;i++) {for (j=0;j<b.len;j++) {res.s[i+j]+= (a.s[i
]*B.S[J]);
RES.S[I+J+1]+=RES.S[I+J]/10;
res.s[i+j]%=10;
}} Res.len=a.len+b.len;
while (res.s[res.len-1]==0&&res.len>1) res.len--;
if (Res.s[res.len]) res.len++;
return res;
}/* High precision times Low precision (note: The Bign*int order must not be reversed, or it will conflict with high precision by high precision */bign operator* (const bign& A,int b) {int i,t,c=0;
Bign Res;
for (i=0;i<a.len;i++) {t=a.s[i]*b+c;
res.s[i]=t%10;
C=T/10;
} Res.len=a.len; while (c!=0) {res.s[i++]=c%10;
c/=10;
res.len++;
} return res;
}/* (optional) subtraction operator overloading */bign operator-(const bign& a,const bign& b) {bign res;
int i,len= (a.len>b.len)? A.len:b.len;
for (i=0;i<len;i++) {res.s[i]+=a.s[i]-b.s[i];
if (res.s[i]<0) {res.s[i]+=10;
res.s[i+1]--;
}} while (res.s[len-1]==0&&len>1) len--;
Res.len=len;
return res;
}/* (optional) Division operator overloading (note: subtraction and multiplication operations and >= operations Fu Bixiang) */bign operator/(const bign& a,const bign& b) {int i,len=a.len;
Bign res,f;
for (i=len-1;i>=0;i--) {f=f*10;
F.s[0]=a.s[i];
while (f>=b) {f=f-b;
res.s[i]++;
}} while (res.s[len-1]==0&&len>1) len--;
Res.len=len;
return res; }
/*(optional) modulo operator overloading (note: subtraction and multiplication operations and >= operations Fu Bixiang) */bign operator% (const bign& a,const bign& b) {int i,len=a.len;
Bign res,f;
for (i=len-1;i>=0;i--) {f=f*10;
F.s[0]=a.s[i];
while (f>=b) {f=f-b;
res.s[i]++;
}} return F;
}/* (optional) operator overloading of X (Note: X method required) */bign& operator+= (bign& a,const bign& b) {a=a+b;
return A;
} bign& operator-= (bign& a,const bign& b) {a=a-b;
return A;
} bign& operator*= (bign& a,const bign& b) {a=a*b;
return A;
} bign& operator/= (bign& a,const bign& b) {a=a/b;
return A;
}/* Optional prefix ++/--with suffix ++/--(Note: addition required) */bign& operator++ (bign& a) {a=a+1;
return A;
} bign& operator++ (bign& a,int) {bign t=a;
a=a+1;
return t;
} bign& operator--(bign& a) { A=a-1;
return A;
} bign& operator--(bign& a,int) {bign t=a;
A=a-1;
return t;
} istream& operator>> (istream &in,bign& x) {string S;
in>>s;
X=s.c_str ();
return in;
} ostream& operator<< (ostream &out,const bign& x) {out<<x.str ();
return out;
} bign fib[130];
Bign Get_num (String a) {bign tmp=0;
for (int j=1,i=a.size () -1;i>=0;i--, J + +) {if (a[i]== ' 1 ') tmp+=fib[j];
} return TMP;
} fstream in,out;
int Find_max (int s,int e,bign tar) {for (int i=e;i>=s;i--) {if (Fib[i]<=tar) return i;
}} int main () {Ios::sync_with_stdio (false);//In.open ("In.txt");//Out.open ("OUT.txt");
fib[1]=1,fib[2]=2;
for (int i=3;i<=120;i++) fib[i]=fib[i-2]+fib[i-1];
Bign a,b,tmp;
String s1,s2;
Vector<int> ans;
int t=0; while (cin>>s1>>s2) {if (t++) cout<<endl;
Ans.clear ();
Tmp=0;
Tmp+=get_num (S1);
Tmp+=get_num (S2);
int inx=120;
while (true) {int ind=lower_bound (fib+1,fib+inx,tmp)-fib;
if (fib[ind]==tmp) {Tmp-=fib[ind];
Ans.push_back (Ind);
Break
} else {tmp-=fib[ind-1];
Ans.push_back (ind-1);
if (tmp==0) break;
}}/* while (true) {int Ind=find_max (1,INX,TMP);
Tmp-=fib[ind];
Ans.push_back (Ind);
if (tmp==0) break;
Inx=ind;
} */bool mark[501];
int ter=-1;
memset (Mark,0,sizeof (Mark));
for (auto X:ans) {if (x>ter) ter=x;
Mark[x]=1; } for (int i=ter;i>=1;i--) cout<<mark[i];
if (Ans.size () ==1&&ans.back () ==0) cout<<0;
cout<<endl;
}//In.close ();
Out.close ();
return 0;
}
Answer:
The high precision of the problem, Fibonacci 100th number will be more than unsigned long long.
First hit the table, note to hit the table to 120 bits, because two 100-bit Fibonacci numbers will be more than 100 bits (nonsense).
Then the two given sequence is converted to 10 summation, and then in Fibonacci number, each time from the back to the maximum can subtract the number of the minus number of the subscript to save.
The Fibonacci number to find the maximum minus can be achieved by two points.