zoukankan      html  css  js  c++  java
  • (记忆话搜索)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
    ",dfs(n));
        }
        return 0;
    }
    

      

  • 相关阅读:
    C# 不用添加WebService引用,调用WebService方法
    贪心 & 动态规划
    trie树 讲解 (转载)
    poj 2151 Check the difficulty of problems (检查问题的难度)
    poj 2513 Colored Sticks 彩色棒
    poj1442 Black Box 栈和优先队列
    啦啦啦
    poj 1265 Area(pick定理)
    poj 2418 Hardwood Species (trie树)
    poj 1836 Alignment 排队
  • 原文地址:https://www.cnblogs.com/water-full/p/4517535.html
Copyright © 2011-2022 走看看