Acdream 1726 A Math game (partial and problematic DFS pruning), acdreamdfs

Source: Internet
Author: User

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 




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.