ACdream 1726 A Math game

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

dfs即可

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1005;
int n, m, a[maxn], sum[maxn];
int f;

void dfs(int x, int y)
{
	if (y == n) f = 1;
	if (y + sum[m] - sum[x - 1] < n || x > m || f || y > n) return;
	dfs(x + 1, y + a[x]);
	dfs(x + 1, y);
}

int main()
{
	while (scanf("%d%d", &m, &n) != EOF)
	{
		sum[0] = 0;
		for (int i = 1; i <= m; i++) scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
		f = 0;
		dfs(1, 0);
		if (!f) printf("No\n"); else printf("Yes\n");
	}
	return 0;
}


你可能感兴趣的:(ACdream)