Codeforces Round #297 (Div. 2)E. Anya and Cubes(折半搜索)
来源:互联网 发布:怎么查询域名注册商 编辑:程序博客网 时间:2024/05/18 01:07
分析:
每个物品有三种决策,直接暴力枚举O(3^n)会超时,但是我们发现O(3^(n/2))不会超时,于是可以采用中途相遇法。
先暴力枚举一半的决策,把对应的值、使用次数和方案数存到map里,然后再枚举另一半,在map中查找。
复杂度O(3^(n/2)log(3^(n/2)))=O(3^(n/2)n)。
#include<bits/stdc++.h>using namespace std;typedef long long ll;ll s, a[30], fact[20], ans;map<ll, int> mp[30];int n, k;void dfs1(ll sum, int pos, int cnt){ if(pos == n / 2 + 1) { if(mp[cnt].count(sum)) mp[cnt][sum]++; else mp[cnt][sum] = 1; return ; } dfs1(sum, pos+1, cnt); if(a[pos] <= s - sum) dfs1(sum + a[pos], pos+1, cnt); if(cnt < k && a[pos] <= 19 && fact[a[pos]] <= s - sum) dfs1(sum+fact[a[pos]], pos+1, cnt+1);}void dfs2(ll sum, int pos, int cnt){ if(pos == n + 1) { for(int i = 0; i + cnt <= k; i++) if(mp[i].count(s-sum)) ans += mp[i][s-sum]; return ; } dfs2(sum, pos+1, cnt); if(a[pos] <= s - sum) dfs2(sum + a[pos], pos+1, cnt); if(cnt < k && a[pos] <= 19 && fact[a[pos]] <= s - sum) dfs2(sum+fact[a[pos]], pos+1, cnt+1);}int main(){ fact[0] = 1; for(ll i = 1; i <= 19; i++) fact[i] = fact[i-1] * i; scanf("%d%d%I64d", &n, &k, &s); for(int i = 1; i <= n; i++) scanf("%I64d", &a[i]); dfs1(0, 1, 0); dfs2(0, n/2+1, 0); printf("%I64d\n", ans); return 0;}
阅读全文
0 0
- Codeforces Round #297 (Div. 2)E. Anya and Cubes 折半搜索
- Codeforces Round #297 (Div. 2)E. Anya and Cubes(折半搜索)
- E. Anya and Cubes (CF #297 (Div. 2) 折半搜索)
- E. Anya and Cubes (CF #297 (Div. 2) 折半搜索)
- Codeforces Round #297 (Div. 2)---E. Anya and Cubes
- Codeforces Round #297 (Div. 2) E - Anya and Cubes
- Codeforces Round #297 (Div. 2) E. Anya and Cubes
- Codeforces Round #297 (Div. 2) E Anya and Cubes
- Codeforces Round #297 (Div. 2) E Anya and Cubes
- Codeforces Round #297 (Div. 2)E. Anya and Cubes
- 【Codeforces Round #297 (Div. 2)】Codeforces 525E Anya and Cubes
- 中途相与法(Codeforces Round #297 (Div. 2)E - Anya and Cubes )
- Codeforces Round #297 (Div. 2) E题. Anya and Cubes (中途相遇法)
- Codeforces Round #297 (Div. 2) E. Anya and Cubes (双向DFS)
- Codeforces Round #297 (Div. 2) 525E Anya and Cubes(dfs)
- #297 (div.2) E. Anya and Cubes
- codeforces 525E E. Anya and Cubes( dp+折中搜索)
- Anya and Cubes - CodeForces 525 E dp
- JDBC操作数据库的基本步骤
- mysql的安装及一些初级操作
- 设置mysql允许外部IP访问
- Dubbo详细介绍与安装使用过程
- Unity3D-调用IOS原生分享的实现
- Codeforces Round #297 (Div. 2)E. Anya and Cubes(折半搜索)
- HPE Mobile Center 试用感受功能简单,不推荐
- Maven 之 SNATSHOT 和release
- 数组中的主元素
- mysql导入导出sql文件
- shell 变量
- 机器学习之树回归
- 给自己要开始认真写博客的一些忠告
- ProgrammingError: 错误: 字段 res_partner.instructor 不存在