zoukankan      html  css  js  c++  java
  • hdu-5117 Fluorescent(状压dp)

    题目链接:

    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
    ",++Case,ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Java学习笔记21---内部类之对成员内部类的补充说明(二)(修正)
    Java学习笔记20---内部类之对成员内部类的补充说明(一)
    Java学习笔记19---内部类之简介成员内部类、局部内部类及匿名内部类
    Java学习笔记18---final关键字修饰变量、方法及类
    Java学习笔记17---成员方法的重载与重写
    Java学习笔记16---抽象类与接口的浅显理解
    Java学习笔记15---instanceof与向下转型
    把大端、小端与堆、栈的生长方向联系起来记忆
    2020综合实践—第7次实践作业 03组
    2020综合实践 第6次实践作业 03组
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/6786657.html
Copyright © 2011-2022 走看看