zoukankan      html  css  js  c++  java
  • 动态规划(DP计数):HDU 5117 Fluorescent

    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

    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.
      
      这道题就是对其计数。
      发现每个情况,假设有x1,x2,…,xk,k个灯亮,那么答案就是k³,答案可以这样计:有三排同样的,作相同的变换,每次各选定一个灯,看最后是否全亮,是则对答案有1的贡献。
      现在先枚举i,j,k三个灯,再DP即可。
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int N=51,Mod=(int)1e9+7;
     6 typedef long long LL;
     7 LL dp[N][8],op[N][N];
     8 int T,cas,n,m;LL ans;
     9 int main(){
    10     scanf("%d",&T);
    11     while(T--){
    12         scanf("%d%d",&n,&m);
    13         memset(op,0,sizeof(op));
    14         for(int i=1,k,x;i<=m;i++){
    15             scanf("%d",&k);
    16             while(k--){
    17                 scanf("%d",&x);
    18                 op[i][x]=1;
    19             }
    20         }
    21         ans=0;
    22         for(int i=1;i<=n;i++)
    23             for(int j=1;j<=n;j++)
    24                 for(int k=1;k<=n;k++){
    25                     memset(dp,0,sizeof(dp));
    26                     dp[0][0]=1;
    27                     for(int t=1;t<=m;t++){
    28                         for(int p=0;p<=7;p++)
    29                             dp[t][p]=dp[t-1][p];
    30                         int go=0;
    31                         if(op[t][k])go+=1;
    32                         if(op[t][j])go+=2;
    33                         if(op[t][i])go+=4;
    34                         for(int p=0;p<=7;p++)
    35                             (dp[t][p^go]+=dp[t-1][p])%=Mod;    
    36                     }
    37                     (ans+=dp[m][7])%=Mod;
    38                 }
    39         printf("Case #%d: %lld
    ",++cas,ans);            
    40     }
    41     return 0;
    42 }
  • 相关阅读:
    Request Validation in ASP.NET
    ANSI、Unicode、Unicode big endian、UTF8编码
    在win7下安装SQL sever2005
    配置SQL Server 2005 以允许远程连接
    传统网站与Web标准——DIV+CSS布局实例
    打造自己的reset.css
    传统网站与Web标准——表格布局实例
    每天工作4小时的程序员
    良好的XHTML规则
    列表导航栏实例(02)——精美电子商务网站赏析
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5943494.html
Copyright © 2011-2022 走看看