此题暴力为何不能过?
查看原帖
此题暴力为何不能过?
162196
伟大的王夫子楼主2020/5/2 14:23

nn只有3030,O(2n)O(2^n)应该能行吧。然而T了

#include <bits/stdc++.h>
using namespace std;
int n, a[50];
long long ans;
void dfs(int dep, long long sum) {
	if (dep == n + 1) {
		ans += sum;
		return;
	}
	dfs(dep + 1, sum + a[dep]);
	dfs(dep + 1, sum);
}
int main() {
	n = 1;
	while (cin >> a[n]) ++n;
	--n;
//	cout << n << endl;
	dfs(1, 0);
	cout << ans;
}
2020/5/2 14:23
加载中...