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

Posted uid001

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
}

 

以上是关于Anya and Cubes CodeForces - 525E (双端搜索)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces508 C. Anya and Ghosts(贪心)

CodeForces - 764B Timofey and cubes(模拟)

codeforces679B Bear and Tower of Cubes(思路)

Codeforces 679B - Bear and Tower of Cubes

CodeForces 887B Cubes for Masha (暴力 || DFS)

Codeforces Round #444 (Div. 2) B. Cubes for Masha 暴力枚举