zoukankan      html  css  js  c++  java
  • 【poj3420】 Quad Tiling

    http://poj.org/problem?id=3420 (题目链接)

    题意

      给出$n*m$的网格,用$1*2$的方块覆盖有多少种方案。

    Solution

      数据很大,不能直接搞了,我们矩乘一下。0表示已放置,1表示未放置。dfs跑出一个$16*16$的转移矩阵,然后矩乘,最后输出$ans[0][0]$就可以了。

    代码

    // poj3420
    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #define LL long long
    #define HAS 4001
    #define inf 2147483640
    #define Pi acos(-1.0)
    #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    using namespace std;
    
    int n,m;
    LL a[16][16],tmp[16][16],t[16][16],ans[16][16];
    
    void dfs(int x,int now,int pre) {
    	if (x==4) {++a[pre][now];return;}
    	dfs(x+1,now<<1|1,pre<<1);
    	dfs(x+1,now<<1,pre<<1|1);
    	if (x<3) dfs(x+2,now<<2,pre<<2);
    }
    void power() {
    	for (int i=0;i<16;i++) {
    		for (int j=0;j<16;j++) t[i][j]=a[i][j],ans[i][j]=0;
    		ans[i][i]=1;
    	}
    	while (n) {
    		if (n&1) {
    			for (int i=0;i<16;i++)
    				for (int j=0;j<16;j++) {
    					tmp[i][j]=0;
    					for (int k=0;k<16;k++) (tmp[i][j]+=ans[i][k]*t[k][j]%m)%=m;
    				}
    			for (int i=0;i<16;i++)
    				for (int j=0;j<16;j++) ans[i][j]=tmp[i][j];
    		}
    		n>>=1;
    		for (int i=0;i<16;i++)
    			for (int j=0;j<16;j++) {
    				tmp[i][j]=0;
    				for (int k=0;k<16;k++) (tmp[i][j]+=t[i][k]*t[k][j]%m)%=m;
    			}
    		for (int i=0;i<16;i++)
    			for (int j=0;j<16;j++) t[i][j]=tmp[i][j];
    	}
    }
    int main() {
    	dfs(0,0,0);
    	while (scanf("%d%d",&n,&m)!=EOF && n && m) {
    		power();
    		printf("%lld
    ",ans[0][0]);
    	}
        return 0;
    }
    
  • 相关阅读:
    Codeforces-754D Fedor and coupons
    LightOJ
    LightOJ
    LightOJ
    LightOJ
    POJ
    HDU
    HDU
    HDU-2159
    方法的重写
  • 原文地址:https://www.cnblogs.com/MashiroSky/p/6417325.html
Copyright © 2011-2022 走看看