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;
}
相关推荐
酱酱哥玩AI1 分钟前
Trae编译器:实现多目标班翠鸟优化算法(IPKO)无人机路径规划仿真(Python版),完整代码
算法
MPCTHU14 分钟前
二叉树、排序算法与结构图
数据结构·算法·排序算法
亓才孓20 分钟前
[leetcode]树的操作
算法·leetcode·职场和发展
王禄DUT30 分钟前
化学方程式配平 第33次CCF-CSP计算机软件能力认证
开发语言·c++·算法
wuqingshun31415933 分钟前
蓝桥杯 XYZ
数据结构·c++·算法·职场和发展·蓝桥杯
DreamByte1 小时前
C++菜鸟教程 - 从入门到精通 第五节
开发语言·c++·算法
南玖yy1 小时前
数据结构C语言练习(两个队列实现栈)
c语言·数据结构·算法
明朝百晓生1 小时前
【强化学习】【1】【PyTorch】【强化学习简介优化框架】
算法
loser~曹1 小时前
基于快速排序解决 leetcode hot215 查找数组中第k大的数字
数据结构·算法·leetcode
Dream it possible!1 小时前
LeetCode 热题 100_打家劫舍(83_198_中等_C++)(动态规划)
c++·算法·leetcode·动态规划