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 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 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 ;
}
CF_225B _Well-known Numbers的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
随机推荐
- MVC知识总结(前序)
距离2015年的来临还有1天的时间,是时候总结一下今年的经过脑子的知识了,由于今年里工作中接触MVC的时间特别多,所以打算针对MVC这个东西的知识进行一个总结,好歹对得起在几个项目中用了MVC来进行开 ...
- Android开发之Handler
我们都知道应用程序开启后,安卓会开启一个主线程(UI线程),主线程管理UI控件,进行事件分发.那为什么会出现Handler呢? 例如你要是点击一个 Button ,Android会分发事件到Butto ...
- java main函数不执行?
今天脑袋短路,对于这个问题纠结了好久.这个问题具体是这样的: public class test { public static void main(String[] args) { test2 t ...
- WildFly 9.0.2+mod_cluster-1.3.1 集群配置
一.配置背景 最近使用WildFly 9.0.2作为中间件开发系统,给客户不熟的时候需要使用集群来进行负载均衡,一开始想到是使用Nginx.但是只通过Nginx使用 ip_hash 模式没有做到ses ...
- ns2出现Client: Handoff Attempt的情况解决
找到mac/mac-802_11.cc,这是系统本身一个bug,对于adhoc网络无需进行切换尝试. > if (*rcount == 3 && handoff == 0) {& ...
- C#学习笔记(3)
先理解一下方法重写和方法重载这2个概念: 1.方法重写(override):发生在父子类之间,子类重写父类中的方法,关键字是override. 2.方法重载(overload):一个类中有多个重名的方 ...
- 网站部署后,ie不能显示本地的图片
html:<div id="imgPreview" style='width:144px; height:80px;'> ...
- 浅谈Javase内存流程图
最近接触OOP面向对象,学习了OOP.this.super.package.extends还有override,整体来说如果不清楚内存的流程的话,对这些知识会很混淆,在老师的帮助下,花了一张图,能清楚 ...
- AVAudioSession 的 AVAudioSessionCategory 和 AVAudioSessionCategoryOptions 相关
AVAudioSessionCategory相关 AVAudioSessionCategoryAmbient 使用这个category的应用会随着静音键和屏幕关闭而静音.并且不会中止其它应用播放声音, ...
- UITableView自动滚动方法
1: NSUInteger rowCount = [self.tableView numberOfRowsInSection:0]; NSIndexPath* indexPath = [NSIndex ...