【CF】E. Anya and Cubes(双向DFS)
来源:互联网 发布:网络性与爱 第一季 编辑:程序博客网 时间:2024/05/13 21:44
根据题意的话每次递归分3种情况
一共最多25个数,时间复杂度为3^25,太大了
我们可以分2次求解第一次求一半的结果,也就是25/2 = 12,记录结果
之后利用剩余的一半求结果 s-结果 = 之前记录过的结果 就可以
时间复杂度降低为 3 ^ (n/2+1)
题目链接:http://codeforces.com/contest/525/problem/E
#include<set>#include<map>#include<cstdio>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 33;set<LL>vis;map<LL,int>cnt[maxn];int n,kk,a[maxn];LL ans = 0;LL vk[20],s;void dfs1(int now,int v,int k,LL value){ if(value > s) return; if(now == v){ vis.insert(value); cnt[k][value]++; return; } int e = a[now]; if(e <= 18 && k < kk) dfs1(now + 1,v,k + 1,value + vk[e]); dfs1(now + 1,v,k,value + (LL)e); dfs1(now + 1,v,k,value); return;}void dfs2(int now,int v,int k,LL value){ if(value > s) return; if(now == v){ LL z = s - value; if(vis.count(z)){ int _k = kk - k; for(int i = 0; i <= _k; i++) ans += cnt[i][z]; } return; } int e = a[now]; if(e <= 18) dfs2(now + 1,v,k + 1,value + vk[e]); dfs2(now + 1,v,k,value + (LL)e); dfs2(now + 1,v,k,value);}int main(){ vk[0] = 1; for(int i = 1; i <= 18; i++) vk[i] = vk[i - 1] * i; scanf("%d%d%I64d",&n,&kk,&s); for(int i = 0; i < n; i++) scanf("%d",&a[i]); dfs1(0,n/2,0,0L); dfs2(n/2,n,0,0L); printf("%I64d\n",ans); return 0;}
0 0
- 【CF】E. Anya and Cubes(双向DFS)
- CF 297 div2 E. Anya and Cubes (hash+dfs)
- Codeforces Round #297 (Div. 2) E. Anya and Cubes (双向DFS)
- E. Anya and Cubes (CF #297 (Div. 2) 折半搜索)
- E. Anya and Cubes (CF #297 (Div. 2) 折半搜索)
- #297 (div.2) E. Anya and Cubes
- Anya and Cubes - CodeForces 525 E dp
- codeforces 525E. Anya and Cubes
- CODEFORCES 525E Anya and Cubes
- Codeforces Round #297 (Div. 2) 525E Anya and Cubes(dfs)
- Codeforces 525E Anya and Cubes【折半枚举法+剪枝Dfs】好题~
- codeforces 525E E. Anya and Cubes( dp+折中搜索)
- 中途相与法(Codeforces Round #297 (Div. 2)E - Anya and Cubes )
- codeforces 525E Anya and Cubes(暴力,meet-in-the-middle)
- 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
- html+css学习总结
- Python类中的私有变量
- Linux添加/删除用户和用户组
- 最简单的基于librtmp的示例:发布H.264(H.264通过RTMP发布)
- Maven常用命令总结
- 【CF】E. Anya and Cubes(双向DFS)
- 安装mongodb
- ExpandableListView吐槽系列(一) -> 设置自定义groupIndicator
- 开锁魔法II 哈尔滨理工大学第五届ACM程序设计竞赛
- 策略模式
- High weight hardwood film faced plywood
- IOS之CoreData初识
- 远程链接mysql :1130
- Combination Sum II--LeetCode