Live2D

CF850E Random Elections 题解

题目传送门

题目大意

没法描述,过于繁杂。

思路

果然自己是个菜鸡,只能靠读题解读题,难受极了,其实不是很难自己应该做得出来的。。。。哎。。。。

不难发现可以统计 \(A\) 获胜的情况乘上 \(3\) 就是总答案。然后 \(A\) 获胜的情况其实就是满足 \(f(S1)=f(S2)=1\) 乘上满足是 \(S1S2\) 的方案数。然后看后面那个东西,你发现对于人 \(i\) 如果 \(S1_i=1\wedge S2_i=1\) 的话有 \(2\) 中情况,就是 \(\text{BCA,CBA}\) ,如果 \(S1_i=0\wedge S2_i=1\) 的话有 \(1\) 种情况,就是 \(\text{BAC}\) ,另外两种类似。然后你就发现如果 \(S1_i=S2_i\) 贡献就是 \(2\) ,反之为 \(1\)

整理一下,发现答案其实就是:

\[\sum_{S1}\sum_{S2}2^{n-\text{pop-count}(S1\oplus S2)}f(S1)f(S2) \]

然后你就发现这个东西可以使用 \(\texttt{FWT}\) 进行优化。时间复杂度就变为了 \(\Theta(2^n n)\)

\(\texttt{Code}\)

#include <bits/stdc++.h>
using namespace std;

#define Int register int
#define mod 1000000007
#define MAXN 1048576

int n,g[MAXN],pw[25],cnt[MAXN];

void FWT (int *f,int type){
	for (Int i = 1;i < n;i <<= 1)
		for (Int j = 0;j < n;j += i << 1)
			for (Int k = 0;k < i;++ k){
				int x = f[j + k],y = f[i + j + k];
				f[j + k] = 1ll * type * (x + y) % mod,
				f[i + j + k] = 1ll * type * (x + mod - y) % mod;
			}
} 

template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}

signed main(){
	read (n);
	pw[n] = 1;for (Int i = n - 1;~i;-- i) pw[i] = pw[i + 1] * 2;n = 1 << n;
	for (Int i = 0;i < n;++ i) scanf ("%1d",&g[i]),cnt[i] = cnt[i >> 1] + (i & 1);
	FWT (g,1);for (Int i = 0;i < n;++ i) g[i] = 1ll * g[i] * g[i] % mod;FWT (g,(mod + 1) >> 1);
	int ans = 0;for (Int i = 0;i < n;++ i) ans = (ans + 1ll * g[i] * pw[cnt[i]] % mod) % mod;
	write (ans * 3ll % mod),putchar ('\n');
	return 0;
}
posted @ 2020-08-22 13:50  Dark_Romance  阅读(112)  评论(0编辑  收藏  举报