POJ 3420 Quad Tiling-like pressure DP

Source: Internet
Author: User
Tags mul

Test instructions

Give 4*n (n<10^9) a large rectangle and ask how many schemes are filled with small rectangles of 1*2.

Analysis:

Tile again, but this time n is too large to be a one-grid DP. We can calculate the state transfer of two adjacent lines first, and then use the matrix to accelerate the state transfer of n rows.

Code:

POJ 3420//sep9#include <iostream>using namespace Std;const int maxn=16;struct matrix{__int64 M[maxN][maxN];} Mat;int n,mod;void dfs (int l,int now,int pre) {if (l>4) return;if (l==4) {++mat.m[pre][now];return;} DFS (l+1, (now<<1) |1,pre<<1);d FS (l+1,now<<1, (pre<<1) | |);d FS (l+2, (now<<2) |3, (pre &LT;&LT;2) |3);}      Matrix Mul (Matrix A,matrix b) {matrix C;      memset (c.m,0,sizeof (C.M)); for (int i=0;i<16;++i) for (int j=0;j<16;++j) for (int k=0;k<16;++k) {C                  . M[i][j]+=a.m[i][k]*b.m[k][j]; C.m[i][j]%=mod;} return C;      }matrix Expo (MATRIX a,int k) {if (k==1) return A;      MATRIX e;      memset (e.m,0,sizeof (E.M));      for (int i=0;i<16;++i) {e.m[i][i]=1;}      if (k==0) return e;          while (k) {if (k&1) E=mul (a,e);          A=mul (A,a);      k>>=1;  } return E;      } int main () {memset (mat.m,0,sizeof (MAT.M));      DFS (0,0,0); while (~SCANF ("%d%d", &n,&mod)) {if (!n&&!mod) break;          if (mod==1) {printf ("0\n"); continue;}          MATRIX Ans=expo (mat,n);          printf ("%i64d\n", ans.m[15][15]);  } return 0;   }


POJ 3420 Quad Tiling-like pressure DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.