码迷,mamicode.com
首页 > 其他好文 > 详细

(记忆话搜索)POI Fibonacci Representation

时间:2015-05-20 17:52:05      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Fibonacci Representation

Memory limit: 64 MB

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.

Input

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 (技术分享).

Output

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.

Example

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!