#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#define de(x) cout<<#x<<" = "<<x<<endl
#define push_back pb
#define make_pair mp
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> V;
typedef queue<int> Q;
typedef priority_queue<int> BQ;
typedef priority_queue<int,vector<int>,greater<int> > SQ;
typedef set<int> S;
typedef map<int,int> M;
const int maxn=1e5+10,mod=2009;
inline ll add(ll a,ll b)
{
a+=b;
if (a>=mod)
a-=mod;
if (a<0)
a+=mod;
return a;
}
inline ll mul(ll a,ll b)
{
return a*b%mod;
}
struct matrix
{
static const int N=5;
int mat[N][N],n,m;
matrix(){}
matrix(int _n,int _m,int v)
{
n=_n;
m=_m;
for (int i=0;i<n;++i)
for (int j=0;j<m;++j)
mat[i][j]=i==j?v:0;
}
matrix operator * (const matrix& a) const
{
matrix res(n,a.m,0);
for (int i=0;i<n;++i)
for (int j=0;j<a.m;++j)
for (int k=0;k<m;++k)
res.mat[i][j]=add(res.mat[i][j],mul(mat[i][k],a.mat[k][j]));
return res;
}
matrix operator ^ (ll b) const
{
matrix res(n,n,1),a=*this;
while (b)
{
if (b&1)
res=res*a;
a=a*a;
b>>=1;
}
return res;
}
};
matrix A(3,3,0),C(3,1,0);
inline void init()
{
A.mat[0][0]=3;
A.mat[0][1]=2;
A.mat[0][2]=7;
A.mat[1][0]=1;
A.mat[2][1]=1;
C.mat[0][0]=5;
C.mat[1][0]=3;
C.mat[2][0]=1;
}
int main()
{
std::ios::sync_with_stdio(0);
cin.tie(0);
int i,j,k,m,n;
int T;
cin>>T;
init();
while (T--)
{
cin>>n;
matrix ans=(A^(n-2))*C;
cout<<ans.mat[0][0]<<endl;
}
return 0;
}
原文:https://www.cnblogs.com/orangee/p/9222022.html