Description
Fib数列0,1,1,2,3,5,8,13,21。
给出一个数字,用FIB数列各项加加减减来得到。例如
10=5+5
19=21-2
17=13+5-1
1070=987+89-5-1
Input
In the first line of the standard input a single positive integer is given (1 <=P<=10) that denotes the number of queries. The following lines hold a single positive integer K each 1<=K<=10^17.
Output
For each query your program should print on the standard output the minimum number of Fibonacci numbers needed to represent the number k as their sum or difference.
Sample Input
1
1070
Sample Output
4
#include<iostream>
#include<cstdio>
#define LL long long
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
const int maxn = 1010;
LL t,x;
LL f[maxn];
int cal(LL x){
if(!x) return 0;
int i;
for(i = 1; f[i] < x; i++);
return cal(min(x-f[i-1], f[i]-x))+1;
}
int main() {
f[0] = f[1] = 1;
for(int i = 2; i <= 91; i++) f[i] = f[i-1]+f[i-2];
scanf(AUTO,&t);
while(t--) scanf(AUTO,&x), printf("%d\n",cal(x));
return 0;
}