题意:有 n 个灯,初始状态都是关闭,有m个开关,每个开关都控制若干个。问在m个开关按下与否的2^m的情况中,求每种情况下亮灯数量的立方和。

析:首先,如果直接做的话,时间复杂度无法接受,所以要对其进行小小的变形,设开灯数X,和每个开关的状态的对应关系是X = x1+x2+...+xn,其中 xi 可能为0,可能为1,那么要求的数X^3 = (x1+x2+...+xn) * (x1+x2+...+xn) * (x1+x2+...+xn) = ∑(xi*xj*xk),只有xi = xj = xk时,那么这种情况才会成立,所以对xi,xj,xk 进行枚举,dp[t][s] 表示前 t 个开关时,状态为s的可能数,这个状态 s 表示的是 xi,xj,xk的状态,只有s == 7 时,才满足条件。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} LL dp[maxn][8];
LL st[maxn]; int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; ++i){
st[i] = 0;
int x, y;
scanf("%d", &y);
for(int j = 0; j < y; ++j){
scanf("%d", &x);
st[i] |= 1LL<<x-1;
}
}
LL ans = 0;
FOR(i, 0, n) FOR(j, 0, n) FOR(k, 0, n){
ms(dp, 0); dp[0][0] = 1;
for(int t = 1; t <= m; ++t){
for(int s = 0; s < 8; ++s){
int newst = 0;
if(st[t]&1LL<<i) newst |= 1;
if(st[t]&1LL<<j) newst |= 2;
if(st[t]&1LL<<k) newst |= 4;
dp[t][s] = dp[t-1][s] + dp[t-1][s^newst];
}
}
ans = (ans + dp[m][7]) % mod;
}
printf("Case #%d: %I64d\n", kase, ans);
}
return 0;
}

  

HDU 5117 Fluorescent (数学+状压DP)的更多相关文章

  1. hdu 3247 AC自动+状压dp+bfs处理

    Resource Archiver Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Ot ...

  2. hdu 2825 aC自动机+状压dp

    Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. HDU 5765 Bonds(状压DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5765 [题目大意] 给出一张图,求每条边在所有边割集中出现的次数. [题解] 利用状压DP,计算不 ...

  4. hdu 3681(bfs+二分+状压dp判断)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 思路:机器人从出发点出发要求走过所有的Y,因为点很少,所以就能想到经典的TSP问题.首先bfs预 ...

  5. hdu 4778 Gems Fight! 状压dp

    转自wdd :http://blog.csdn.net/u010535824/article/details/38540835 题目链接:hdu 4778 状压DP 用DP[i]表示从i状态选到结束得 ...

  6. hdu 4856 Tunnels (bfs + 状压dp)

    题目链接 The input contains mutiple testcases. Please process till EOF.For each testcase, the first line ...

  7. HDU 4272 LianLianKan (状压DP+DFS)题解

    思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...

  8. HDU 3362 Fix (状压DP)

    题意:题目给出n(n <= 18)个点的二维坐标,并说明某些点是被固定了的,其余则没固定,要求添加一些边,使得还没被固定的点变成固定的, 要求总长度最短. 析:由于这个 n 最大才是18,比较小 ...

  9. HDU 3001 Travelling (状压DP,3进制)

    题意: 给出n<=10个点,有m条边的无向图.问:可以从任意点出发,至多经过同一个点2次,遍历所有点的最小费用? 思路: 本题就是要卡你的内存,由于至多可经过同一个点2次,所以只能用3进制来表示 ...

随机推荐

  1. Linux配置Nginx+Tomcat负载均衡

    cd /usr/local/tomcat1/webapps/ROOT/ tar -zxvf nginx-1.14.2.tar.gz -C /usr/local 一.Linux配置Nginx 一.下载N ...

  2. DNS记录类型名单

    原文:http://www.worldlingo.com/ma/enwiki/zh_cn/List_of_DNS_record_types DNS记录类型名单 这 DNS记录类型名单 提供一个方便索引 ...

  3. 开始SDK之旅-入门2-集成流程图、轨迹图到系统

    http://bbs.ccflow.org/showtopic-2562.aspx 经测试,基本可用,还需增加 WF/Admin/pub.ascx 首先你得先理解流程图的数据的获取方式,其他的就很容易 ...

  4. (转)ASP连接sql server实例解析

    本文转载自:http://blog.csdn.net/xys_777/article/details/5696276 1.首先确定自己的iis没有问题 2.其次确定自己sqlserver没有问题 然后 ...

  5. [转]命令行在IIS添加虚拟目录

    来自:http://www.jb51.net/softjc/29702.htmlMkwebdir -c LocalHost -w "Default Web Site" –v Com ...

  6. logger示例

    胜哥版 打印日志是很多程序的重要需求,良好的日志输出可以帮我们更方便的检测程序运行状态.Python标准库提供了logging模块,让我们也可以方便的在Python中打印日志. 日志介绍 完整的使用方 ...

  7. ElasticSearch 基础概念学习(未完)

    1.基本定义 摘自百度百科 elasticseaElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elastic ...

  8. springboot 测试 有注入HttpSession的bean

    question: nested exception is java.lang.IllegalStateException: No thread-bound request found: Are yo ...

  9. python学习-day 2

    1.执行Python脚本的两种方式1)调用解释器 Python +绝对路径+文件名称2)调用解释器 Python +相对路径+文件名称 2.简述位.字节的关系8位为1个字节 3.简述ASCII.uni ...

  10. open语句对文本和二进制文件的读写

    文本文件的操作此种方式是以行为单位进行读取的基本单位,主要应用的方法和函数有Open,Close,Line Input,FreeFile,EOF等.先简述其功能然后结合代码示例进行说明.Open:顾名 ...