51nod1122 机器人走方格 V4

矩阵快速幂求出每个点走n步后到某个点的方案数。然后暴力枚举即可

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
int read(){
	int x=0;char c=getchar();
	while(!isdigit(c)) c=getchar();
	while(isdigit(c)) x=x*10+c-'0',c=getchar();
	return x;
} 
const int nmax=5;
const int mod=1e9+7;
struct node{
	ll a[nmax][nmax];
	node(){
		clr(a,0);
	}
	node operator*(const node&o)const{
	    node ans;
	    rep(i,1,4) rep(j,1,4) {
	    	rep(k,1,4) ans.a[i][j]+=a[i][k]*o.a[k][j]%mod;
	    	ans.a[i][j]%=mod;
	    }
	    return ans;
	}
};
node a,b;
int main(){
	int n=read();
	rep(i,1,4) rep(j,1,4) if(i!=j) b.a[i][j]=1;
	rep(i,1,4) a.a[i][i]=1;
	while(n){
		if(n&1) a=a*b;
		b=b*b;n>>=1;
	}
	ll ans=0;
	rep(i,1,4) rep(j,1,4) if(i!=j) rep(k,1,4) if(k!=j&&k!=i) rep(t,1,4) if(t!=i&&t!=j&&t!=k)
	  ans=(ans+a.a[1][i]*a.a[2][j]%mod*a.a[3][k]%mod*a.a[4][t]%mod)%mod;
	printf("%lld
",ans);
	return 0;
}

  

基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
 收藏
 关注
四个机器人a b c d,在2 * 2的方格里,一开始四个机器人分别站在4个格子上,每一步机器人可以往临近的一个格子移动或留在原地(同一个格子可以有多个机器人停留),经过n步后有多少种不同的走法,使得每个毯子上都有1机器人停留。由于方法数量巨大,输出 Mod 10^9 + 7的结果。
 
Input
输入1个数N(0 <= N <= 10^9)
Output
输出走法的数量 Mod 10^9 + 7
Input示例
1
Output示例
9
原文地址:https://www.cnblogs.com/fighting-to-the-end/p/5866348.html