题目描述
The Fibonacci sequence is a sequence of integers, called Fibonacci numbers, defined as follows:
Fib0=0,Fib1=1,Fibn=Fibn−2+Fibn−1 for n>1Fib_{0}=0,Fib_{1}=1,Fib_{n}=Fib_{n-2}+Fib_{n-1}\ for\ n>1Fib0?=0,Fib1?=1,Fibn?=Fibn−2?+Fibn−1? for n>1
Its initial elements are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
Byteasar investigates representations of numbers as sums or differences of Fibonacci numbers. Currently he is wondering what is the minimum representation, i.e., one with the minimum number of (not necessarily different) Fibonacci numbers, for a given positive integer kkk . For example, the numbers 10, 19, 17, and 1070 can be minimally represented using, respectively, 2, 2, 3, and 4 Fibonacci numbers as follows:
10=5+510=5+510=5+5
19=21−219=21-219=21−2
17=13+5−117=13+5-117=13+5−1
1070=987+89−5−11070=987+89-5-11070=987+89−5−1
Help Byteasar! Write a program that, for a given positive integer kkk determines the minimum number of Fibonacci numbers required to represent kkk as their sum or difference.
给一个数,问最少可以用几个斐波那契数加加减减凑出来
例如 10=5+5 19=21-2
17=13+5-1
1070=987+89-5-1
输入输出格式
输入格式:
In the first line of the standard input a single positive integer ppp is given (1≤p≤101\le p\le 101≤p≤10 ) that denotes the number of queries. The following ppp lines hold a single positive integer kkk each (1≤k≤1×10171\le k\le 1\times 10^{17}1≤k≤1×1017 ).
多组数据
输出格式:
For each query your program should print on the standard
output the minimum number of Fibonacci numbers needed to represent the
number kkk as their sum or difference.
输入输出样例
输入样例#1:
1 1070
输出样例#1:
4
说明
给一个数,问最少可以用几个斐波那契数加加减减凑出来
Solution:
贪心水题,刷了那么多道斐波拉契,看到本题感觉简直水到爆了(红题难度)。
代码:
1 #include<bits/stdc++.h> 2 #define il inline 3 #define ll long long 4 using namespace std; 5 ll f[100],n,t; 6 il void getans(ll x){ 7 ll p=lower_bound(f+1,f+93,x)-f,q=p-1,tot=0; 8 while(x){ 9 x=min(f[p]-x,x-f[q]); 10 p=lower_bound(f+1,f+93,x)-f; 11 q=p-1; 12 tot++; 13 } 14 cout<<tot<<endl; 15 } 16 int main() 17 { 18 ios::sync_with_stdio(0); 19 cin>>n; 20 f[1]=f[2]=1; 21 for(int i=3;i<=100;i++)f[i]=f[i-1]+f[i-2]; 22 while(n--){ 23 cin>>t; 24 getans(t); 25 } 26 }
原文地址:https://www.cnblogs.com/five20/p/8810419.html