题目链接:

Fluorescent

Time Limit: 3000/3000 MS (Java/Others)    

Memory Limit: 512000/512000 K (Java/Others)

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
 
题意:一些灯和一些开关,每个开关都能控制一部分灯,求在开关的所有状态亮灯个数立方和
 
思路:ans=sigma(x^3) ,把x^3拆开就是(x1+x2+...xn)*(x1+x2+...+xn)*(x1+x2+...+xn),ans=sigma(∑∑∑(xi*xj*xk))=∑∑∑(sigma(使得xixjxk同时亮的状态数))
这样可以避免枚举2^m个状态,因为只有三个灯亮,状态为2^3,所以复杂度约为O(n^3*m))
 
AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m,a[51][51],k[50];
LL dp[51][8];
const LL mod=1e9+7;
LL solve(int u,int v,int w)
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<=m;i++)
{
int tep=0;
for(int j=1;j<=k[i];j++)
{
if(a[i][j]==u)tep+=1;
if(a[i][j]==v)tep+=2;
if(a[i][j]==w)tep+=4;
}
for(int j=0;j<8;j++)dp[i][j]=dp[i-1][j];
for(int j=0;j<8;j++)dp[i][j^tep]+=dp[i-1][j],dp[i][j^tep]%=mod;
}
return dp[m][7]%mod;
}
int main()
{
int T,Case=0;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d",&k[i]);
for(int j=1;j<=k[i];j++)scanf("%d",&a[i][j]);
}
LL ans=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
ans=ans+solve(i,j,k),ans%=mod;
printf("Case #%d: %lld\n",++Case,ans);
}
return 0;
}

  

hdu-5117 Fluorescent(状压dp)的更多相关文章

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

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

  2. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  3. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  4. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  5. hdu 4114(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...

  6. HDU 3091 - Necklace - [状压DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  7. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  8. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  9. hdu 4628 Pieces 状压dp

    题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...

随机推荐

  1. Redis——安装

    Linux下面: 下载安装包: wget http://download.redis.io/releases/redis-4.0.10.tar.gz 解压: tar xzf redis-4.0.10. ...

  2. mysql的两种常用的引擎

    MyISAM引擎特点1.不支持事务(事务是指逻辑上的一组操作,组成这组操作的各个单元,要么全成功,要么全失败)2.表级锁定(数据更新时锁整个表):其锁定机制是表级锁定,这虽然可以让锁定的实现成本很小但 ...

  3. Maven实战--- dependencies与dependencyManagement的区别

    在上一个项目中遇到一些jar包冲突的问题,之后还有很多人分不清楚dependencies与dependencyManagement的区别,本篇文章将这些区别总结下来. 1.DepencyManagem ...

  4. Python读写改Excel的方法

    (注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 面对疾风吧. 回首往昔,更进一步. 且随疾风前行,身后一许流星. 正文: 数据处理是Python的一大应用场景,而 Exce ...

  5. jQuery单选框跟复选框美化

    在线演示 本地下载

  6. 20145229吴姗姗web安全基础实践

    20145229吴姗姗web安全基础实践 基础与实践 基础问题 (1)SQL注入攻击原理,如何防御 SQL注入就是把SQL语句插入到之前已经定义好的语句中,作为网页中的比如用户名输入来达到攻击的目的, ...

  7. NSwag Tutorial: Integrate the NSwag toolchain into your ASP.NET Web API project

    https://blog.rsuter.com/nswag-tutorial-integrate-the-nswag-toolchain-into-your-asp-net-web-api-proje ...

  8. C#与C++之间类型对应关系

    //C++中的DLL函数原型为  //extern "C" __declspec(dllexport) bool 方法名一(const char* 变量名1, unsigned c ...

  9. Java 获取路径的几种方法 - 转载

    1.获取当前类所在的“项目名路径” String rootPath = System.getProperty("user.dir"); 2.获取编译文件“jar包路径”(反射) S ...

  10. Hystrix工作流程图