首页 > 其他 > 详细

acdream 1726 A Math game

时间:2015-05-10 14:22:19      阅读:499      评论:0      收藏:0      [点我收藏+]


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 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.

Input

There 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.

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

先储存所有可能的和,然后从大到小搜索。。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#include <cstdlib>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#define MAXN 1000000001
typedef long 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

原文:http://blog.csdn.net/sky_miange/article/details/45619927

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!