题目:http://acm.hdu.edu.cn/showproblem.php?pid=5950
题意:
给出公式f(n)=f(n-1)+f(n-2)*2+n^4
给出n f[1] f[2] 求f[n]
分析:
快速幂
把公式分解一下,求一下矩阵,矩阵快速幂搞一下就好
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const ll mod=2147493647;
typedef struct mat {
ll m[7][7];
} mat;
mat mul(mat a,mat b) {
mat c;
memset(c.m,0,sizeof(c.m));
for(int k=0; k<7; k++)
for(int i=0; i<7; i++)
for(int j=0; j<7; j++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j]%mod)%mod;
return c;
}
mat qmod(mat a,ll k) {
mat c;
for(int i=0; i<7; i++)
for(int j=0; j<7; j++)c.m[i][j]=(i==j);
for(; k; k>>=1) {
if(k&1)c=mul(c,a);
a=mul(a,a);
}
return c;
}
mat p = {1, 1, 0, 0, 0, 0, 0,
2, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0,
4, 0, 4, 1, 0, 0, 0,
6, 0, 6, 3, 1, 0, 0,
4, 0, 4, 3, 2, 1, 0,
1, 0, 1, 1, 1, 1, 1
};
int main() {
//freopen("f.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--) {
ll n, a, b;
scanf("%lld%lld%lld",&n,&a,&b);
if(n==1) {
printf("%lld\n",a);
continue;
}
if(n==2) {
printf("%lld\n",b);
continue;
}
mat c=qmod(p,n-2);
ll ans = b*c.m[0][0]%mod+ a*c.m[1][0]%mod;
ans+=16*c.m[2][0]+8*c.m[3][0]+4*c.m[4][0]+2*c.m[5][0]+c.m[6][0];
printf("%lld\n",ans%mod);
}
return 0;
}
本文介绍了一种使用矩阵快速幂解决特定递推问题的方法,针对题目中的递推公式 f(n) = f(n-1) + f(n-2)*2 + n^4,通过构建并求解特定矩阵来高效计算 f(n) 的值。
513

被折叠的 条评论
为什么被折叠?



