AT_abc200_d [ABC200D] Happy Birthday! 2 的题解

AT_abc200_d [ABC200D] Happy Birthday! 2 的题解

洛谷传送门

AT传送门

思路

我们可以证明,只要 N ≥ 8 N\ge 8 N8,那么就一定有解。

证明如下:

  • 8 8 8 个元素能组成的子序列有 2 8 − 1 = 255 2^8-1=255 281=255 种。(每个元素可以选或不选,去掉全不选的情况)

  • 根据抽屉原理,我们将这 255 255 255 种子序列按照他们除以 200 200 200 的余数分别放入抽屉中,则至少有两个子序列在一个抽屉中,即必定有合法的 A A A B B B

N < 8 N<8 N<8 时,我们暴力枚举所有可能;

N ≥ 8 N \ge 8 N8 时,我们暴力枚举其中任意 8 8 8 个元素组成的所有可能即可找到解。

代码

#include <bits/stdc++.h>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
    
    
	inline int read() {
    
    
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
    
    
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
    
    
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
inline void print(const vector<int>& v) {
    
    
	printf("%llu", v.size());
	for(int x: v) {
    
    
		printf(" %d", x + 1);
	}
	putchar('\n');
}
using namespace fastIO;
int a[15];
vector<int> bkt[200];
int main() {
    
    
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	int n;
	n = read();
	if(n > 8) {
    
    
		n = 8;	
	}
	for(int i = 0; i < n; i ++) {
    
    
		scanf("%d", a + i);
	}
	int lim = 1 << n;
	for(int st = 0; st < lim; st ++) {
    
    
		int s = 0;
		vector<int> pos;
		for(int i = 0; i < n; i ++) {
    
    
			if(st >> i & 1) {
    
    
				s = (s + a[i]) % 200;
				pos.push_back(i);
			}
		}				
		if(!bkt[s].empty()) {
    
    
			puts("Yes");
			print(bkt[s]);
			print(pos);
			return 0;
		}
		else bkt[s] = pos;
	}
	puts("No");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ZH_qaq/article/details/130456455
今日推荐