TOJ4505: KOSARE
Total Submit: 11 Accepted:3
Description
Mirko found N boxes with various forgotten toys at his attic. There are M different toys, numbered 1 through M, but each of those can appear multiple times across various boxes.
Mirko decided that he will choose some boxes in a way that there is at least one toy of each kind present, and throw the rest of the boxes away.
Determine the number of ways in which Mirko can do this.
Input
The first line of input contains two integers N and M (1 ≤ N ≤ 1 000 000, 1 ≤ M ≤ 20).
Each of the following N lines contains an integer Ki (0 ≤ Ki ≤ M) followed by Ki distinct integers from interval [1, M], representing the toys in that box.
Output
The first and only line of output should contain the requested number of ways modulo 1 000 000 007.
Sample Input
3 3
3 1 2 3
3 1 2 3
3 1 2 3
Sample Output
7
Source
#include<stdio.h> const int N=1<<20,MD=1e9+7; int cnt[N],B[N],n,m,ans[N],f; int main() { scanf("%d%d",&n,&m); f=1<<m; B[0]=1; for(int i=1; i<=n; i++) B[i]=(B[i-1]<<1)%MD; for(int i=0,t,x,s; i<n; i++) { scanf("%d",&t); s=0; while(t--) scanf("%d",&x),s|=1<<x-1; cnt[s]++; } for(int i=0; i<m; i++) for(int s=0; s<f; s++) if(s&1<<i) cnt[s]+=cnt[s^1<<i]; for(int s=0; s<f; s++) ans[s]=B[cnt[s]]; for(int i=0; i<m; i++) for(int s=0; s<f; s++) if(s&1<<i) ans[s]=(ans[s]-ans[s^1<<i]+MD)%MD; printf("%d",ans[f-1]); return 0; }