Acdream 1726 A Math Game

Source: Internet
Author: User

A Math GameTime limit:2000/1000ms (java/others)Memory limit:256000/128000kb (java/others)Submitstatistic Next Problemproblem 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 is several cases.
In each case, there is 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 is integers.Outputif Losanto could 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
First store all possible and then search from big to small:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace STD, #include <cstdlib> #include <map> #include <cmath> #include <queue> #include <vector>        #define MAXN 1000000001typedef Long ll;int flag;int a[45];int sum[45];int h;void dfs (int n,int zsum) {if (flag)    Return    if (Zsum>sum[n]) return;        if (Sum[n]==zsum | | zsum==0) {flag=1;    Return              } for (int i=n; i>=1; i--) if (Zsum>=a[i]) {//cout<<zsum<<endl;          DFS (I-1,zsum-a[i]);    }}int Main () {int n,i,j;        while (cin>>n>>h) {flag=0;        sum[0]=0;            for (I=1; i<=n; i++) {cin>>a[i];        Sum[i]=sum[i-1]+a[i];        } dfs (N,H);        if (flag) cout<< "Yes" <<endl;    else cout<< "No" <<endl; } return 0;}

Acdream 1726 A Math game

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.