题目描述
The FZU Code Carnival is a programming competetion hosted by the ACM-ICPC Training Center of Fuzhou University. The activity mainly includes the programming contest like ACM-ICPC and strive to provide participants with interesting code challenges in the future.
Before the competition begins, YellowStar wants to know which teams are likely to be winners. YellowStar counted the skills of each team, including data structure, dynamic programming, graph theory, etc. In order to simplify the forecasting model, YellowStar only lists M skills and the skills mastered by each team are represented by a 01 sequence of length M. 1 means that the team has mastered this skill, and 0 does not.
If a team is weaker than other teams, this team cannot be a winner. Otherwise, YellowStar thinks the team may win. Team A(a1, a2, ..., aM ) is weaker than team B(b1, b2, ..., bM ) if ∀i ∈ [1, M], ai ≤ bi and ∃i ∈ [1, M], ai < bi.
Since YellowStar is busy preparing for the FZU Code Carnival recently, he dosen’t have time to forecast which team will be the winner in the N teams. So he asks you to write a program to calculate the number of teams that might be winners.
输入
Input is given from Standard Input in the following format:
N M
s1 s2 . . . sN
The binary representation of si indicates the skills mastered by teami.
Constraints
1 ≤ N ≤ 2 × 106
1 ≤ M ≤ 20
0 ≤ si < 2M
输出
Print one line denotes the answer.
样例输入
复制样例数据
3 3 2 5 6
样例输出
2
#include<iostream>
using namespace std;
int dp[1<<20+1];//最多有2进制20位
int main()
{
int n,m;
cin>>n>>m;
int p;
int maxn=-2;
for(int i=1; i<=n; i++)
{
cin>>p;
dp[p]++;
maxn=max(maxn,p);//找到最大的值,从最大值开始覆盖。
}
int res=0;
for(int i=maxn; i>=0; i--)
{
if(dp[i])
{
if(dp[i]>0)
res+=dp[i];//如果没被覆盖,结果直接加上i的数量
for(int j=m-1; j>=0; j--)
{
if(i&(1<<j))//如果i的二进制在该位置有1,用1<<j左移j位与i异或将j位的1去掉覆盖标记该值。
{
dp[i^(1<<j)]=-2;
}
}
}
}
cout<<res<<endl;
return 0;
}