您的位置:首页 > 其它

山东省第7届省赛 B题 Fibonacci

2016-06-10 19:27 225 查看

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 long
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: