hdu 5950(化解公式+矩阵快速幂)

来源:互联网 发布:建筑设计优化职责 编辑:程序博客网 时间:2024/06/08 19:25

题目: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;}
0 0
原创粉丝点击