Codeforces225B - Well-known Numbers
Description
定义\(k\)-bonacci数列\(\{F_n\}\):\(F_i=0 \ (i<k),F_i=1 \ (i=k),F_i=\sum_{j=i-k}^{i-1}F_j\)
给出\(s(s\leq10^9)\)和\(k(k\leq10^9)\),将\(s\)拆成若干个\(k\)-bonacci数之和。
Solution
结论:重复从\(s\)中减掉最大的\(F_i\),一定能使\(s=0\)。
可以用数学归纳法证明。
若对于正整数\(k\),\(\forall s\in [0,F_k-1]\)该结论成立,则\(\forall s\in [F_k,F_{k+1}-1]\),其下最大的\(F_i\)为\(F_k\),而\(s-F_k\in [0,F_{k-1}-1]\),其必然也能按上述方法减至0。
而因为\(k=1\)时该结论成立,所以\(\forall s\)该结论均成立。
Code
//Well-known Numbers
#include <cstdio>
#include <algorithm>
using namespace std;
int const N=1e5+10;
long long f[N];
int n,m,ans[N];
int main()
{
int s,k; scanf("%d%d",&s,&k);
int n; f[1]=1;
for(n=2;f[n-1]<s;n++)
for(int j=max(1,n-k);j<=n-1;j++) f[n]+=f[j];
int m=0;
for(int i=n-1;i>=1&&s;i--) if(f[i]<=s) ans[++m]=f[i],s-=f[i];
if(m<2) ans[++m]=0;
printf("%d\n",m);
for(int i=1;i<=m;i++) printf("%d ",ans[i]);
puts("");
return 0;
}
P.S.
看标签猜结论系列binary search greedy number theory。不过根本不需要binary search啊!
Codeforces225B - 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 ...
随机推荐
- solr 常见异常
solr4.3本地数据提交异常分析 (2013-06-19 16:03:15) 转载▼ 异常一. Exception in thread "main" java.lang.No ...
- Web service简介 与servletContext的参数
Web service顾名思义是基于web的服务,它是一种跨平台,跨语言的服务. 我们可以这样理解它,比如说我们可以调用互联网上查询天气信息的web服务,把它嵌入到我们的B/S程序中,当用户从我们的网 ...
- Ionic之ui-sref引入图片,图片部分挡住解决方案
ionic图片设置大小跟图片像素相同,但是使用ui-sref="parkInfo"上半部分图片会挡住,增加height的高度,就可以显示原本图片 页面: <ion-conte ...
- 写给技术lead的招聘指南
工作这么久,面试过的工程师不下两三百人.大部份招到的人都比靠谱当然也有失败的例子.把亲身经历总结如下: 1. 什么人一定不能招: 理解能力差: 对你提出的问题,答不对题,重复提问.面试官可以在面试当中 ...
- ios项目中引用其他开源项目
1. 将开源项目的.xcodeproj拖入项目frameworks 2. Build Phases下 Links Binary With Libraries 引入.a文件.Target Depende ...
- QProcess执行带管道的shell命令
QStringList options; options << "-c" << "ls -l | grep a | sort"; QPr ...
- 自制Jquery下拉框插件
(function ($) { $.fn.select3 = function (option) { $(this).each(function () { var _this = $(this); v ...
- java_日期和时间
1.System类中的currentTimeMillis:1970年1.1到现在的毫秒数 public class DateTest { public static void main(String[ ...
- 小程序request请求 POST请求参数传递不到后台
wx.request({ url: 'https://xxx.com/xxxx.php', data: { 'jscode': code }, method: 'POST', header: { &q ...
- 洛谷——P4932 浏览器
P4932 浏览器 __stdcall给了你n个点,第i个点有权值x[i],对于两个点u和v,如果x[u] xor x[v]的结果在二进制表示下有奇数个1,那么在u和v之间连接一个Edge,现在__s ...