zoukankan      html  css  js  c++  java
  • BZOJ 2796 POI2012 Fibonacci Representation

    2796: [Poi2012]Fibonacci Representation

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 479  Solved: 253
    [Submit][Status][Discuss]

    Description

    给出一个正整数x,问x最少能由多少个Fibonacci数加减算出。
    例如1070=987+89-5-1,因此x=1070时答案是4。

    Input

    第一行一个正整数q (q<=10),表示有q组输出。
    下面q行每行一个正整数x (x<=4*10^17)。

    Output

    输出q行,依次表示每个输出的答案。

    Sample Input

    1
    1070

    Sample Output

    4

    HINT

     

    Source

    dp即可

    #include <bits/stdc++.h>
    #define ll long long
    #define inf 1e18+10
    #define eps 1e-7
    using namespace std;
    inline ll read(){
    	ll x=0;int f=1;char ch=getchar();
    	while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    	while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    const int MAXN=1e6+10;
    ll fac[MAXN],top;
    map <ll,int> f;
    inline int dp(ll n){
    	if(f[n]) return f[n];
    	int b=lower_bound(fac,fac+top,n)-fac;
    	if(fac[b]==n) return 1;
    	return f[n]=min(dp(n-fac[b-1]),dp(fac[b]-n))+1;
    }
    int main(){
    	fac[1]=1;
    	for(int i=2;fac[i-1]<inf;i++){
    		fac[i]=fac[i-1]+fac[i-2];
    		top=i;
    	}
    	int t=read();
    	while(t--){
    		ll n=read();
    		printf("%d
    ",dp(n));
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    hdu 5961 传递(暴力搜索)
    hdu 3577 Fast Arrangement(线段树区间修改,求区间最小值)
    hdu 5898 odd-even number(数位dp)
    Python-编码
    Golang-教程
    Python-待
    Python_每日习题_0006_斐波那契数列
    计算机网络
    Python_老男孩练习题1
    Python_内置函数2_44
  • 原文地址:https://www.cnblogs.com/something-for-nothing/p/8295018.html
Copyright © 2011-2022 走看看