状压与矩阵加速的藕断丝连
- 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...1010→0101...等的其余状态都是不可能最后刚好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;
}