Given integers a1, a2, ......, an, several judges whether to choose the number, and to make them exactly to be k.
limitation factor
1<=n<=20
-10^n<=ai<=10^8
-10^8<=k<=10^8
Input:
n=4
a={1,2,4,7}
k=13
Output:
Yes (13 = 2 + 4 + 7)
int a[MAX_N];
int n, k;
// 已经从i项得到了和sum, 然后对于i项之后的进行分支
bool dfs(int i, int sum)
{
if(i == n) return sum == k;
// 不加上a[i]的情况
if(dfs(i + 1, sum)) return true;
// 加上a[i]的情况
if(dfs(i + 1, sum + a[i]) return true;
return false;
}
void solve()
{
if(dfs(0, 0)) printf("Yes\n");
else printf("No\n");
}