Fluorescent UVALive - 7063 (状压dp+三次方分解)

Fluorescent

Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 937    Accepted Submission(s): 477


 

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 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.

 

 

Source

2014ACM/ICPC亚洲区北京站-重现赛(感谢北师和上交)

 

 

Recommend

liuyiding

题目大意:给出n盏灯,m个开关,这m个开关连接着一些灯,当摁下

开关时,这些相连接的灯的状态将会发生改变。问这些开关是否摁下的2^m个状态中亮着的灯的的三次方的和。

思路:我们把灯亮着看做是1,暗了看做是0.那么亮着的灯的总数就可以看做是所有灯的状态的和。

例如此时有X盏灯亮着,xi代表第i盏灯是亮是暗,那么X=x1+x2+x3+x4+.......xn而其三次方按照牛顿展开式就是\sumCaiajak,据组合公式我们可以得知当三者相等时则为1两者相等则为。。。当然,这些都不重要这都是组合的结果,实际上我们只要把所有的结果都假如即可。知道了这些之后我们对所有的X的三次方求和,那么在我们只需要求出不同的i,j,k组合下,不同的开关组合西,ai,aj,ak均为1的情况数的总合即可。这样每次就只需要对三盏灯进行dp。

详情见代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
bool op[55][55];
int dp[55][8];
const int mod=1e9+7;
int main()
{
	int t;
	scanf("%d",&t);
	for(int kace = 1; kace <= t; kace++){
		int n,m;
		int ans=0;
		memset(op,0,sizeof(op));
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
		{
			int s;
			scanf("%d",&s);
			for(int j=1;j<=s;j++) {int p;scanf("%d",&p);op[i][p]=true;}
		}
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				for(int k=1;k<=n;k++){
					int v=0;
					memset(dp,0,sizeof(dp));
					dp[0][0]=1;
					for(int l=1;l<=m;l++){
						int s=0;
						if(op[l][i]) s|=1;
						if(op[l][j]) s|=2;
						if(op[l][k]) s|=4;
						for(int v=0;v<8;v++){
							dp[l][v]=(dp[l-1][v]+dp[l-1][v^s])%mod;
						}
					}
					ans=(ans+dp[m][7])%mod;
				}
			}
		}
		printf("Case #%d: %d\n",kace,ans);
	}
}

 

posted @ 2018-09-26 23:03  Fly_White  阅读(143)  评论(0编辑  收藏  举报