计蒜客 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem (位压缩)

The frequent subset problem is defined as follows. Suppose UUU={1, 2,…\ldots,N} is the universe, and S1S_{1}S1, S2S_{2}S2,…\ldots,SMS_{M}SM are MMM sets over UUU. Given a positive constant α\alphaα, 0<α≤10<\alpha \leq 10<α1, a subset BBB (B≠0B \neq 0B0) is α-frequent if it is contained in at least αM\alpha MαM sets of S1S_{1}S1, S2S_{2}S2,…\ldots,SMS_{M}SM, i.e. ∣{i:B⊆Si}∣≥αM\left | \left \{ i:B\subseteq S_{i} \right \} \right | \geq \alpha M{i:BSi}αM. The frequent subset problem is to find all the subsets that are α-frequent. For example, let U={1,2,3,4,5}U=\{1, 2,3,4,5\}U={1,2,3,4,5}, M=3M=3M=3, α=0.5\alpha =0.5α=0.5, and S1={1,5}S_{1}=\{1, 5\}S1={1,5}, S2={1,2,5}S_{2}=\{1,2,5\}S2={1,2,5}, S3={1,3,4}S_{3}=\{1,3,4\}S3={1,3,4}. Then there are 333 α-frequent subsets of UUU, which are {1}\{1\}{1},{5}\{5\}{5} and {1,5}\{1,5\}{1,5}.

Input Format

The first line contains two numbers NNN and α\alpha α, where NNN is a positive integers, and α\alpha α is a floating-point number between 0 and 1. Each of the subsequent lines contains a set which consists of a sequence of positive integers separated by blanks, i.e., line i+1i + 1i+1 contains SiS_{i}Si, 1≤i≤M1 \le i \le M1iM . Your program should be able to handle NNN up to 202020 and MMM up to 505050.

Output Format

The number of α\alphaα-frequent subsets.

样例输入
15 0.4
1 8 14 4 13 2
3 7 11 6
10 8 4 2
9 3 12 7 15 2
8 3 2 4 5
样例输出
11
题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛




题意:
有M个全集U下的子集S1....SM,问你存在多少个子集被k个Si包含(即该子集是Si的子集)且k>=(M*a),全集就是{1,2,3,...,n}

解析:
因为n最大只有20个,所以可以用二进制表示。一个n答案(子集)最多有((1<<n)-1)个,就枚举答案,再将每一个答案与每一个集合里的元素(二进制表示)比较,若包含次数>=Ma,说明该答案满足条件

#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
using namespace std;

const int N = 30;
const int M = 60;

int mm[M];
int n;
int ans;
int Ma;

int main()
{
	double a;
	scanf("%d%lf",&n,&a);
	getchar();
	int line=0;
	char str[1000];
	while(gets(str)!=NULL)
	{
		line++;
		int temp=0;
		for(int i=0;str[i]!='\0';i++)
		{
			
			if(str[i]==' '||str[i]=='\0')
			{
				mm[line]=mm[line]|(1<<(temp-1));
				temp=0;
			}
			else
			{
				temp=temp*10+str[i]-'0';
			}
		}
		
		if(temp) 
		{
			mm[line]=mm[line]|(1<<(temp-1));
		}
		//if(line==5) break;
	}
	Ma=(int)((a*line)+0.999999999);
	ans=0;
	for(int i=1;i<(1<<n);i++)
	{
		int fi=0;
		for(int j=1;j<=line;j++)
		{
			if((i&mm[j])==i) fi++;
			if(fi>=Ma) break;
		}
		if(fi>=Ma) ans++;
	}
    printf("%d\n",ans);
    return 0;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值