hdu1588 Gauss Fibonacci

其实就是一个矩阵加上等比数列求和的问题,在因为b等于0的问题上卡住了,最后看了人家的解法重新构造了乘数矩阵

code:

#include <ctime>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#define LL long long
using namespace std;


LL k,b,n,M;

struct Matrix
{
    LL mat[3][3];
}A,E;

Matrix operator + (const Matrix a,const Matrix b)
{
    Matrix res;
    int i,j;
    for(i=1;i<=2;i++)
    {
        for(j=1;j<=2;j++)
        {
            res.mat[i][j] = (a.mat[i][j] + b.mat[i][j])%M;
        }
    }
    return res;
}

Matrix operator * (const Matrix a,const Matrix b)
{
    Matrix res;
    int i,j,k;
    for(i=1;i<=2;i++)
    {
        for(j=1;j<=2;j++)
        {
            res.mat[i][j] = 0;
            for(k=1;k<=2;k++)
            {
                res.mat[i][j] = (res.mat[i][j] + a.mat[i][k]*b.mat[k][j]%M )%M;
            }
        }
    }
    return res;
}

Matrix operator ^ (const Matrix a,LL exp)
{
    Matrix tmp=a;
    Matrix res=E;
    while(exp)
    {
        if(exp & 1)
            res = res *tmp;
        exp >>= 1;
        tmp = tmp * tmp;
    }
    return res;
}

Matrix GetSum(Matrix a,LL k)
{
    if(k==1)
        return a;
    if(k&1)
        return GetSum(a,k-1) + (a^k);
    return GetSum(a,k>>1) * ( (a^(k>>1)) + E) ;
}

void init()
{
    memset(A.mat,0,sizeof(A.mat));
    memset(E.mat,0,sizeof(E.mat));
    A.mat[1][2]=A.mat[2][2]=A.mat[2][1]=1;
    E.mat[1][1]=E.mat[2][2]=1;
}

int main()
{
    init();
    Matrix t,ans;
    while(~scanf("%lld%lld%lld%lld",&k,&b,&n,&M))
    {
        t = A ^ k;
        ans = GetSum(t,n-1) + E ;
        ans = (A ^ b) * ans ;
        printf("%lld\n",ans.mat[2][1]);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值