[HDU - 5117 ]-Fluorescent (状态压缩DP,期望)

[HDU - 5117 ]-Fluorescent (状态压缩DP,期望)

题目链接: HDU - 5117

题面:

题意:

给定\(\mathit n\)个灯泡,初始全是熄灭状态。

\(\mathit m\)个开关,每一个开关可以控制一些灯。当该开关被按下时,它控制的灯泡状态将取反。

每一个开关将被等概率的按下/不按下。设\(\mathit X\) 为最终被点亮的灯泡个数,求\(E[X^3] × 2^M mod (10^9 + 7)\)

思路:

\[E[X^3]\times2^M=\sum (X^{3} * (\frac{1}{2})^{M}) * 2^{M} = \sum X^{3} =\sum_{i,j,m=1}^{n}[l_i*l_j*l_k] \]

于是问题转化为,枚举所有的\(i,j,k\),求有多少种方案数使其让这三个灯都亮着,所有情况的总和就是答案。

我们对每一个\((i,j,k)\)三元组进行状压DP求解方案数,

\(dp[s][w]\) 代表对于前\(1\dots m\) 个开关,使其(i,j,k)三个灯是否点亮的状态为s的方案数。

初始状态:\(dp[0][0]=1\)

转移方程:对于第\(\mathit w\)个开关,(i,j,k)三个灯是否被其控制的状态为\(\mathit s\)

那么\(dp[q][w]=dp[q][w-1]+dp[q\oplus s][w-1],q\in[1,7]\)

该三元组对答案的贡献为\(dp[(111)_2][m]\)

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
const ll mod = 1e9 + 7;
int dp[8][55];
int n, m;
ll info[55];

int main()
{
#if DEBUG_Switch
    freopen("D:\\code\\input.txt", "r", stdin);
#endif
    //freopen("D:\\code\\output.txt","w",stdout);
    int t;
    t = readint();
    for (int icase = 1; icase <= t; ++icase) {
        n = readint(), m = readint();
        repd(i, 1, m) {
            info[i] = 0ll;
            int num = readint();
            while (num--) {
                int x = readint();
                info[i] |= (1ll << x);
            }
        }
        ll ans = 0ll;
        repd(i, 1, n) {
            repd(j, 1, n) {
                repd(k, 1, n) {
                    MS0(dp);
                    dp[0][0] = 1ll;
                    repd(w, 1, m) {
                        int s = 0;
                        if (info[w] & (1ll << i)) { s |= 1; }
                        if (info[w] & (1ll << j)) { s |= 2; }
                        if (info[w] & (1ll << k)) { s |= 4; }
                        repd(q, 0, 7) {
                            dp[q][w] = (dp[s ^ q][w - 1] + dp[q][w - 1]) % mod;
                        }
                    }
                    ans = (ans + dp[7][m]) % mod;
                }
            }
        }
        printf("Case #%d: %lld\n", icase, ans );
    }

    return 0;
}



posted @ 2020-10-22 20:46  茄子Min  阅读(114)  评论(0编辑  收藏  举报