poj 3420 Quad Tiling 狀壓dp,pojtiling

來源:互聯網
上載者:User

poj 3420 Quad Tiling 狀壓dp,pojtiling

題意:

給4*n(n<10^9)的大矩形,問有多少種用1*2的小矩形填滿的方案。

分析:

又是鋪瓷磚,不過這次n太大,不能再一個一個格的dp了。可以先算出相鄰兩行的狀態轉移,再用矩陣來加速n行的狀態轉移。

代碼:

//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);dfs(l+1,now<<1,(pre<<1)|1);dfs(l+2,(now<<2)|3,(pre<<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;  }  


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.