【GDOI2018模擬7.12】B 矩陣乘法 dp

Description

Input

1

Output

229

``````#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define fo(i,a,b) for(int i=a;i<=b;i  )
#define fd(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int N=1e5 5;
const int mo=1e9 7;
typedef long long ll;
ll n;
int ans,f[20][20];
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
bool vis[10];
int t[10];
struct matrix
{
int a[10][10];
inline void init(int n)
{
memset(a,0,sizeof(a));
fo(i,1,n)a[i][i]=1;
}
inline void clear()
{
memset(a,0,sizeof(a));
}
}a;
inline int trans(int x,int y)
{
return (x-1)*3 y;
}
inline void mul(matrix &c,matrix a,matrix b)
{
c.clear();
fo(i,1,9)
fo(j,1,9)
fo(k,1,9)
{
c.a[i][j]=(c.a[i][j] 1ll*a.a[i][k]*b.a[k][j]%mo)%mo;
}
}
inline matrix pow(matrix x,ll b)
{
matrix ans;
ans.init(9);
while (b)
{
if (b&1)mul(ans,ans,x);
mul(x,x,x);
b>>=1;
}
return ans;
}
inline int calc()
{
int ans=1;
fo(i,1,9)ans=1ll*ans*f[i][t[i]]%mo;
return ans;
}
inline void dfs(int x)
{
if (x>9)
{
ans=(ans calc())%mo;
return;
}
fo(i,1,9)
if (!vis[i])t[x]=i,vis[i]=1,dfs(x 1),vis[i]=0;
}
int main()
{
scanf("%lld",&n);
fo(i,1,3)
fo(j,1,3)
{
a.a[trans(i,j)][trans(i,j)]=1;
fo(k,0,3)
{
int x=i dx[k];
int y=j dy[k];
if (x<1||x>3||y<1||y>3)continue;
a.a[trans(x,y)][trans(i,j)]=1;
}
}
a=pow(a,n);
fo(i,1,9)
fo(j,1,9)
f[i][j]=a.a[i][j];
dfs(1);
printf("%d\n",ans);
return 0;
}``````