题目链接:Filling
轮廓线dp出整个的,一半的,1/4的,然后用burnside引理去重。
#include <bits/stdc++.h>
using namespace std ;
typedef long long LL ;
typedef pair < int , int > pii ;
#define clr( a , x ) memset ( a , x , sizeof a )
const int mod = 1e9 + 7 ;
const LL v2 = 500000004 ;
const LL v4 = 250000002 ;
int dp[2][1 << 21] , n ;
int ans[22] = { 0 , 1 , 2 , 2 , 12 , 84 , 1641 , 50788 , 3183091 , 338785462 , 429880385 , 948920428 , 392930107 , 842384602 , 36468923 , 534250025 , 693507546 , 311288183 , 320259213 , 232751393 , 866290697 };
;
void up ( int& x , int y ) {
x += y ;
if ( x >= mod ) x -= mod ;
}
void show ( int s ) {
printf ( "s = " ) ;
for ( int i = 0 ; i < n + 1 ; ++ i ) {
printf ( "%d" , s >> i & 1 ) ;
}
puts ( "" ) ;
}
void solve () {
if ( n == 1 ) {
printf ( "%d\n" , 1 ) ;
return ;
}
int cur = 0 ;
int tot = 1 << n + 1 , x = 1 << n , y = ( 3 << n - 1 ) + 1 ;
clr ( dp , 0 ) ;
dp[cur][0] = 1 ;
for ( int i = 1 ; i < n ; ++ i ) {
for ( int j = 0 ; j < n ; ++ j ) {
cur ^= 1 ;
clr ( dp[cur] , 0 ) ;
for ( int s = 0 ; s < tot ; ++ s ) if ( dp[cur ^ 1][s] ) {
if ( j && !( s & 3 ) && !( s & x ) ) up ( dp[cur][s >> 1 | y] , dp[cur ^ 1][s] ) ;
up ( dp[cur][s >> 1] , dp[cur ^ 1][s] ) ;
}
}
}
int ans0 = 0 , ans180 = 0 , ans90 = 0 , ans90_2 = 0 ;
for ( int s = 0 ; s < tot ; ++ s ) {
up ( ans0 , dp[cur][s] ) ;
}
clr ( dp , 0 ) ;
dp[cur][0] = 1 ;
for ( int i = 1 ; i < n / 2 ; ++ i ) {
for ( int j = 0 ; j < n ; ++ j ) {
cur ^= 1 ;
clr ( dp[cur] , 0 ) ;
for ( int s = 0 ; s < tot ; ++ s ) if ( dp[cur ^ 1][s] ) {
if ( j && !( s & 3 ) && !( s & x ) ) up ( dp[cur][s >> 1 | y] , dp[cur ^ 1][s] ) ;
up ( dp[cur][s >> 1] , dp[cur ^ 1][s] ) ;
}
}
}
if ( n & 1 ) {
for ( int j = 0 ; j < n ; ++ j ) {
cur ^= 1 ;
clr ( dp[cur] , 0 ) ;
for ( int s = 0 ; s < tot ; ++ s ) if ( dp[cur ^ 1][s] ) {
if ( j && !( s & 3 ) && !( s & x ) ) up ( dp[cur][s >> 1 | y] , dp[cur ^ 1][s] ) ;
up ( dp[cur][s >> 1] , dp[cur ^ 1][s] ) ;
}
}
} else {
for ( int j = 0 ; j < n / 2 ; ++ j ) {
cur ^= 1 ;
clr ( dp[cur] , 0 ) ;
for ( int s = 0 ; s < tot ; ++ s ) if ( dp[cur ^ 1][s] ) {
if ( j && !( s & 3 ) && !( s & x ) ) up ( dp[cur][s >> 1 | y] , dp[cur ^ 1][s] ) ;
up ( dp[cur][s >> 1] , dp[cur ^ 1][s] ) ;
}
}
}
for ( int s = 0 ; s < tot ; ++ s ) {
int ok = 1 ;
for ( int j = 1 ; j <= ( n + 1 ) / 2 ; ++ j ) {
if ( ( s & ( 1 << j ) ) && ( s & ( 1 << n + 1 - j ) ) ) {
ok = 0 ;
break ;
}
}
if ( ok ) {
up ( ans180 , dp[cur][s] ) ;
if ( n % 2 == 0 && !( s & ( 1 << n ) ) && !( s & 3 ) ) {
up ( ans180 , dp[cur][s] ) ;
}
//if ( dp[cur][s] ) show ( s ) ;
}
}
int m = ( n + 1 ) / 2 ;
tot = 1 << m + 1 ;
x = 1 << m ;
y = 3 << ( m - 1 ) | 1 ;
for ( int o = 0 ; o < 1 << m ; ++ o ) {
if ( ( n & 1 ) && ( o & ( 1 << ( n / 2 ) ) ) ) continue ;
for ( int s = 0 ; s < tot ; ++ s ) {
dp[cur][s] = 0 ;
}
dp[cur][0] = 1 ;
for ( int i = 1 ; i < n / 2 + 1 ; ++ i ) {
for ( int j = 0 ; j < m ; ++ j ) {
cur ^= 1 ;
for ( int s = 0 ; s < tot ; ++ s ) {
dp[cur][s] = 0 ;
}
for ( int s = 0 ; s < tot ; ++ s ) if ( dp[cur ^ 1][s] ) {
if ( j && !( s & 3 ) && !( s & x ) ) up ( dp[cur][s >> 1 | y] , dp[cur ^ 1][s] ) ;
up ( dp[cur][s >> 1] , dp[cur ^ 1][s] ) ;
}
}
for ( int s = 0 ; s < tot ; ++ s ) {
if ( ( s & 1 ) != ( ( o >> i - 1 ) & 1 ) ) dp[cur][s] = 0 ;
}
}
for ( int s = 0 ; s < tot ; ++ s ) {
int ok = 1 ;
for ( int k = 0 ; k < m ; ++ k ) {
if ( ( o >> k ) & ( s >> k + 1 ) ) {
ok = 0 ;
break ;
}
}
if ( ok ) {
up ( ans90 , dp[cur][s] ) ;
if ( n % 2 == 0 && !( s & ( 1 << m ) ) && !( s & 1 ) ) {
up ( ans90 , dp[cur][s] ) ;
}
}
}
}
ans[n] = ( ( ans0 - ans180 + mod ) % mod * v4 + ( ans180 - ans90 + mod ) * v2 + ans90 ) % mod ;
printf ( " , %d" , ans[n] ) ;
}
int main () {
/*
printf ( "{ 1" ) ;
for ( n = 2 ; n <= 20 ; ++ n ) {
solve () ;
}
printf ( " };\n" ) ;
*/
int T ;
scanf ( "%d" , &T ) ;
for ( int i = 1 ; i <= T ; ++ i ) {
scanf ( "%d" , &n ) ;
printf ( "Case #%d: %d\n" , i , ans[n] ) ;
}
return 0 ;
}