(记忆话搜索)POI Fibonacci Representation

本文探讨了如何通过选择最接近的斐波那契数来最小化给定正整数的表示,包括算法实现及实例解析。

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;
}

  

转载于:https://www.cnblogs.com/water-full/p/4517535.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值