Matrix..0.0

Input

1 2 3
1 2 5

Output

4
7

Hint

1 2 3 4
7 12 19 30
49 80 129 208

Solution

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())

typedef long long int LLI;

const int MOD = 1000000007;
const int N = 4;
LLI l,r,t,n;
struct Matrix {
    LLI mat[N][N];
    Matrix operator*(const Matrix& m)const {
        Matrix tmp;
        for(LLI i = 0 ; i < N ; i++) {
            for(LLI j = 0 ; j < N ; j++) {
                tmp.mat[i][j] = 0;
                for(LLI k = 0 ; k < N ; k++)
                    tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*m.mat[k][j])%MOD;
                tmp.mat[i][j] %= MOD;
            }
        }
        return tmp;
    }
};
Matrix m;
void print(Matrix d)
{
    LLI i,j;
    for (i=0;i<4;++i)
    {
        for (j=0;j<4;++j)
            printf("%4lld",d.mat[i][j]);
        printf("\n");
    }
    printf("\n");
}
LLI Pow() {
    Matrix ans,tt=m;
    for (LLI i=0; i<4; ++i)        ans.mat[i][0]=l;
    for (LLI i=0; i<4; ++i)        ans.mat[i][1]=r;
    for (LLI i=0; i<4; ++i)        ans.mat[i][2]=l+r;
    for (LLI i=0; i<4; ++i)        ans.mat[i][3]=l+r+r-1;
    n--;
    while(n) {
        if (n%2) tt = tt*m;
        //print(tt);
        n /= 2;
        m = m*m;
        //print(m);
    }

    ans=ans*tt;
    //print(ans);
    return ans.mat[0][t]%MOD;
}

int main() {

    while(~scanf("%lld%lld",&l,&r)) {
        for (LLI i=0; i<4; ++i) m.mat[0][i]=0;
        m.mat[1][0]=0;
        m.mat[2][0]=m.mat[3][0]=m.mat[1][1]=m.mat[1][2]=m.mat[1][3]=m.mat[3][1]=1;
        m.mat[2][1] = m.mat[3][2]=2;
        m.mat[2][2] = 3;
        m.mat[2][3]=m.mat[3][3]=4;

        scanf("%lld", &n);
        t=(n-1)%4;
        n=(n-1)/4;
        if (!n)
        {
            if (t==3) printf("%lld\n",r+r+l-1);
            else if (t==2) printf("%lld\n",r+l);
            else if (t==1) printf("%lld\n",r);
            else printf("%lld\n",l);
        } else   printf("%lld\n", Pow());
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值