Anya and Cubes CodeForces - 525E (双端搜索)

大意: 给定$n$元素序列$a$, 可以任选不超过$k$个$a_i$变换为$a_i!$, 求变换后任选若干元素和为S的方案数.

分成两块暴搜, 复杂度$O(3^{\frac{n}{2}})$

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <unordered_map>
#define REP(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll; int n, k, a[30];
ll S, fac[30];
unordered_map<ll,int> x[30], y[30]; void dfs(int d, int mx, int kk, ll num, unordered_map<ll,int> *x) {
if (num>S||kk>k) return;
if (d==mx) return ++x[kk][num],void();
dfs(d+1,mx,kk,num+a[d],x);
if (a[d]<20) dfs(d+1,mx,kk+1,num+fac[a[d]],x);
dfs(d+1,mx,kk,num,x);
} int main() {
fac[0] = 1;
REP(i,1,19) fac[i]=fac[i-1]*i;
cin>>n>>k>>S;
REP(i,0,n-1) cin>>a[i];
dfs(0,n/2,0,0,x), dfs(n/2,n,0,0,y);
ll ans = 0;
REP(i,0,k) for (auto &&it:x[i]) {
auto &&t = it.second;
auto &&num = S-it.first;
REP(j,0,k-i) ans += t*y[j][num];
}
printf("%lld\n", ans);
}
上一篇:IHttpHandler处理请求api


下一篇:Uber上海公司被司机打上门