题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6198

题意:给出一个数k,问用k个斐波那契数相加,得不到的数最小是几。

解法:先暴力打表看看有没有规律。

#include <bits/stdc++.h>
using namespace std;
int dp[2000][2000];
typedef long long LL;
int main()
{
LL c[50];
c[0]=0;
c[1]=1;
c[2]=1;
for(int i=2; i<50; i++) c[i] = c[i-1]+c[i-2];
dp[0][0]=1;
for(int i=0; i<=40; i++){
for(int j=1; j<=40; j++){
for(int k=c[i]; k<=1000; k++){
dp[j][k] = dp[j][k] + dp[j-1][k-c[i]];
}
}
}
for(int i=1; i<=40; i++)
for(int j=1; j<=1000; j++)
if(dp[i][j]==0){
printf("%d\n", j);
break;
}
}

然后发现这恰好是一个线形递推,递推式就是dp[n]=dp[n-1]*3-dp[n-2]+1。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353;
struct Matrix{
LL a[3][3];
void set1(){
memset(a, 0, sizeof(a));
}
void set2(){
set1();
for(int i=0; i<3; i++) a[i][i]=1;
}
void set3(){
a[0][0]=3,a[0][1]=-1,a[0][2]=1;
a[1][0]=1,a[1][1]=0,a[1][2]=0;
a[2][0]=0,a[2][1]=0,a[2][2]=1;
}
void set4(){
set1();
a[0][0]=12;
a[1][0]=4;
a[2][0]=1;
}
};
Matrix operator*(const Matrix &a, const Matrix &b){
Matrix res;
res.set1();
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
for(int k=0; k<3; k++){
res.a[i][j] = (res.a[i][j]+a.a[i][k]*b.a[k][j]+mod)%mod;
}
}
}
return res;
}
Matrix qsm(Matrix a, int n){
Matrix res;
res.set2();
while(n){
if(n&1) res=res*a;
a=a*a;
n/=2;
}
return res;
}
int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
if(n==1) printf("4\n");
else if(n==2) puts("12");
else{
Matrix a,b;
a.set3();
b.set4();
a = qsm(a,n-2);
a=a*b;
printf("%lld\n", (a.a[0][0]+mod)%mod);
}
}
return 0;
}

最新文章

  1. Java笔记:文件夹操作
  2. 安卓学习之--UI控件用法 单选 按钮 下拉框
  3. 【JAVA】 Java 连接池的工作原理
  4. JavaScript document属性和方法
  5. android加固系列—1.如何检验so文件是否加壳成功
  6. Intellij Idea 14 生成serialVersionUID的方法
  7. C# 调用控制台程序,并获取输出写入文件
  8. virtual析构函数的作用
  9. .net对js和css、img剥离项目进行压缩优化、cdn加速
  10. 使用PHP解压文件Unzip
  11. NYOJ 12 喷水装置(二)
  12. 2013集训.DAY21.A
  13. WebForm和Asp.Net MVC的理解
  14. Mathematics for Computer Graphics
  15. JAVAEE——SSH项目实战01:SVN介绍、安装和使用方法
  16. 【JavaScript流程控制语句的用法及练习】
  17. 分支界定( BRANCH-AND-BOUND)
  18. HanLP汉语言分析框架
  19. bat如何实现多台android设备同时安装多个apk
  20. P1434 [SHOI2002]滑雪 dfs

热门文章

  1. 洛谷 Roy&amp;October之取石子
  2. Race to 1 UVA - 11762 (记忆dp概率)
  3. 解决:LNMP架构下访问php页面出现500错误
  4. bzoj3251: 树上三角形(思维题)
  5. HDU--2722
  6. idea plugin 插件开发之检测文件修改
  7. centos7 配置 yum 安装的 jdk
  8. 安装黑苹果的config.plist
  9. 第3章-Vue.js 指令扩展 和 todoList练习
  10. python标准数据类型 Bytes