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

题目:https://codeforces.com/contest/1182/problem/E

分析:一道矩阵快速幂的题,关键是要两边取对数,再构造矩阵。

两边取对数:\log cf[i]=\log cf[i-1]+\log cf[i-2]+\log cf[i-3]+2*i-6

令g[i]=\log cf[i],则g[i]=g[i-1]+g[i-2]+g[i-3]+2*i-6,从而可以构造出矩阵。

 

 最后再取幂还原就行了

Ac code:

#include<cstdio>
#include <iostream>
#include <cmath>

typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
ll f1,f2,f3,n,c;

void muti(ll a[][5], ll b[][5]) {
    ll tmp[5][5] = {0};
    for (int i = 0; i <5; i++)
        for (int j = 0; j <5; j++)
            for (int k = 0; k <5; k++)
                tmp[i][j] = (tmp[i][j]+ a[i][k] * b[k][j] ) % (mod-1);///由于是答案的幂,取模时需取mod的欧拉函数
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 5; j++)
            a[i][j] = tmp[i][j];
}
ll quickpow(ll a,ll b)
{
    ll ans=1,base=a;
    while(b){
        if(b&1)
            ans=(ans*base)%mod;
        base=(base*base)%mod;
        b>>=1;
    }
    return ans;
}
ll Pow(ll n)
{
    ll ans[5][5]={0};
    ans[1][1]=ans[2][2]=ans[3][3]=ans[4][4]=ans[0][0]=1;
    ll res[5][5]={0};
    res[0][0]=res[0][1]=res[0][2]=res[0][3]=res[1][0]=res[2][1]=res[3][3]=res[3][4]=res[4][4]=1;
    while(n){
        if(n&1)
            muti(ans,res);
        muti(res,res);
        n>>=1;
    }
    ll x=quickpow(f3,ans[0][0]);
    x=(x*quickpow(f2,ans[0][1]))%mod;
    x=(x*quickpow(f1,ans[0][2]))%mod;
    x=(x*quickpow(c,2*(ans[0][3]+ans[0][4])))%mod;
    return x;
}

int main() {
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>n>>f1>>f2>>f3>>c;
    ll y=Pow(n-3);
    cout<<y<<endl;
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值