HDU 6198 2017沈阳网络赛 线形递推
2024-09-27 19:12:02
题目链接: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;
}
最新文章
- Java笔记:文件夹操作
- 安卓学习之--UI控件用法 单选 按钮 下拉框
- 【JAVA】 Java 连接池的工作原理
- JavaScript document属性和方法
- android加固系列—1.如何检验so文件是否加壳成功
- Intellij Idea 14 生成serialVersionUID的方法
- C# 调用控制台程序,并获取输出写入文件
- virtual析构函数的作用
- .net对js和css、img剥离项目进行压缩优化、cdn加速
- 使用PHP解压文件Unzip
- NYOJ 12 喷水装置(二)
- 2013集训.DAY21.A
- WebForm和Asp.Net MVC的理解
- Mathematics for Computer Graphics
- JAVAEE——SSH项目实战01:SVN介绍、安装和使用方法
- 【JavaScript流程控制语句的用法及练习】
- 分支界定( BRANCH-AND-BOUND)
- HanLP汉语言分析框架
- bat如何实现多台android设备同时安装多个apk
- P1434 [SHOI2002]滑雪 dfs