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;
}
相关推荐
To_OC28 分钟前
LC 79 单词搜索:都说这是回溯入门题,我却连错三回
javascript·算法·leetcode
问商十三载1 小时前
2026大模型GEO内链优化:3个传导逻辑提权重,零成本提29%收录优先级附布局表
人工智能·算法
Risk Actuary2 小时前
手动示例解释机器学习中 GBDT 算法原理
人工智能·算法·机器学习
Lucis__3 小时前
基于Cache替换算法的LRU缓存实现
数据结构·c++·算法·缓存·lru
血色橄榄枝9 小时前
基于用户注册信息的关键词检测挑战赛「Datawhale AI 夏令营」
人工智能·算法·机器学习
c2385610 小时前
第二篇:《测试指挥官:可视化单题自测框架(含 assert 实操)》
java·数据库·c++·算法·安全性测试
六点_dn10 小时前
Linux学习笔记-printf命令
linux·运维·算法
遥感知识服务11 小时前
Sentinel-1 + DEM + FwDET + 随机森林:从快速水深初估到多因子误差修正
算法·随机森林·sentinel
来一碗刘肉面11 小时前
顺序表与链表的比较
数据结构·算法·链表
alphaTao12 小时前
LeetCode 每日一题 2026/7/13-2026/7/19
算法·leetcode