CF_225B _Well-known Numbers

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 nn > 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 ≤ 109k > 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 mdistinct 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—Fibonacci数列,也就是,第0,1项是1,然后后面的第i项为前面k项之和,即f[i]=f[i-1]+.....f[i-k],(i>=k+1),然后输入整数s,k,输出能使得加起来和为s的m(m>=2)个不同的k—Fibonacci数,1<=s<=10^9,k>1,考虑到k最小为2时,f[50]>=10^10,所以对于任意k,满足条件的整数不会超过10^9,只需要存储前50个就可以了。这样s依次减去小于它的最大Fibonacci值,直到s为0.

题目要求最少输出2个数,所以遇到恰好为Fibonacci数的s值,可以输出一个0。

代码:

 #include<stdio.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define N 50
typedef long long ll;
ll f[N];
int main(void)
{
int s,k;
int i,j,ct=;
ll ans[N];
scanf("%d%d",&s,&k);
f[]=f[]=;
f[]=;
for(i=;i<N;i++)
{
if(i>=k+)
f[i]=*f[i-]-f[i-k-];//i>=k+1,递推公式:f[i]=2*f[i-1]-f[i-k-1]
else for(j=i-;j>=max(i-k,);j--)
f[i]+=f[j];//否则f[i]为前面k项之和
}
for(i=N-;i>;i--)
{
if((s>=f[i]))
{
s-=f[i];
ans[ct++]=f[i];
}
if(s==)
{
if(ct==){
printf("%d\n",ct+);
printf("0 ");
}
else printf("%d\n",ct);
for(i=;i<ct;i++)
printf("%I64d%c",ans[i],i==ct-?'\n':' ');
return ;
}
}
return ;
}
上一篇:[bzoj4011] [洛谷P3244] [HNOI2015] 落忆枫音


下一篇:深入浅出ClassLoader