http://acm.hdu.edu.cn/showproblem.php?pid=6470
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define INF 0x3f3f3f3f;
#define fi first
#define se second
#define MP make_pair
#define PI pair<int,int>
#define lson l,m,rt<<1,ls,rs
#define rson m+1,r,rt<<1|1,ls,rs
#define test printf("here!!!")
using namespace std;
const int mx=6;
const int mod=123456789;
struct Mat
{
ll a[mx][mx];
};
struct Node
{
ll a[mx][1];
};
Node Mul(Mat &x,Node &y)
{
Node t;
for (int i=0;i<mx;++i) t.a[i][0]=0;
for (int i=0;i<mx;++i)
for (int j=0;j<mx;++j)
t.a[i][0]=(t.a[i][0]+x.a[i][j]*y.a[j][0]%mod)%mod;
return t;
}
Mat Mul2(Mat &x,Mat &y)//这里只写这一个,不写上面那个也够了。。
{
Mat t;
for (int i=0;i<mx;++i)
for (int j=0;j<mx;++j)
t.a[i][j]=0;
for (int i=0;i<mx;++i)
for (int j=0;j<mx;++j)
for (int k=0;k<mx;++k)
t.a[i][j]=(t.a[i][j]+x.a[i][k]*y.a[k][j]%mod)%mod;
return t;
}
Node Mpow(Mat x,ll b)
{
Node ans={2,1,27,9,3,1};
while (b)
{
if (b&1) ans=Mul(x,ans);
x=Mul2(x,x);
b>>=1;
}
return ans;
}
int main()
{
ll n;
Mat x=
{
1,2,1,0,0,0,
1,0,0,0,0,0,
0,0,1,3,3,1,
0,0,0,1,2,1,
0,0,0,0,1,1,
0,0,0,0,0,1
};
int cas;
scanf("%d",&cas);
while (cas--)
{
scanf("%lld",&n);
if (n==1) printf("1\n");
else printf("%lld\n",Mpow(x,n-2).a[0][0]);
}
}
此题主要难点在怎么算出来。。。不难发现,可以通过
转换到
,如下
然后就可以做啦。。
矩阵的系数如上面所示
答案矩阵ans所记录的东西为,系数矩阵如代码中的Mat x所示