动态规划(DP计数):HDU 5117 Fluorescent

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 1 2
3 1
3 1 2 3

Sample Output

Case #1: 10
Case #2: 27

Hint

For the first sample, there’re 4 possible situations: All the switches is off, no light is bright, X^3 = 0. Only the first switch is on, the first light is bright, X^3 = 1. Only the second switch is on, all the lights are bright, X^3 = 8. All the switches is on, the second lights are bright, X^3 = 1. Therefore, the answer is E[X^3] × 2^2 mod (10^9 + 7) = 10. For the second sample, there’re 2 possible situations: The switches is off, no light is bright, X^3 = 0. The switches is on, all the lights are bright, X^3 = 27. Therefore, the answer is E[X^3] × 2^1 mod (10^9 + 7) = 27.
  
  这道题就是对其计数。
  发现每个情况,假设有x1,x2,…,xk,k个灯亮,那么答案就是k³,答案可以这样计:有三排同样的,作相同的变换,每次各选定一个灯,看最后是否全亮,是则对答案有1的贡献。
  现在先枚举i,j,k三个灯,再DP即可。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int N=,Mod=(int)1e9+;
typedef long long LL;
LL dp[N][],op[N][N];
int T,cas,n,m;LL ans;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
memset(op,,sizeof(op));
for(int i=,k,x;i<=m;i++){
scanf("%d",&k);
while(k--){
scanf("%d",&x);
op[i][x]=;
}
}
ans=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
for(int k=;k<=n;k++){
memset(dp,,sizeof(dp));
dp[][]=;
for(int t=;t<=m;t++){
for(int p=;p<=;p++)
dp[t][p]=dp[t-][p];
int go=;
if(op[t][k])go+=;
if(op[t][j])go+=;
if(op[t][i])go+=;
for(int p=;p<=;p++)
(dp[t][p^go]+=dp[t-][p])%=Mod;
}
(ans+=dp[m][])%=Mod;
}
printf("Case #%d: %lld\n",++cas,ans);
}
return ;
}
上一篇:CF 86D Powerful array 【分块算法,n*sqrt(n)】


下一篇:pig相关