fzu2198(矩阵乘法模板)

参考代码:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<sstream>
#include<string>
#include<bitset>
using namespace std;

typedef long long LL;

const LL LINF = (1LL <<63);
const int INF = 1 << 31;


const int NS = 500010;
const int MS = 19;
const LL MOD = 1000000007;


struct matrix{
    #define M_SIZE (3)
    int a[M_SIZE][M_SIZE];

    void initClear()
    {
        memset(a, 0, sizeof(a));
    }

    void initOne()
    {
        memset(a, 0, sizeof(a));
        for(int i = 0; i < M_SIZE; i++)
        {
            a[i][i] = 1;
        }
    }

    void initStruct()
    {
        memset(a, 0, sizeof(a));
        a[0][0] = 6;
        a[0][1] = MOD - 1;
        a[0][2] = 5;
        a[1][0] = 1;
        a[2][2] = 1;
    }

    matrix operator * (const matrix &right)const
    {
        matrix res; res.initClear();

        for(int row = 0; row < M_SIZE; row++)
        {
            for(int col = 0; col < M_SIZE; col++)
            {
                for(int pos = 0; pos < M_SIZE; pos++)
                {
                    if(a[row][pos] > 0 && right.a[pos][col])
                    {
                        res.a[row][col] += ((LL)a[row][pos] * right.a[pos][col]) % MOD;
                        if(res.a[row][col] >= MOD)
                        {
                            res.a[row][col] -= MOD;
                        }
                    }
                }
            }
        }

        return res;
    }

    matrix operator + (const matrix &right)const
    {
        matrix res; res.initClear();

        for(int row = 0; row < M_SIZE; row++)
        {
            for(int col = 0; col < M_SIZE; col++)
            {
                res.a[row][col] = (this->a[row][col] + right.a[row][col]);
                if(res.a[row][col] >= MOD)
                {
                    res.a[row][col] -= MOD;
                }
                else if(res.a[row][col] < 0)
                {
                    res.a[row][col] += MOD;
                }
            }
        }
        return res;
    }

    matrix qpow(LL y)
    {
        matrix ans; ans.initOne();
        matrix x; x = *this;
        for(;y > 0; y >>= 1)
        {
            if(y & 1)
            {
                ans = ans * x;
            }
            x = x * x;
        }
        return ans;
    }

    void print()
    {

        for(int row = 0; row < M_SIZE; row++)
        {
            for(int col = 0; col < M_SIZE; col++)
            {
                printf("%I64d ", this->a[row][col]);
            }
            puts("");
        }
        puts("");
    }
};

matrix shine[66];

void prepare()
{
    shine[0].initStruct();
    for(int i = 1; i < 66; i++)
    {
        shine[i] = shine[i - 1] * shine[i - 1];
    }
}

LL n;

int main()
{
    prepare();
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif

    int nCase;
    scanf("%d", &nCase);
    for(int T = 1; T <= nCase; T++)
    {
        scanf("%I64d", &n);

        LL res;
        if(1 == n)
        {
            res = 6;
        }
        else if(2 == n)
        {
            res = 41;
        }
        else
        {
            matrix ans; ans.initOne();
            LL y = n - 2;
            for(int i = 0; y > 0; i++, y>>=1)
            {
                if(y & 1)
                {
                    ans = ans * shine[i];
                }
            }
            res = ((LL)ans.a[0][0] * 41) + ((LL)ans.a[0][1] * 6) + ans.a[0][2];
            res %= MOD;
            res += MOD;
            res %= MOD;
        }
//        cout<<res<<endl;
        printf("%I64d\n", res);
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值