最近事情好多,数据库作业,没天要学2个小时java,所以更新的sgu就比较少了
SGU 131
题意:给你两种小块一种,1*1,一种2*2-1*1,问你填满一个m*n的矩形有多少钟方法,n和m小于等于9,
收获:状态压缩,每一行都最多由上一行转移过来,因为上一行,那么最多有7情况,详情看代码
#include<bits/stdc++.h> #define de(x) cout<<#x<<"="<<x<<endl; #define dd(x) cout<<#x<<"="<<x<<" "; #define rep(i,a,b) for(int i=a;i<(b);++i) #define repd(i,a,b) for(int i=a;i>=(b);--i) #define repp(i,a,b,t) for(int i=a;i<(b);i+=t) #define ll long long #define mt(a,b) memset(a,b,sizeof(a)) #define fi first #define se second #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define pii pair<int,int> #define pdd pair<double,double> #define pdi pair<double,int> #define mp(u,v) make_pair(u,v) #define sz(a) (int)a.size() #define ull unsigned long long #define ll long long #define pb push_back #define PI acos(-1.0) #define qc std::ios::sync_with_stdio(false) #define db double #define all(a) a.begin(),a.end() const int mod = 1e9+7; const int maxn = 11; const double eps = 1e-6; using namespace std; bool eq(const db &a, const db &b) { return fabs(a - b) < eps; } bool ls(const db &a, const db &b) { return a + eps < b; } bool le(const db &a, const db &b) { return eq(a, b) || ls(a, b); } ll gcd(ll a,ll b) { return a==0?b:gcd(b%a,a); }; ll lcm(ll a,ll b) { return a/gcd(a,b)*b; } ll kpow(ll a,ll b) {ll res=1;a%=mod; if(b<0) return 1; for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll read(){ ll x=0,f=1;char ch=getchar(); while (ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while (ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } //inv[1]=1; //for(int i=2;i<=n;i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod; int n,m; ll dp[maxn][1<<maxn]; void dfs(int i,int b1,int b2,int s1,int s2,int k){ //b1,b2分别代表上行的前一列对这列的影响,和这行前一列对这列的影响 //s1表示这行达到s2状态上一行需要满足的状态 if(k==m){ if(!b1&&!b2) dp[i][s2]+=dp[i-1][s1]; return; } if(!b1&&!b2){ dfs(i,0,0,s1<<1,s2<<1|1,k+1); dfs(i,0,1,s1<<1,s2<<1|1,k+1); dfs(i,1,0,s1<<1,s2<<1|1,k+1); } if(!b1) dfs(i,1,1,s1<<1,s2<<1|b2,k+1); if(!b2){ dfs(i,0,1,s1<<1|(1-b1),s2<<1|1,k+1); dfs(i,1,1,s1<<1|(1-b1),s2<<1|1,k+1); } dfs(i,0,0,s1<<1|(1-b1),s2<<1|b2,k+1); } int main(){ scanf("%d%d",&n,&m); dp[0][(1<<m)-1] = 1; rep(i,1,n+1) dfs(i,0,0,0,0,0); printf("%lld\n",dp[n][(1<<m)-1]); return 0; }
原文地址:https://www.cnblogs.com/chinacwj/p/9042784.html
时间: 2024-11-05 22:56:01