• HDU 5117 Fluorescent (数学+状压DP)


    题意:有 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
    ", kase, ans);
      }
      return 0;
    }
    

      

  • 相关阅读:
    通过Jenkins调用自动部署war包及jar包到服务器上的Shell脚本
    CentOS7.3+MySQL5.7+Apache2.4+PHP7.1+phpMyAdmin4.7+JDK1.8+SVN1.6+Jenkins2.1环境搭建
    telegraf1.8+influxdb1.6+grafana5.2 环境搭建 结合JMeter3.2
    HttpRunner环境搭建
    Jenkins中启动从节点时,出现问题如何解决,问题:No Known Hosts...
    python读xml文件
    使用poi或jxl,通过java读写xls、xlsx文档
    编写生成32位大写和小写字符的md5的函数
    将一个字符与对应Ascii码互转
    生成随机删除的航班信息
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7563473.html
Copyright © 2020-2023  润新知