HDU3364 Lanterns
? 解题记录 ? ? HDU ? ? 高斯消元 ?    2018-04-02 16:22:13    404    0    0
Problem Description
Alice has received a beautiful present from Bob. The present contains n lanterns and m switches. Each switch controls some lanterns and pushing the switch will change the state of all lanterns it controls from off to on or from on to off. A lantern may be controlled by many switches. At the beginning, all the lanterns are off. 

Alice wants to change the state of the lanterns to some specific configurations and she knows that pushing a switch more than once is pointless. Help Alice to find out the number of ways she can achieve the goal. Two ways are different if and only if the sets (including the empty set) of the switches been pushed are different.
 


Input
The first line contains an integer T (T<=5) indicating the number of test cases.
The first line of each test case contains an integer n (1<=n<=50) and m (1<=m<=50).
Then m lines follow. Each line contains an integer k (k<=n) indicating the number of lanterns this switch controls.
Then k integers follow between 1 and n inclusive indicating the lantern controlled by this switch.
The next line contains an integer Q (1<=Q<=1000) represent the number of queries of this test case.
Q lines follows. Each line contains n integers and the i-th integer indicating that the state (1 for on and 0 for off) of the i-th lantern of this query.
 


Output
For each test case, print the case number in the first line. Then output one line containing the answer for each query.
Please follow the format of the sample output.
 


Sample Input
2
3 2
2 1 2
2 1 3
2
0 1 1
1 1 1
3 3
0
0
0
2
0 0 0
1 0 0
 


Sample Output
Case 1:
1
0
Case 2:
8
0
 


Source
 


Recommend
lcy   |   We have carefully selected several similar problems for you:  3362 3363 3361 3365 3367 

高斯消元消异或方程组,我们把每一个灯的状态列成一些线性异或方程组,然后就可以高斯消元了!最后的答案就是2的自由元次幂。

#include<cstdio>
#include<cstring>
#define LL long long
using namespace std;
const int maxn = 55 + 5;
LL mtx[maxn], Nmtx[maxn], ans;
int n, num, m, a, q, freenum, t, vis[maxn];

bool Gbit(LL mask, int i) {
    return (mask & (1LL << i - 1));
}

LL Gauss(int n, int m) {
	memset(vis, 0, sizeof(vis));
    register int p = 1, now = 1;
    LL i = 1;int ans = 0;
    for(; p <= m; ++p, i <<= 1) {
        freenum = 1;
        for(register int j = 1; j <= n; ++j) {
        	if(vis[j]) continue;
            if(mtx[j] & i) {
                for(register int k = 1; k <= n; ++k)
                    if(j != k && (mtx[k] & i) && !vis[k]) mtx[k] ^= mtx[j];
                freenum = 0, vis[j] = 1;
                break;
            }
        }
        if(freenum) ++ans;
    }
    for(register int i = 1; i <= n; ++i) {
        if(!vis[i] && (!Gbit(mtx[i], m) && Gbit(mtx[i], m + 1)))
            return -1;
    }
    return ans;
}
/*m是灯 n是开关*/
int main() {
    scanf("%d", &t);
    for(register int cs = 1; cs <= t; ++cs) {
        memset(Nmtx, 0, sizeof(Nmtx));
        scanf("%d%d", &m, &n);
        for(register int i = 1; i <= n; ++i) {
            scanf("%d", &num);
            for(register int j = 1; j <= num; ++j)
                scanf("%d", &a), Nmtx[a] |= 1LL << i - 1;
        }
        printf("Case %d:\n", cs);
        scanf("%d", &q);
        for(register int i = 1; i <= q; ++i) {
            memcpy(mtx, Nmtx, sizeof(mtx));
            for(register int j = 1; j <= m; ++j) {
                scanf("%d", &a);
                if(a) mtx[j] |= 1LL << n;
            }
            ans = Gauss(m, n);
            printf("%lld\n", ans != -1 ? (1LL << ans) : 0);
        }
    }
    return 0;
}


上一篇: 洛谷P4151 [WC2011]最大XOR和路径

下一篇: 洛谷P3980 [NOI2008]志愿者招募

404 人读过
立即登录, 发表评论.
没有帐号? 立即注册
0 条评论
文档导航