Portal

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

  1. //Well-known Numbers
  2. #include <cstdio>
  3. #include <algorithm>
  4. using namespace std;
  5. int const N=1e5+10;
  6. long long f[N];
  7. int n,m,ans[N];
  8. int main()
  9. {
  10. int s,k; scanf("%d%d",&s,&k);
  11. int n; f[1]=1;
  12. for(n=2;f[n-1]<s;n++)
  13. for(int j=max(1,n-k);j<=n-1;j++) f[n]+=f[j];
  14. int m=0;
  15. for(int i=n-1;i>=1&&s;i--) if(f[i]<=s) ans[++m]=f[i],s-=f[i];
  16. if(m<2) ans[++m]=0;
  17. printf("%d\n",m);
  18. for(int i=1;i<=m;i++) printf("%d ",ans[i]);
  19. puts("");
  20. return 0;
  21. }

P.S.

看标签猜结论系列binary search greedy number theory。不过根本不需要binary search啊!

Codeforces225B - Well-known Numbers的更多相关文章

  1. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  2. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  3. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  4. [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 ...

  5. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

  8. [LeetCode] Consecutive Numbers 连续的数字

    Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...

  9. [LeetCode] Compare Version Numbers 版本比较

    Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...

随机推荐

  1. 基于socketserver实现的并发(tcp和udp)

    threading 线程 基于tcp协议:请求建立连接,然后开启进程 基于udp协议:直接开启新进程 基于tcp协议 import socketserver # 导入socketserver模块 # ...

  2. poj3616 Milking Time

    思路: dp. 实现: #include <iostream> #include <cstdio> #include <algorithm> using names ...

  3. requirejs&&springboot

    1.Spring Boot Spring boot 基础结构主要有三个文件夹: (1)src/main/java  程序开发以及主程序入口 (2)src/main/resources 配置文件 (3) ...

  4. jdk1.8新日期时间类(DateTime、LocalDateTime)demo代码

    //获取当前时间 LocalDateTime d0 = LocalDateTime.now(); System.out.println(DataConvertUtil.localDateTimeToS ...

  5. pandas中loc-iloc-ix的使用

    转自:https://www.jianshu.com/p/d6a9845a0a34 Pandas中loc,iloc,ix的使用 使用 iloc 从DataFrame中筛选数据 iloc 是基于“位置” ...

  6. ssm框架搭建(下) 简单案例

    前言 这段时间没有更新博客,一直想做一个基于ssm的简单的项目.经过多次的尝试,终于实现了简单的增删查改功能了. 正文 由于前端的技术不是很熟悉,经过多方的查阅,使用了bootstrap的样式,来使界 ...

  7. Angularjs 实现 $(document).ready()的两种方法

    1.在controller里面利用$on或者$watch bookControllers.controller('bookctrl_test', ['$scope', '$routeParams', ...

  8. android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误

    第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap passed to Canvas constructor ...

  9. ArrayList Vector LinkedList分析

    1.创建 ArrayList 的底层是一个数组.  ArrayList<String> list1 = new ArrayList<>(); list1.add("a ...

  10. TensorFlow低阶API(三)—— 变量

    简介 TensorFlow变量是表示程序处理的共享持久状态的最佳方法. 我们使用tf.Variable类操作变量.tf.Variable表示可通过其运行操作来改变其值的张量.与tf.Tensor对象不 ...