Codeforces Round #566 (Div. 2) E.Product Oriented Recurrence(矩阵快速幂)

本文介绍了一种利用矩阵快速幂和费马小定理解决特定数学问题的方法,通过构造特定矩阵并运用快速幂算法计算大数幂次方,结合费马小定理进行模运算,有效地解决了复杂序列的计算问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

思路来源

涛神+贤神

题解

先构造出g(x)的系数矩阵,计算出cnum,num[1],num[2]和num[3]

在要算g(x)的时候,偷梁换柱成f(x)计算,加上费马小定理降幂即可

题解有左右同乘c^{x},令h(x)=c^{x}*f(x),则h(x)=h(x-1)*h(x-2)*h(x-3)

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=5;
const int mod=1e9+6;
const int MOD=1e9+7; 
struct mat
{
    ll a[maxn][maxn];
    mat()
    {
        memset(a,0,sizeof a);
    }
    void clear()
    {
        memset(a,0,sizeof a);
    }
};
mat operator*(mat a,mat b)
{
    mat c;
    for(int i=0;i<=4;++i)
    {
        for(int j=0;j<=4;++j)
        {
            for(int k=0;k<=4;++k)
            {
                c.a[i][j]+=a.a[i][k]%mod*b.a[k][j]%mod;
                c.a[i][j]=(c.a[i][j]%mod+mod)%mod;
            }
        }
    }
    return c;
}
mat modpow(mat x,ll p)
{
    mat res;
    for(int i=0;i<=4;++i)
    res.a[i][i]=1;
    for(;p;x=x*x,p/=2)
    if(p&1)res=res*x; 
    return res;
}
ll modpow(ll x,ll n,ll p)
{
	ll res=1;
	for(;n;x=x*x%p,n/=2)
	if(n&1)res=res*x%p;
	return res;
} 
mat b;
ll n,a[maxn],c;
ll cnum,num[maxn];
ll res;
void init()
{
    b.a[0][0]=1;b.a[0][1]=1;b.a[0][2]=1;b.a[0][3]=2;b.a[0][4]=-4;
    b.a[1][0]=1;
    b.a[2][1]=1;
    b.a[3][3]=1;b.a[3][4]=1;
    b.a[4][4]=1;
}
int main()
{
    scanf("%I64d",&n);
    scanf("%I64d%I64d%I64d%I64d",&a[1],&a[2],&a[3],&c);
    if(n<=3)
    {
        printf("%I64d\n",a[n]);
        return 0;
    }
    init();
    mat ans;
    for(int i=0;i<=5;++i)
    ans.a[i][i]=1;
    ans=modpow(b,n-3);
    //费马小定理 系数模(1e9+6) 算值模(1e9+7) 
	cnum=(ans.a[0][3]*3+ans.a[0][4])%mod;
	num[3]=ans.a[0][0];
    num[2]=ans.a[0][1];
    num[1]=ans.a[0][2];
    res=1;
    for(int i=1;i<=3;++i)
    res=res*modpow(a[i],num[i],MOD)%MOD;
    res=res*modpow(c,cnum,MOD)%MOD;
    printf("%I64d\n",res);
    return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小衣同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值