http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1010&cid=843

题解:矩阵快速幂

参考文章:https://blog.csdn.net/weixin_43272781/article/details/85939539

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=6;
const int M=100000+10;
const int MOD=123456789;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
ll t,n,m,k,q;
ll ans,cnt,flag,temp,sum;
char str;
struct node{};
ll tmp[N][N];
void multi(ll a[][N],ll b[][N],int n)
{
    memset(tmp,0,sizeof (tmp));
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            for(int k=0;k<n;k++)
                tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j])%MOD;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            a[i][j]=tmp[i][j];
}
ll res[N][N];
void Pow(ll a[][N],ll b)
{
    memset(res,0,sizeof res);//n是幂,N是矩阵大小
    for(int i=0;i<N;i++)
        res[i][i]=1;
    while(b)
    {
        if(b&1)
            multi(res,a,N);//res=res*a;复制直接在multi里面实现了;
        multi(a,a,N);//a=a*a
        b>>=1;
    }
    for(int i=0;i<N;i++)
        for(int j=0;j<N;j++)
            a[i][j]=res[i][j];
}

int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%lld",&t);
    while(t--){
        scanf("%lld",&n);
        ll a[N][N]={{1,2,1,3,3,1},//f[n]
                    {1,0,0,0,0,0},//f[n-1]
                    {0,0,1,3,3,1},//n3
                    {0,0,0,1,2,1},//n2
                    {0,0,0,0,1,1},//n1
                    {0,0,0,0,0,1}};//n0
        ll b[N][N]={{2,0,0,0,0,0},
                    {1,0,0,0,0,0},
                    {8,0,0,0,0,0},
                    {4,0,0,0,0,0},
                    {2,0,0,0,0,0},
                    {1,0,0,0,0,0}};
        Pow(a,n-2);
//        for(int i=0;i<6;i++){
//            for(int j=0;j<6;j++){
//                printf("%lld%c",a[i][j]," \n"[j==5]);
//            }
//        }
        multi(a,b,N);
        cout<<a[0][0]<<endl;
//        for(int i=0;i<6;i++){
//            for(int j=0;j<6;j++){
//                printf("%lld%c",a[i][j]," \n"[j==5]);
//            }
//        }
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}