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

      

  • 相关阅读:
    TabControl 切换 内嵌web页面直接响应滚动事件
    进程、应用程序域和对象上下文
    CSharp中的多线程——线程同步基础
    CSharp中的多线程——入门
    注重实效的程序员之快速参考指南
    学习语言技术快速入门——五步骤
    利用jQuery选择将被操作的元素
    CSharp中的多线程——使用多线程
    android开发文件介绍
    三角函数公式
  • 原文地址:https://www.cnblogs.com/water-full/p/4517535.html
Copyright © 2011-2022 走看看