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 ...
随机推荐
- 基于socketserver实现的并发(tcp和udp)
threading 线程 基于tcp协议:请求建立连接,然后开启进程 基于udp协议:直接开启新进程 基于tcp协议 import socketserver # 导入socketserver模块 # ...
- poj3616 Milking Time
思路: dp. 实现: #include <iostream> #include <cstdio> #include <algorithm> using names ...
- requirejs&&springboot
1.Spring Boot Spring boot 基础结构主要有三个文件夹: (1)src/main/java 程序开发以及主程序入口 (2)src/main/resources 配置文件 (3) ...
- jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码
//获取当前时间 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToS ...
- pandas中loc-iloc-ix的使用
转自:https://www.jianshu.com/p/d6a9845a0a34 Pandas中loc,iloc,ix的使用 使用 iloc 从DataFrame中筛选数据 iloc 是基于“位置” ...
- ssm框架搭建(下) 简单案例
前言 这段时间没有更新博客,一直想做一个基于ssm的简单的项目.经过多次的尝试,终于实现了简单的增删查改功能了. 正文 由于前端的技术不是很熟悉,经过多方的查阅,使用了bootstrap的样式,来使界 ...
- Angularjs 实现 $(document).ready()的两种方法
1.在controller里面利用$on或者$watch bookControllers.controller('bookctrl_test', ['$scope', '$routeParams', ...
- android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误
第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap passed to Canvas constructor ...
- ArrayList Vector LinkedList分析
1.创建 ArrayList 的底层是一个数组. ArrayList<String> list1 = new ArrayList<>(); list1.add("a ...
- TensorFlow低阶API(三)—— 变量
简介 TensorFlow变量是表示程序处理的共享持久状态的最佳方法. 我们使用tf.Variable类操作变量.tf.Variable表示可通过其运行操作来改变其值的张量.与tf.Tensor对象不 ...