The frequent subset problem is defined as follows. Suppose U={1, 2,…,N} is the universe, and S1, S2,…,SMare M sets over U. Given a positive constant α, 0<α≤1, a subset B (B≠0) is α-frequent if it is contained in at least αM sets of S1, S2,…,SM, i.e. ∣{i:B⊆Si}∣≥αM. The frequent subset problem is to find all the subsets that are α-frequent. For example, let U={1,2,3,4,5}, M=3, α=0.5, and S1={1,5}, S2={1,2,5}, S3={1,3,4}. Then there are 3 α-frequent subsets of U, which are {1},{5} and {1,5}.
Input Format
The first line contains two numbers N and α, where N is a positive integers, and α 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+1 contains Si, 1≤i≤M . Your program should be able to handle N up to 20 and M up to 50.
Output Format
The number of α-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
题目来源
题意:先给你两个数 n 和 aa,n 表示一个有 n 个元素的集合 U = {1,2,......,n},而 aa 则表示在接下来的 M 个子集合 Si(i = 1, 2, ..., M)中,能否从中抽出一组数,使得这组数在这 M 个集合中存在的个数 >= aa * M,然后接下来有 M 行输入,表示子集合 S,输入完毕。要求你输出一共有多少组数符合上述条件。
题解:这道题就是典型的状压dp,但一开始我没想到这一点,一直tle。
直接上代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
///o(っ。Д。)っ AC万岁!!!!!!!!!!!!!!
const int maxn = 100;
int sum[maxn] = {};
int main()
{
int n;
float aa;
while(scanf("%d %f", &n, &aa) != EOF)
{
mem(sum, 0);
int cnt = 1, book;
char ch;
while(scanf("%d%c", &book, &ch) != EOF)
{
sum[cnt] += (1 << book);
if(ch == '\n') cnt++;
}
cnt--;
int jud = (cnt * aa) - (int)(cnt * aa) >= 0.000001 ? (int)(cnt * aa + 1) : (int)(cnt * aa);
///不是(int)(cnt * aa),因为会有像6.04这样的情况
///也可以这样:int jud = ceil(cnt * aa);
int ans = 0, maxx = 1 << n, counter = 0;
for(int i = 1; i <= maxx; i++)
{
ans = 0;
for(int j = 1; j <= cnt; j++)
{
if((i & sum[j]) == i)
{
ans++;
/*if(ans >= jud)
{
printf("i = %d sum[%d] = %d\n", i, j, sum[j]);
for(int k = 1; k <= n; k++)
{
if((1 << k) & i) printf("%d ", k);
}
printf("\n");
}
*/
}
}
if(ans >= jud) counter++;
}
printf("%d\n", counter);
}
return 0;
}
/*
*/