题解:[COCI 2011/2012 #6] KOŠARE

题目传送门

题意分析

观察数据范围,发现 \(n\leq10^6\),但 \(m\leq20\),考虑一个 \(2^m\) 的做法。

设 \(\textit{cnt}_s\) 为玩具状态为 \(s\) 的子集的箱子个数,可以高维前缀和 \(\mathcal O\left(n+m2^m\right)\) 预处理。

之后考虑求答案,设 \(g_s\) 表示玩具状态为 \(s\) 的子集的箱子方案数,有 \(g_s=2^{\textit{cnt}_s}\)。

容斥一下,则有最终方案数为:

\[\sum_{T\subseteq\set{1,2,3,\cdots,m}}(-1)^{m-\vert T\vert}g_T \]

AC 代码

//#include<bits/stdc++.h>
#include<algorithm> 
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
constexpr const int N=1e6,M=20,P=1e9+7;
int n,m,cnt[1<<M|1],g[1<<M|1];
int qpow(int base,int n){
	int ans=1;
	while(n){
		if(n&1){
			ans=1ll*ans*base%P;
		}
		base=1ll*base*base%P;
		n>>=1;
	}
	return ans;
}
int popcount(int n){
	int ans=0;
	while(n){
		ans++;
		n-=n&-n;
	}
	return ans;
}
int main(){
	/*freopen("test.in","r",stdin);
	freopen("test.out","w",stdout);*/
	
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int k;
		cin>>k;
		int pl=0;
		while(k--){
			int x;
			cin>>x;
			pl|=1<<x-1;
		}
		cnt[pl]++;
	}
	for(int i=0;i<m;i++){
		for(int j=0;j<(1<<m);j++){
			if(j&(1<<i)){
				cnt[j]=(cnt[j]+cnt[j^(1<<i)])%P;
			}
		}
	}
	for(int i=0;i<(1<<m);i++){
		g[i]=qpow(2,cnt[i]);
	}
	int ans=0;
	for(int i=0;i<(1<<m);i++){
		if((m-popcount(i))&1){
			ans=(ans-g[i])%P;
		}else{
			ans=(ans+g[i])%P;
		}
	}
	if(ans<0){
		ans+=P;
	}
	cout<<ans<<'\n';
	
	cout.flush(); 
	
	/*fclose(stdin);
	fclose(stdout);*/
	return 0;
}
posted @ 2026-10-11 20:10  TH911  阅读(11)  评论(0)    收藏  举报