The number of set(位运算+状态dp)一道十分美妙的题目哦!!!!!!

Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use these sets to combinate the new set,how many different new set you can get.The given sets can not be broken.

代码:

#include<bits/stdc++.h>
using namespace std;
int s[1<<15];//集合最大容量
int n,m,k,e;
//输入n个集合,每个集合的容量上限是m.
//集合元素 e
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int ans=0;
		memset(s,0,sizeof(s));
		while(n--)
		{
			int set=0;
			scanf("%d",&k);
			//每个集合元素总数有k个
			while(k--)
			{
				scanf("%d",&e);
				//用一个二进制数保存一个集合的元素
				set=set|(1<<(e-1));
			}
			//状态数组吧,我想!
			s[set]=1;

			//(1<<14)
			//借助于位运算合并集
			for(int j=0;j<=(1<<14);j++)
			{
				if(s[j]) s[set|j]=1;
			}
		}

		for(int i=0;i<=1<<14;i++)
		{
			if(s[i]) ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

赏析:

能力积累:

1.

int setval,size,element;//setval作为集合一一对应的特征值。
void f()
{
    setval=0;
    scanf("%d",&size);
    while(size--)
    {
        scanf("%d",&element);
        setval=setval|(1<<(element-1));
    }
    cout<<setval<<endl;
}

 

参考:

https://www.cnblogs.com/lonelycatcher/archive/2011/05/27/2060158.html

 

posted @ 2019-08-13 14:11  龙龙666666  阅读(238)  评论(0编辑  收藏  举报