acdream 1726 A Math game (部分和问题 DFS剪枝)
2015-05-12 00:04
387 查看
A Math game
Time Limit: 2000/1000MS (Java/Others)Memory Limit: 256000/128000KB (Java/Others)
Problem Description
Recently, Losanto find an interesting Math game. The rule is simple: Tell you a numberH, and you can choose some numbers from a set {a[1],a[2],......,a}.If the sum of the number you choose isH,
then you win. Losanto just want to know whether he can win the game.
Input
There are several cases.In each case, there are two numbers in the first line n (the size of the set) andH. The second line has n numbers {a[1],a[2],......,a
}.0<n<=40, 0<=H<10^9, 0<=a[i]<10^9,All
the numbers areintegers.
Output
If Losanto could win the game, output "Yes" in a line. Else output "No" in a line.Sample Input
10 87 2 3 4 5 7 9 10 11 12 13 10 38 2 3 4 5 7 9 10 11 12 13
Sample Output
No Yes
Source
第九届北京化工大学程序设计竞赛题目链接:http://acdream.info/problem?pid=1726
题目大意:部分和问题
题目分析:正解是二分+DFS,我的做法是DFS用前缀和剪枝,4ms过,估计还是数据水了,
#include <cstdio> #include <cstring> #define ll long long ll a[45], h, sum[45]; int n; bool flag; void DFS(ll num, int pos) { if(num == h) { flag = true; return; } if(flag || pos == n + 1) return; //若当前数字加剩下的数字和小于h或者当前数字大于h则返回 //不加妥T if(sum - sum[pos - 1] + num < h || num > h) return; DFS(num + a[pos], pos + 1); DFS(num, pos + 1); return; } int main() { while(scanf("%d %lld", &n, &h) != EOF) { memset(sum, 0, sizeof(sum)); flag = false; for(int i = 1; i <= n; i++) { scanf("%lld", &a[i]); sum[i] = sum[i - 1] + a[i]; } DFS(0, 1); if(flag) printf("Yes\n"); else printf("No\n"); } }
相关文章推荐
- ACdream 1726 A Math game (dfs+二分)
- ACdream-1726-A Math game(dfs+二分)
- 部分和问题 【DFS】or【贪心】【DFS+ 剪枝】
- HDU 2553 N皇后问题( DFS + 剪枝 )
- 1058 部分和问题【dfs+记录】
- 部分和问题(南阳oj1058)(DFS)
- DFS 部分和问题
- NYoj1282部分和问题(dfs入门)
- 部分和问题-dfs穷举
- NYOJ 1058部分和问题(dfs+回溯)
- nyoj 部分和问题(深度优先搜索DFS)
- ACDREAM 05E 哗啦啦族的01背包问题(DFS专场)
- 八皇后问题 DFS,回溯剪枝 //http://poj.grids.cn/practice/2698
- 深度优先搜索(DFS)——部分和问题
- 砝码(01背包问题的DFS剪枝)
- NYOJ 1058 部分和问题(dfs)
- nyoj 1058部分和问题(DFS)
- nyoj 部分和问题(深度优先搜索DFS)
- ACdream 1431——Sum vs Product——————【dfs+剪枝】
- nyoj 1058 部分和问题 【简单DFS】