Well-known Numbers(CodeForces - 225B )

Well-known Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Numbers k-bonacci (k is integer, k > 1) are a generalization of Fibonacci numbers and are determined as follows:

F(k, n) = 0, for integer n, 1 ≤ n < k;
F(k, k) = 1;
F(k, n) = F(k, n - 1) + F(k, n - 2) + … + F(k, n - k), for integer n, n > k.
Note that we determine the k-bonacci numbers, F(k, n), only for integer values of n and k.

You’ve got a number s, represent it as a sum of several (at least two) distinct k-bonacci numbers.

Input
The first line contains two integers s and k (1 ≤ s, k ≤ 109; k > 1).

Output
In the first line print an integer m (m ≥ 2) that shows how many numbers are in the found representation. In the second line print m distinct integers a1, a2, …, am. Each printed integer should be a k-bonacci number. The sum of printed integers must equal s.

It is guaranteed that the answer exists. If there are several possible answers, print any of them.
思路:
根据题意前k-1个k-bonacci 数是0 第k个和第k+1个是1,可以忽略前k个数 从第k+1个数开始 即k+1为1,根据题意,当i<=k的时候 第i个k-bonacci 数是前i-1个数的和,当i>k使,第k个数是第k个数前边的k个数的和
代码

#include<bits/stdc++.h>
using namespace std;
int v[100],kb[100],sum[100];
int main(){
    int s,k;
    int cnt=0;
    cin>>s>>k;
    kb[0]=sum[0]=1;
    int i;

    for(i=1;kb[i-1]<=s;i++){

        kb[i]=sum[i-1]-(i>k?sum[i-k-1]:0);
        sum[i]=sum[i-1]+kb[i];
    }
   // for(int j=1;j<i;j++)cout<<kb[j]<<endl;
    int l=i-1;
    while(s){
        while(s<kb[l])l--;
        s-=kb[l];
        v[++cnt]=kb[l];

        l--;
    }
    if(cnt==1){
        cout<<2<<endl<<0<<' '<<v[cnt];
    }
    else
    {
        cout<<cnt<<endl;
        for(int i=1;i<=cnt;i++)
            cout<<v[i]<<' ';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轩辕青山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值