题意:有 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\n", kase, ans); } return 0; }
时间: 2024-10-24 23:30:30