HDU 5117 Fluorescent

|--状压DP 同时被 2 个专栏收录
1 篇文章 0 订阅
11 篇文章 0 订阅

题意:有n(n<=50)个灯,初始状态都是关闭,有m个开关,每个开关都控制若干个灯,按下一个开关,他所控制的灯就会改变状态。问在m个开关按下与否的2^m的情况中,求每种情况下亮灯数量的立方和。


解法:假设一种情况是开灯数是X, X=(x1+x2+x3...xn),xi是第i个灯的开闭情况。

X^3=(x1+x2+x3...xn)*(x1+x2+x3...xn)*(x1+x2+x3...xn) = sigma(Xi * Xj * Xk)

期望E[X] * 2^M=X1+X2+...+XN

E[X^3] * 2^M = X^3 = sigma(Xi * Xj * Xk)

当Xi, Xj, Xk都为1时,加上符合条件的方法数。

我们可以压缩Xi, Xj, Xk的状态。

DP[t][s] = 前 t 个按钮, 使得Xi, Xj, Xk状态为 s 的方法数。

枚举i, j, k,然后就是简单DP了。

ans = sigma(DP[M][7])

Fluorescent

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


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 ≤ K i ≤ N ). K i distinct integers l ij(1 ≤ l ij ≤ 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
 

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define prt(k) cout<<#k" = "<<k<<endl;
typedef long long ll;
#include <algorithm>

const ll mod = 1e9 + 7;
const int N = 52;
ll S[N];
int DP[N][8];
int n,m ;
int i, j, k;
int g( ll s)
{
    int ret = 0;
    if (s >> i & 1) ret ^= 1;
    if (s >> j & 1) ret ^= 2;
    if (s >> k & 1) ret ^= 4;
    return ret;
}
void add(int &a, int b) { a=(a+b)%mod; }
int main()
{
    int re; int ca=1;
    cin>>re;
    while (re--)
    {
        cin>>n>>m;
        for (int i=0;i<m;i++)
        {
            ll t = 0;
            int k; cin>>k;
            while (k--)
            {
                int x ;scanf("%d", &x);
                x--;
                t |= (1ll<<x);
            }
            S[i] = t;
        }
        int ans = 0;
        for (i=0;i<n;i++)
        {
            for (j=0;j<n;j++)
            {
                for (k=0;k<n;k++)
                {
                    memset(DP, 0, sizeof DP);
                    DP[0][0] = 1;
                    for (int t=0;t<m;t++)
                    {
                        int s = g(S[t]);
                        for (int x=0;x<8;x++)
                        {
                            add(DP[t+1][x^s], DP[t][x]);
                            add(DP[t+1][x], DP[t][x]);
                        }
                    }
                    add(ans, DP[m][7]);
                }
            }
        }
        printf("Case #%d: %d\n", ca++, ans);
    }
    return 0;
}


  • 1
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

oilover

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值