CSP-S2020 Problem 2 (Zoo) Problem Solution

Problem-solving ideas
First find out all the binary digits covered by at least one animal number.
If a binary bit corresponds to a feed, but this bit is not covered by an existing animal, the newly added animal number cannot cover this bit, otherwise a new feed type will appear.
If the last x bits can be overwritten, the final answer is 2^x−n.
Special attention : need to consider the reference code of some boundary conditions

#include <bits/stdc++.h>
using namespace std;
const string str = "18446744073709551616"; const int N = 65;
bool vis[N], flag[N];
int main() {
    
    
	int n, m, c, k; cin >> n >> m >> c >> k;
 	for (int i = 1; i <= n; ++i) {
    
    
    	unsigned long long x; cin >> x;
    	for (int j = k - 1; j >= 0; --j) {
    
    
			vis[j] |= (x >> j) & 1;
		} 
  	}
  	for (int i = 1; i <= m; ++i) {
    
    
    	int p, q; cin >> p >> q;
    	if (!vis[p]) {
    
    
			flag[p] = true;
		}
  	}
  	int sum = 0;
  	for (int i = 0; i < k; ++i) {
    
    
  		if (flag[i]) {
    
    
  			++sum;
		}
	} 
  	if (k - sum == 64) {
    
    
    	if (n) {
    
     
      		cout << (unsigned long long) -n << '\n';
      	} else {
    
     
      		cout << str << '\n';
      	}
  	} else {
    
     
  	  	cout << (1ull << (k - sum)) - n << '\n';
  	}
  	return 0;
}

Guess you like

Origin blog.csdn.net/yueyuedog/article/details/112633282