标签:
The Fibonacci sequence is a sequence of integers, called Fibonacci numbers, defined as follows:
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 . For example, the numbers 10, 19, 17, and 1070 can be minimally represented using, respectively, 2, 2, 3, and 4 Fibonacci numbers as follows:
Help Byteasar! Write a program that, for a given positive integer determines the minimum number of Fibonacci numbers required to represent as their sum or difference.
In the first line of the standard input a single positive integer is given () that denotes the number of queries. The following lines hold a single positive integer each ().
For each query your program should print on the standard output the minimum number of Fibonacci numbers needed to represent the number as their sum or difference.
For the input data:
1 1070
the correct result is:
4
Task author: Karol Pokorski.
<Submit a solution> [Done]
给出一些数,用最少的斐波那契亚数字组成
肯定是选最近的啊。。。
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> #include<map> #define INF 2e18 using namespace std; map<long long ,int> dp; long long n,top; long long fib[1010]; int dfs(long long x) { if(dp[x]) return dp[x]; int pos=lower_bound(fib+1,fib+1+top,x)-fib; if(fib[pos]==x) return 1; return dp[x]=min(dfs(x-fib[pos-1]),dfs(fib[pos]-x))+1; } int main() { int tt; scanf("%d",&tt); fib[1]=1,fib[2]=1; for(int i=3;fib[i-1]<=2e18;i++,top++) fib[i]=fib[i-1]+fib[i-2]; while(tt--) { scanf("%lld",&n); printf("%d\n",dfs(n)); } return 0; }
(记忆话搜索)POI Fibonacci Representation
标签:
原文地址:http://www.cnblogs.com/water-full/p/4517535.html