Fibonacci Time Limit: 2000MS Memory limit: 131072K 題目描述
Fibonacci numbers are well-known as follow:
Now given an integer N, please find out whether N can be represented as the sum of several Fibonacci numbers in such a way that the sum does not include any two consecutive Fibonacci numbers. 輸入 Multiple test cases, the first line is an integer T (T<=10000), indicating the number of test cases.
Each test case is a line with an integer N (1<=N<=109). 輸出 One line per case. If the answer don’t exist, output “-1” (without quotes). Otherwise, your answer should be formatted as “N=f1+f2+…+fn”. N indicates the given number and f1, f2, … , fn indicating the Fibonacci numbers in ascending order. If there are multiple ways, you can output any of them.
樣本輸入
4
5
6
7
100
樣本輸出
5=5
6=1+5
7=2+5
100=3+8+89
提示 來源 “浪潮杯”山東省第七屆ACM大學生程式設計競賽
樣本程式 暴力打表然後用n遞減就ok了 ACcode:
#include <bits/stdc++.h>#define maxn 1005#define mod 1000000007#define ll long longusing namespace std;ll dp[55],ans[55];int main(){ dp[0]=dp[1]=1; dp[2]=2; for(int i=3;;++i){ dp[i]=dp[i-1]+dp[i-2]; if(dp[i]>1e9)break; } int n,loop; scanf("%d",&loop); while(loop--){ scanf("%d",&n); printf("%d=",n); int i=44,tot=0; while(n){ if(n>=dp[i]){ ans[++tot]=dp[i]; n-=dp[i]; } --i; } for(int i=tot;i>1;--i)cout<<ans[i]<<"+"; cout<<ans[1]<<'\12'; } return 0;}