Live2D

Solution -「AGC 016F」Games on DAG

\(\mathcal{Description}\)

  Link.

  给定一个含 \(n\) 个点 \(m\) 条边的 DAG,有两枚初始在 1 号点和 2 号点的棋子。两人博弈,轮流移动其中一枚棋子到邻接结点位置,无法移动者负。求 \(2^m\) 个边集中,加入图中能使先手必胜的方案数。答案对 \(10^9+7\) 取模。

  \(n\le15\)

\(\mathcal{Solution}\)

  先从博弈角度思考:两枚棋子显然独立,那么先手必胜等价于 \(\operatorname{sg}(1)\not=\operatorname{sg}(2)\),其中,\(\operatorname{sg}\)

\[\operatorname{sg}(u)=\operatorname{mex}_{v\in\operatorname{adj}(u)}\operatorname{sg}(v) \]

在不同边集的情况中,\(\operatorname{adj}(u)\) 有所改变,所以情况不同。

  接下来,方便起见,尝试计数 \(\operatorname{sg}(1)=\operatorname{sg}(2)\) 的情况。令 \(f(S)~(1,2\in S)\) 表示仅考虑点集 \(S\) 的导出子图时,\(\operatorname{sg}(1)=\operatorname{sg}(2)\) 的方案数。而 DP 中的“子问题”源于 \(\operatorname{sg}\) 函数中 \(\operatorname{mex}\) 的定义——如果我们挑除所有 \(\operatorname{sg}(u)=0\)\(u\)(一定存在)及其邻接边,剩下的子图仍然是同类问题,只是所有 \(\operatorname{sg}\) 值减少 \(1\),不影响 DP 定义,这就是转移的雏形。具体地,枚举点集 \(T\subseteq S\),钦定其中所有点的 \(\operatorname{sg}\)\(0\),考虑:

  • \(1,2\in T\)\(T\) 中每个点至少连向一个 \(S-T\) 中的点,其余边任意(注意 \(T\) 中的边是子问题,不考虑)。

  • \(1,2\not\in T\),已经满足条件,在上种情况的基础上加上 \(T\) 中边任选的方案,直接贡献。

  综上,复杂度 \(\mathcal O(3^nn)\)

\(\mathcal{Code}\)

/* Clearink */

#include <cstdio>

#define rep( i, l, r ) for ( int i = l, repEnd##i = r; i <= repEnd##i; ++i )
#define per( i, r, l ) for ( int i = r, repEnd##i = l; i >= repEnd##i; --i )

const int MAXN = 15, MAXM = MAXN * ( MAXN - 1 ) >> 1, MOD = 1e9 + 7;
int n, m, pwr[MAXM + 5], conc[1 << MAXN][MAXN], f[1 << MAXN];
bool adj[MAXN + 5][MAXN + 5];

inline int mul( const long long a, const int b ) { return a * b % MOD; }
inline int sub( int a, const int b ) { return ( a -= b ) < 0 ? a + MOD : a; }
inline void subeq( int& a, const int b ) { ( a -= b ) < 0 && ( a += MOD ); }
inline int add( int a, const int b ) { return ( a += b ) < MOD ? a : a - MOD; }
inline void addeq( int& a, const int b ) { ( a += b ) >= MOD && ( a -= MOD ); }

int main() {
	scanf( "%d %d", &n, &m ), pwr[0] = 1;
	rep ( i, 1, m ) {
		pwr[i] = add( pwr[i - 1], pwr[i - 1] );
		int u, v; scanf( "%d %d", &u, &v );
		adj[u - 1][v - 1] = true;
	}

	rep ( S, 1, ( 1 << n ) - 1 ) {
		int v = 0; for ( ; !( S >> v & 1 ); ++v );
		rep ( u, 0, n - 1 ) conc[S][u] = conc[S ^ 1 << v][u] + adj[u][v];
	}

	rep ( S, 0, ( 1 << n ) - 1 ) if ( ( S & 3 ) == 3 ) {
		f[S] = 1;
		for ( int T = S & ( S - 1 ); T; T = ( T - 1 ) & S ) {
			if ( T & 1 && T & 2 ) {
				int coe = 1;
				rep ( u, 0, n - 1 ) if ( S >> u & 1 ) {
					if ( T >> u & 1 ) {
						coe = mul( coe, pwr[conc[S ^ T][u]] - 1 );
					} else {
						coe = mul( coe, pwr[conc[T][u]] );
					}
				}
				addeq( f[S], mul( coe, f[T] ) );
			} else if ( !( T & 1 || T & 2 ) ) {
				int coe = 1;
				rep ( u, 0, n - 1 ) if ( S >> u & 1 ) {
					if ( T >> u & 1 ) {
						coe = mul( coe,
							mul( pwr[conc[S ^ T][u]] - 1, pwr[conc[T][u]] ) );
					} else {
						coe = mul( coe, pwr[conc[T][u]] );
					}
				}
				addeq( f[S], coe );
			}
		}
	}

	printf( "%d\n", sub( pwr[m], f[( 1 << n ) - 1] ) );
	return 0;
}
posted @ 2021-05-04 13:23  Rainybunny  阅读(67)  评论(0编辑  收藏  举报