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.
思路:book[i]表示数字i是否是其他数的子集,若是book[i]=-1,否则book[i]>0,表示数字i有几个。枚举数字i下一状态的所有子集,并标记为-1。对于book[i]>0的值,表示i不是其他数的自己,ans+=book[i]
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll s,book[1<<20+1],maxn=-1;
int main(){
ll m,n;
scanf("%lld%lld",&n,&m);
for(ll i=0;i<n;i++){
scanf("%lld",&s);
book[s]++;
maxn=max(s,maxn);
}
ll ans=0;
for(ll i=maxn;i>=0;i--){
if(book[i]){
if(book[i]>0)
ans+=book[i];
for(ll j=m-1;j>=0;j--){
if(i&(1<<j)){
book[i^(1<<j)]=-1;
}
}
}
}
printf("%lld\n",ans);
return 0;
}