Acdream 1726 A Math game (partial and problematic DFS pruning), acdreamdfs
A Math gameTime Limit: 2000/1000 MS (Java/Others)
Memory Limit: 256000/128000 KB (Java/Others) Problem DescriptionRecently, Losanto find an interesting Math game. The rule is simple: Tell you a number
H, And you can choose some numbers from a set {a [1], a [2],..., a [n]}. if the sum of the number you choose is
H, Then you win. Losanto just want to know whether he can win the game. InputThere are several cases.
In each case, there are two numbers in the first line
N(The size of the set) and
H. The second line has n numbers {a [1], a [2],..., a [n]}.
0 <n <= 40, 0 <= H <10 ^ 9, 0 <= a [I] <10 ^ 9, All the numbers are
Integers. OutputIf Losanto cocould win the game, output "Yes" in a line. Else output "No" in a line. Sample Input
10 872 3 4 5 7 9 10 11 12 1310 382 3 4 5 7 9 10 11 12 13
Sample Output
NoYes
Source Program Design Competition of the ninth Beijing University of Chemical Technology
Question link: http://acdream.info/problem? Pid = 1, 1726
Part and question
Question analysis: the positive solution is binary + DFS. My practice is to use the prefix and pruning of DFS. After 4 ms, it is estimated that it is still data water,
# Include <cstdio> # include <cstring> # define ll long longll 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; // if the current number is added with the remaining number and less than h or the current number is greater than h, return // if (sum [n]-sum [pos-1] + num