Fluorescent
(标题为传送门)
Time Limit : 3000MS Memory Limit : 500MB
Problem Description
Matt, a famous adventurer who once defeated a pack of dire wolves alone, found a lost court. Matt finds that there are N fluorescent lights which seem to be the stars from the firmament. What’s more, there are M switches that control these fluorescent lights. Each switch is connected to a group of lights. When Matt touches a switch, all the lights connected to it will change their states (turning the dark on, turning the bright off).
Initially, all the fluorescent lights are dark. For each switch, Matt will touch it with probability 1 .
As a curious gentleman, Matt wants to calculate E[X3], where X represents the number of bright lights at the end, E[X3] represents the expectation of cube of X.
Input
The first line contains only one integer T , which indicates the number of test cases.
For each test case, the first line contains N, M (1 ≤ N, M ≤ 50), denoting the number of fluorescent lights (numbered from 1 to N ) and the number of switches (numbered from 1 to M ).
M lines follow. The i-th line begins with an integer Ki (1 ≤ Ki ≤ N ). Ki distinct integers lij(1 ≤ lij ≤ N ) follow, denoting the fluorescent lights that the i-th switch controls.
Output
For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the answer. To avoid rounding error, the answer you should output is:
E[X3] × 2M mod (109 + 7)
Sample Input
2
2 2
1 1
2 12
3 1
3 1 2 3
Sample Output
Case #1: 10
Case #2: 27
题意解释:
有n(n<=50)个灯,初始状态都是关闭,有m个开关,每个开关都控制若干个灯,按下一个开关,他所控制的灯就会改变状态。问在m个开关按下与否的2^m的情况中,求每种情况下亮灯数量的立方和。
样例解释:
对于样例的第一组数据:共有两个灯,两个开关。第一个开关控制1号灯,第二个开关控制1号和2号灯。可知是否按开关共有2^2=4种状态:
1和2均不按,亮灯数为0 按1,亮灯数为1 按2,亮灯数为2 同时按1和2,亮灯数为1
所以最后答案为:0^3+1^3+2^3+1^3=10
思路:
设一种情况是开灯数是大X,这种情况下每一个灯的开关状况为小x
X = x1+x2+x3...xn xi是第i个灯的开闭情况(0或1)
X^3=(x1+x2+x3...xn)*(x1+x2+x3...xn)*(x1+x2+x3...xn)=∑((Xi * Xj * Xk)
E[X] * 2^M = X1+X2+...+Xn ,Xi是每种情况i的亮灯总数
E[X^3] * 2^M = X^3=∑(Xi * Xj * Xk)
显然,只有当Xi, Xj, Xk都为1时,Xi * Xj * Xk才有效,才可累计。所以此问题可用状态压缩DP求解
用二进制数0~7压缩表示Xi, Xj, Xk的状态
dp[ i ][ state ] 表示前 i 个按钮,使 Xi, Xj, Xk 状态为 state 的方法数
枚举i, j, k,ans = ∑ (dp[m][7])
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MOD=1e9+7;
const int maxn=55;
long long temp[maxn];
long long dp[maxn][8];
int n,m;
int main()
{
int cas=1,T;
cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=0;i<m;i++)
{
long long t=0;
int k;
cin>>k;
for(int j=1;j<=k;j++)
{ int x;
scanf("%d",&x);
x--;
t |= (1<<x);
}
temp[i]=t;//temp[i]转化为二进制表示第i个开关对应控制的灯
}
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
for(int k=0;k<n;k++)
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int t=0;t<m;t++)
{
int flag=0;
if(temp[t]>>i & 1) flag ^= 1;//001
if(temp[t]>>j & 1) flag ^= 2;//010
if(temp[t]>>k & 1) flag ^= 4;//100
for(int x=0;x<8;x++)
{
dp[t+1][x^flag]=(dp[t+1][x^flag]+dp[t][x])%MOD;
dp[t+1][x]=(dp[t+1][x]+dp[t][x])%MOD;
}
}
ans=(ans+dp[m][7])%MOD;
}
printf("Case #%d: %d\n",cas++,ans);
}
return 0;
}