UKE?
查看原帖
UKE?
1087986
didi2012楼主2024/1/18 20:07
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100;
int n, m, t;
int b[maxn];
ll S, ans;
ll a[maxn];
map<pair<int, ll>, int>f;

ll fact(ll x) {
	ll ret;
	for (ret = 1; x; ret *= x--)
		if ((S / ret) < x)
			return -1;
	return ret;
}

void che1() {
	int cnt = 0;
	ll sum = 0;
	for (int i = 1; i <= m; ++i)
		if (b[i] == 1)
			sum += a[i];
		else if (b[i] == 2) {
			ll s = fact(a[i]);
			if ((s == -1) || (++cnt > t) || ((s + sum) > S))
				return;
			sum += s;
		}
	++f[ {cnt, sum}];
	++f[ {-1, sum}];
}

void dfs(int u) {
	if (u > m)
		che1();
	else {
		for (int i = 0; i <= 2; ++i) {
			b[u] = i;
			dfs(u + 1);
		}
	}
}
map<pair<int, ll>, ll>g;
\\chichen!
ll qry(int x, ll s) {
	if (g.count({x, s}))
		return g[ {x, s}];
	return g[ {x, s}] = f[ {x, s}] + (x ? qry(x - 1, s) : 0);
}

void che2() {
	int cnt = 0;
	ll sum = 0;
	for (int i = m + 1; i <= n; ++i)
		if (b[i] == 1)
			sum += a[i];
		else if (b[i] == 2) {
			ll s = fact(a[i]);
			if ((s == -1) || (++cnt > t) || ((s + sum) > S))
				return;
			sum += s;
		}
	if (f.count({-1, S - sum}))
		ans += qry(t - cnt, S - sum);
}

void dfs2(int u) {
	if (u > n)
		che2();
	else {
		for (int i = 0; i <= 2; ++i) {
			b[u] = i;
			dfs2(u + 1);
		}
	}
}

int main() {
	cin >> n >> t >> S;
	for (int i = 1; i <= n; ++i)
		scanf("%lld", a + i);
	m = n >> 1;
	dfs(1);
	dfs2(m + 1);
	cout << ans << endl;
	return 0;
}
2024/1/18 20:07
加载中...