参考文章:https://blog.csdn.net/qq_34594236/article/details/53616283
举一反三:好好思考互斥的关系矩阵表现形式
计蒜客的上一道题:https://www.jisuanke.com/course/737/37733 1-m m n 1-n形式也很值得参考
以下是AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[]={0,4,5,6,1,2,3}; //对面
const int mod=1000000007;
typedef struct
{
long long d[7][7];
}node;
node mul(node p1,node p2)
{
node ans;
for(int i=1;i<=6;++i)
for(int j=1;j<=6;++j)
{
long long res=0;
for(int k=1;k<=6;++k)
res+=p1.d[i][k]*p2.d[k][j];
ans.d[i][j]=res%mod;
}
return ans;
}
node pow(node a,int n)
{
node ans;
for(int i=1;i<=6;++i)
for(int j=1;j<=6;++j)
{
if(i==j)
ans.d[i][j]=1;
else ans.d[i][j]=0;
}
while(n)
{
if(n&1)
ans=mul(ans,a);
a=mul(a,a);
n/=2;
}
return ans;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int x,y;
node G;
for(int i=1;i<=6;++i)
for(int j=1;j<=6;++j)
G.d[i][j]=4;
for(int i=0;i<m;++i)
{
scanf("%d%d",&x,&y);
G.d[a[x]][y]=0; G.d[a[y]][x]=0;
}
node res=pow(G,n-1);
long long ans=0;
for(int i=1;i<=6;++i)
for(int j=1;j<=6;++j)
{
ans+=res.d[j][i]*4;
}
printf("%lld\n",ans%mod);
return 0;
}