HDU 4565 So Easy(矩阵解公式)

So Easy

【题目链接】So Easy

【题目类型】矩阵解公式

&题解:

感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的.
942601-20170407001027707-40433901.png
942601-20170407001531738-1523405153.png
所以最后2Xn就是答案

【时间复杂度】\(O(logn)\)

&代码:

#include <cstdio>
#include <bitset>
#include <iostream>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int si=2;
int M,a,b,n;
struct mat
{
    int m[si][si];
}A;
mat Mul(mat a,mat b)
{
    mat c;
    for(int i=0;i<si;i++)
    for(int j=0;j<si;j++){
        c.m[i][j]=0;
        for(int k=0;k<si;k++){
            c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%M;
        }
    }
    return c;
}
mat bPow(mat a,ll z)
{
    mat b;
    for(int i=0;i<si;i++)for(int j=0;j<si;j++)
        b.m[i][j]=(i==j);
    while(z){
        if(z&1)
            b=Mul(b,a);
        a=Mul(a,a);
        z>>=1;
    }
    return b;
}
int tb[si];
void Init()
{
    tb[0]=a,tb[1]=1;
    A.m[0][0]=a,A.m[0][1]=b;
    A.m[1][0]=1,A.m[1][1]=a;
}
void DF(mat A)
{
    for(int i=0;i<si;i++){
        for(int j=0;j<si;j++)
            cout<<A.m[i][j]<<" ";
        cout<<endl;
    }
}
int main()
{
    freopen("E:1.txt","r",stdin);
    while(cin>>a>>b>>n>>M){
        //输入必须要取模,在这wa了1次
        a%=M,b%=M;
        Init();
        A=bPow(A,n-1);
//      DF(A);
        ll ans=0;
        for(int i=0;i<si;i++)
            ans=(ans+A.m[0][i]*tb[i])%M;
        cout<<2*ans%M<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/s1124yy/p/6676193.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值