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 ;
}

动态规划(DP计数):HDU 5117 Fluorescent的更多相关文章

  1. HDU - 5117 Fluorescent(状压dp+思维)

    原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...

  2. HDU 5117 Fluorescent (数学+状压DP)

    题意:有 n 个灯,初始状态都是关闭,有m个开关,每个开关都控制若干个.问在m个开关按下与否的2^m的情况中,求每种情况下亮灯数量的立方和. 析:首先,如果直接做的话,时间复杂度无法接受,所以要对其进 ...

  3. HDU 5117 Fluorescent

    题目链接:HDU-5117 题意为有n盏灯,m个开关,每个开关控制着\( k_{i} \)灯.X为最后亮着的灯的个数,要求出\( E(X^{3} ) * 2^{M} mod (10^9 + 7) \) ...

  4. 动态规划(DP计数):HDU 5116 Everlasting L

    Matt loves letter L.A point set P is (a, b)-L if and only if there exists x, y satisfying:P = {(x, y ...

  5. HDU 4055 The King’s Ups and Downs(DP计数)

    题意: 国王的士兵有n个,每个人的身高都不同,国王要将他们排列,必须一高一矮间隔进行,即其中的一个人必须同时高于(或低于)左边和右边.问可能的排列数.例子有1千个,但是最多只算到20个士兵,并且20个 ...

  6. HDU 4055 Number String(DP计数)

    题意: 给你一个含n个字符的字符串,字符为'D'时表示小于号,字符为“I”时表示大于号,字符为“?”时表示大小于都可以.比如排列 {3, 1, 2, 7, 4, 6, 5} 表示为字符串 DIIDID ...

  7. 动态规划dp

    一.概念:动态规划dp:是一种分阶段求解决策问题的数学思想. 总结起来就一句话:大事化小,小事化了 二.例子 1.走台阶问题 F(10):10级台阶的走法数量 所以:F(10)=F(9)+F(8) F ...

  8. 【POJ1952】逢低吸纳 dp+计数

    题目大意:给定一个有 N 个数的序列,求其最长下降子序列的长度,并求出有多少种不同的最长下降子序列.(子序列各项数值相同视为同一种) update at 2019.4.3 题解:求最长下降子序列本身并 ...

  9. 算法-动态规划DP小记

    算法-动态规划DP小记 动态规划算法是一种比较灵活的算法,针对具体的问题要具体分析,其宗旨就是要找出要解决问题的状态,然后逆向转化为求解子问题,最终回到已知的初始态,然后再顺序累计各个子问题的解从而得 ...

随机推荐

  1. OC - 4.OC核心语法

    一.点语法 1> 基本使用 点语法本质上是set方法/get方法的调用 2> 使用注意 若出现在赋值操作符的右边,在执行时会转换成get方法 若出现在赋值操作符的左边,在执行时会转换成se ...

  2. sharepoint2013 新建母板页 新建页面布局 关联母板页和页面布局

    1     母板页的应用和layout(页面布局)的创建和应用 母板页上传:将准备好的html和样式 通过spd中的导入方式导入模版html, 导入后: 然后在网站设置中进行转换为母板页.  随后编辑 ...

  3. 用crontab、crond在嵌入式系统中添加定时任务

    在嵌入式系统中,定时任务通过crond和cronttab两个系统命令来联合执行. 其中crond是定时任务的守护进程,系统开始时是没有开启的.crontab主要作用是管理用户的crontab file ...

  4. P1396 营救

    P1396 营救 218 通过 571 提交 题目提供者yeszy 标签 二分 图论 并查集 福建省历届夏令营 难度 普及- 题目描述 "咚咚咚--""查水表!" ...

  5. (hdu)5234 Happy birthday 二维dp+01背包

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5234 Problem Description Today is Gorwin’s birt ...

  6. 网站前端优化 -saveForSelf

    九.网站前端优化 – IMG 9.1.可以优化的图片 图片总共可以分为两类,一个是CSS IMAGES,另一个是产品图片. 9.2.CSS IMAGES CSS图片现在采用的方式是图片合并的方式,这样 ...

  7. [GUIDE] How to install Scipy in Maya Windows 64 bit - Google 网上论坛 - Google Chrome

    I've seen a lot of queries about getting scipy working in Maya (Windows 64 bit) with a few not 100% ...

  8. 【译】4个你需要知道的Asset Catalog的秘密

    本文由CocoaChina译者 @唧唧歪歪 翻译,作者:Hector Matos 原文:4 XCODE ASSET CATALOG SECRETS YOU NEED TO KNOW 恶梦 想象你正在干 ...

  9. ipad ------ 与iPhone的差别

    1. 差异 iPhone是手机,iPad.iPad Mini是平板电脑 iPhone和iPad开发的区别 屏幕的尺寸 \分辨率 UI元素的排布 \设计 键盘 API 屏幕方向的支持 … … 2. iP ...

  10. iOS 静态库中使用宏定义区分iPhone模拟器与真机---备用

    问题描述 一般项目中,可以使用宏定义来判断模拟器还是真机,这无疑是有效的. #if TARGET_IPHONE_SIMULATOR #define SIMULATOR 1 #elif TARGET_O ...