[HDU1757]A Simple Math Problem(矩阵快速幂)

245 篇文章 0 订阅
21 篇文章 0 订阅

题目描述

传送门

题意
f(i)的定义看题面吧,就是那个递推式。
给定n,Mod,求f(n),答案对Mod取模。

题解

简单的矩阵加速dp题目。

A=[fn1fn2fn10]

B=a1a2a10100010001

ABn9 即为答案

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long
LL n,Mod;
struct hp{LL a[20][20];}unit,A,m,ans;

inline hp cheng(hp a,hp b)
{
    hp ans;
    memset(ans.a,0,sizeof(ans.a));
    for (int i=1;i<=10;++i)
        for (int j=1;j<=10;++j)
            for (int k=1;k<=10;++k)
                ans.a[i][j]=(ans.a[i][j]+a.a[i][k]*b.a[k][j]%Mod)%Mod;
    return ans;
}
inline hp matrix_fast_pow(hp a,LL p)
{
    hp ans=unit;
    for (;p;p>>=1,a=cheng(a,a))
        if (p&1)
            ans=cheng(ans,a);
    return ans;
}
int main()
{
    for (int i=1;i<=10;++i) unit.a[i][i]=1;
    for (int i=9;i>=0;--i) A.a[1][10-i]=(LL)i;
    for (int i=2;i<=10;++i) m.a[i-1][i]=1;
    while (~scanf("%I64d%I64d",&n,&Mod))
    {
        for (int i=1;i<=10;++i) scanf("%I64d",&m.a[i][1]);
        ans=matrix_fast_pow(m,n-9);
        ans=cheng(A,ans);
        printf("%I64d\n",ans.a[1][1]);
    }
}

总结
注意矩阵不满足交换律。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值