【题解】[CSP-S 2020] 动物园

题目链接

题目链接

(题意略)

题解

由于题目保证所有饲料编号互不相同,所以把所有动物或起来,再或上那些不需要饲料的位。求 \(2^{\text{popcnt}}-n\) 即可。

注意 1ull<<64 是未定义行为(CSP-S 2019 D1T1 可能出同样的锅)。

注意 \(n=0\) 时答案可能是 \(2^{64}\),需要用计算器算好。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
ull getint(){
	ull ans=0;
	char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	while(c>='0'&&c<='9'){
		ans=ans*10+c-'0';
		c=getchar();
	}
	return ans;
}
const int N=1e6+10;

int main(){
	int n=getint(),m=getint(),c=getint(),k=getint();
	ull all=0;
	ull lim=0;
	for(int i=1;i<=n;i++){
		ull x=getint();
		all|=x;
	}
	for(int i=1;i<=m;i++){
		int p=getint(),q=getint();
		lim|=(1ull<<p);
/****************** Wrong code *****************
		lim|=(1<<p);
************************************************/
	}
    if(k==64)all|=(-1ull)^lim;
	else all|=((1ull<<k)-1)^lim;
	int popc=0;
	for(;all;all^=(all&-all))++popc;
    if(popc==64&&n)cout<<0ull-(unsigned long long)n<<endl;
    else if(popc==64)cout<<"18446744073709551616"<<endl;
	else cout<<(1ull<<popc)-n<<endl;
/****************** Wrong code *****************
	all|=((1ull<<k)-1)^lim;
	int popc=0;
	for(;all;all^=(all&-all))++popc;
	cout<<(1ull<<popc)-n<<endl;
************************************************/
}
posted @ 2020-11-09 11:08  破壁人五号  阅读(434)  评论(0编辑  收藏  举报