17ms 的痛苦,求卡常 QAQ
查看原帖
17ms 的痛苦,求卡常 QAQ
560516
喵仔牛奶楼主2023/3/11 15:43

求卡常 QAQ,https://www.luogu.com.cn/record/104356129

#include <bits/stdc++.h>
using namespace std;
namespace Milkcat {
	const int N = 105;
	int n, m, sum, tot, a[N], next[N];
	bool vis[N];
	void dfs(int u, int len, int qwq, int last) {
		if (len == qwq) dfs(u + 1, 0, qwq, 2);
		if (u * qwq == sum) printf("%d\n", qwq), exit(0);
		for (int i = last; i <= n; i ++)
			if (!vis[i] && len + a[i] <= qwq) {
				vis[i] = 1, dfs(u, len + a[i], qwq, i), vis[i] = 0;
				if (qwq - len == a[i] || !len) break;
				i = next[i] - 1;
			}
	}
	int main() {
		scanf("%d", &n);
		for (int i = 1; i <= n; i ++)
			scanf("%d", &a[i]), sum += a[i];
		sort(a + 1, a + 1 + n, greater<int>());
		for (int i = n; i >= 1; i --) {
			if (a[i] != a[i + 1]) next[i] = i + 1;
			else next[i] = next[i + 1];
		}
		for (int i = a[1]; i <= sum; i ++)
			if (sum % i == 0) vis[1] = 1, dfs(0, a[1], i, 2);
		puts("STR AKs IOI");
		return 0;
	}
}
int main() {
	return Milkcat::main();
}

2023/3/11 15:43
加载中...