CSP201409T5拼图

题意:给出一个 n × m n×m n×m的方格图,现在要用如下L型的占3个的积木拼到这个图中,总共有多少种拼法使图满。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
long long n,m,k=1,Now;
int Mod=1000000007;
struct Matrix
{
	long long a[129][129];
	Matrix(){for(long long i=0;i<k;i++)a[i][i]=1;}
	Matrix(int x){memset(a,0,sizeof(a));}
	Matrix operator*(Matrix& B)//矩阵乘法 
	{
		Matrix t(0);
		for(int i=0;i<k;i++)
		for(int j=0;j<k;j++)
		for(int l=0;l<k;l++)
		t.a[i][j]=(t.a[i][j]+a[i][l]*B.a[l][j])%Mod;
		return t;
	}
}; 	
Matrix quickmul(Matrix ans,long long p)//矩阵快速幂 
{
	Matrix t;	
	while(p)
	{
		if(p&1)t=t*ans;
		ans=ans*ans;
		p>>=1;
	}
	return t;
}
Matrix A(0);
void dfs(int now,int next)
{
	//cout<<now<<" "<<next<<endl;
	if(now+1==k)
	{
		A.a[Now][next]++;
		return ;
	}
	for(int l=0;l<m;l++)
	{
		if((now>>l)&1)continue;//当前列的位置上有拼图则跳过
		int temp=1<<l;
		int now1,next1;//递归,四种拼图 
		now1=temp|(temp<<1);// 1 1
		next1=temp;         // 0 1
		if((l+1<m)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		
		now1=temp|(temp<<1);// 1 1 
		next1=temp<<1;      // 1 0
		if((l+1<m)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		
		now1=temp;		    // 0 1 
		next1=temp|temp<<1; // 1 1
		if((l+1<m)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		
		now1=temp;	        // 1 0 
		next1=temp|temp>>1; // 1 1
		if((l)&&!(now&now1||next&next1))dfs(now1|now,next1|next);
		return ;
	}
}
int main()
{
	cin>>n>>m;
	for(long long i=0;i<m;i++)k<<=1;
	for(Now=0;Now<k;Now++)dfs(Now,0);
	Matrix ANS=quickmul(A,n);	
	/*for(int i=0;i<k;i++)
	{
		for(int j=0;j<k;j++)cout<<ANS.a[i][j]<<" ";
		cout<<endl;	
	}*/
	cout<<ANS.a[0][0]<<endl;
	return 0;
}
相关推荐
iuu_star20 分钟前
C语言数据结构-顺序查找、折半查找
c语言·数据结构·算法
Yzzz-F27 分钟前
P1558 色板游戏 [线段树 + 二进制状态压缩 + 懒标记区间重置]
算法
漫随流水34 分钟前
leetcode算法(515.在每个树行中找最大值)
数据结构·算法·leetcode·二叉树
mit6.8241 小时前
dfs|前后缀分解
算法
扫地的小何尚2 小时前
NVIDIA RTX PC开源AI工具升级:加速LLM和扩散模型的性能革命
人工智能·python·算法·开源·nvidia·1024程序员节
千金裘换酒3 小时前
LeetCode反转链表
算法·leetcode·链表
byzh_rc3 小时前
[认知计算] 专栏总结
线性代数·算法·matlab·信号处理
qq_433554544 小时前
C++ manacher(求解回文串问题)
开发语言·c++·算法
歌_顿4 小时前
知识蒸馏学习总结
人工智能·算法
闲看云起4 小时前
LeetCode-day6:接雨水
算法·leetcode·职场和发展