日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

线性代数三之状压DP的矩阵加速——Quad Tiling,Bus公交线路

發布時間:2023/12/3 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 线性代数三之状压DP的矩阵加速——Quad Tiling,Bus公交线路 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

狀壓與矩陣加速的藕斷絲連

  • Quad Tiling
    • description
    • solution
    • code
  • [Hnoi2010]Bus 公交線路
    • description
    • solution
    • code

Quad Tiling

description

solution

dpi,S:dp_{i,S}:dpi,S?: iii列的狀態為SSS的方案數,最后答案為dpn,(1<<4)?1dp_{n,(1<<4)-1}dpn,(1<<4)?1?(最后一列剛好鋪滿)

  • [0000]→[1001]/[0000]/[0011]/[1111]/[1100]\begin{bmatrix} 0\\ 0\\ 0\\ 0 \end{bmatrix} \rightarrow \begin{bmatrix} 1\\ 0\\ 0\\ 1\\ \end{bmatrix} / \begin{bmatrix} 0\\ 0\\ 0\\ 0\\ \end{bmatrix} / \begin{bmatrix} 0\\ 0\\ 1\\ 1\\ \end{bmatrix} / \begin{bmatrix} 1\\ 1\\ 1\\ 1\\ \end{bmatrix} / \begin{bmatrix} 1\\ 1\\ 0\\ 0\\ \end{bmatrix} ?????0000???????????1001??????/?????0000??????/?????0011??????/?????1111??????/?????1100??????

  • [1111]→[0000]\begin{bmatrix} 1\\ 1\\ 1\\ 1\\ \end{bmatrix} \rightarrow \begin{bmatrix} 0\\ 0\\ 0\\ 0\\ \end{bmatrix} ?????1111???????????0000??????

  • [1001]→[0110]/[0000]\begin{bmatrix} 1\\ 0\\ 0\\ 1 \end{bmatrix} \rightarrow \begin{bmatrix} 0\\ 1\\ 1\\ 0\\ \end{bmatrix} / \begin{bmatrix} 0\\ 0\\ 0\\ 0\\ \end{bmatrix} ?????1001???????????0110??????/?????0000??????

  • [0110]→[1001]\begin{bmatrix} 0\\ 1\\ 1\\ 0 \end{bmatrix} \rightarrow \begin{bmatrix} 1\\ 0\\ 0\\ 1\\ \end{bmatrix} ?????0110???????????1001??????

  • [1100]→[0011]/[0000]\begin{bmatrix} 1\\ 1\\ 0\\ 0 \end{bmatrix} \rightarrow \begin{bmatrix} 0\\ 0\\ 1\\ 1\\ \end{bmatrix} / \begin{bmatrix} 0\\ 0\\ 0\\ 0\\ \end{bmatrix} ?????1100???????????0011??????/?????0000??????

  • [0011]→[1100]/[0000]\begin{bmatrix} 0\\ 0\\ 1\\ 1\\ \end{bmatrix} \rightarrow \begin{bmatrix} 1\\ 1\\ 0\\ 0\\ \end{bmatrix} / \begin{bmatrix} 0\\ 0\\ 0\\ 0\\ \end{bmatrix} ?????0011???????????1100??????/?????0000??????

  • 1010→0101...1010\rightarrow 0101...10100101...等的其余狀態都是不可能最后剛好nnn被多米諾骨牌鋪滿而不凸出去幾格的

code

#include <cstdio> #include <cstring> #define int long long int n, mod; struct matrix {int n, m;int c[16][16];matrix() {memset( c, 0, sizeof( c ) );}matrix operator * ( matrix &t ) {matrix ans;ans.n = n, ans.m = t.m;for( int i = 0;i < n;i ++ )for( int j = 0;j < t.m;j ++ )for( int k = 0;k < m;k ++ )ans.c[i][j] = ( ans.c[i][j] + c[i][k] * t.c[k][j] ) % mod;return ans;} }g, ret; int id[2][2][2][2];matrix qkpow( matrix x, int y ) {matrix ans;ans.n = ans.m = x.m;for( int i = 0;i < ans.n;i ++ )ans.c[i][i] = 1;while( y ) {if( y & 1 ) ans = ans * x;x = x * x;y >>= 1;}return ans; }void rebuild() {memset( g.c, 0, sizeof( g.c ) );memset( ret.c, 0, sizeof( ret.c ) );g.n = g.m = ret.m = 1 << 4, ret.n = 1;g.c[id[0][0][0][0]][id[1][1][0][0]] = 1;g.c[id[0][0][0][0]][id[0][0][1][1]] = 1;g.c[id[0][0][0][0]][id[1][0][0][1]] = 1;g.c[id[0][0][0][0]][id[1][1][1][1]] = 1;g.c[id[0][0][0][0]][id[0][0][0][0]] = 1;g.c[id[0][0][1][1]][id[1][1][0][0]] = 1;g.c[id[0][0][1][1]][id[0][0][0][0]] = 1;g.c[id[1][1][0][0]][id[0][0][0][0]] = 1;g.c[id[1][1][0][0]][id[0][0][1][1]] = 1;g.c[id[1][0][0][1]][id[0][0][0][0]] = 1;g.c[id[1][0][0][1]][id[0][1][1][0]] = 1;g.c[id[0][1][1][0]][id[1][0][0][1]] = 1;g.c[id[1][1][1][1]][id[0][0][0][0]] = 1;ret.c[0][id[0][0][0][0]] = 1; }signed main() {for( int i = 0;i < 2;i ++ )for( int j = 0;j < 2;j ++ )for( int k = 0;k < 2;k ++ )for( int w = 0;w < 2;w ++ )id[i][j][k][w] = w + ( 1 << 1 ) * k + ( 1 << 2 ) * j + ( 1 << 3 ) * i;while( scanf( "%lld %lld", &n, &mod ) ) {if( ! n && ! mod ) return 0;rebuild();g = qkpow( g, n );ret = ret * g;printf( "%lld\n", ret.c[0][id[0][0][0][0]] );}return 0; }

[Hnoi2010]Bus 公交線路

description

solution

dpi,S:dp_{i,S}:dpi,S?:i?1i-1i?1個站都已經被有且訪問過一次,然后[i,i+p?1][i,i+p-1][i,i+p?1]的站的訪問狀態為SSS

SSS從高到低第jjj位代表i+j?1i+j-1i+j?1車站的訪問狀態)

dpi,S→dpi+1,S′:dp_{i,S}\rightarrow dp_{i+1,S'}:dpi,S?dpi+1,S?: 必須滿足SSS的最高位為111且車站訪問狀態翻譯過來是一樣的

也就是說原來SSS的第jjj位,變成了S′S'S中的第j?1j-1j?1

枚舉下一個被訪問的車站,要滿足該車站的狀態原來是000

最后套矩陣加速

code

#include <cstdio> #include <cstring> #define mod 30031 struct matrix {int n, m;int c[130][130];matrix() {memset( c, 0, sizeof( c ) );}matrix operator * ( matrix &t ) {matrix ans;ans.n = n, ans.m = t.m;for( int i = 1;i <= n;i ++ )for( int j = 1;j <= t.m;j ++ )for( int k = 1;k <= m;k ++ )ans.c[i][j] = ( ans.c[i][j] + c[i][k] * t.c[k][j] ) % mod;return ans;} }g, ret; int n, k, p, tot; int s[130];matrix qkpow( matrix x, int y ) {matrix ans;ans.n = ans.m = x.n;for( int i = 1;i <= ans.n;i ++ )ans.c[i][i] = 1;while( y ) {if( y & 1 ) ans = ans * x;x = x * x;y >>= 1;}return ans; }int main() {scanf( "%d %d %d", &n, &k, &p );for( int i = ( 1 << p - 1 );i < ( 1 << p );i ++ ) {int cnt = 0;for( int j = 0;j < p;j ++ )if( 1 << j & i ) cnt ++;if( cnt == k ) s[++ tot] = i;}g.n = g.m = ret.m = tot, ret.n = 1;ret.c[1][tot] = 1;for( int i = 1;i <= tot;i ++ )for( int j = 1;j <= tot;j ++ ) {int New = s[i] - ( 1 << p - 1 ) << 1;for( int w = 0;w < p;w ++ )if( ! ( 1 << w & New ) && New + ( 1 << w ) == s[j] ) {g.c[i][j] = 1;break;}}g = qkpow( g, n - k );ret = ret * g;printf( "%d\n", ret.c[1][tot] );return 0; }

總結

以上是生活随笔為你收集整理的线性代数三之状压DP的矩阵加速——Quad Tiling,Bus公交线路的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。