-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexhaustive_search.cpp
More file actions
42 lines (38 loc) · 1.13 KB
/
exhaustive_search.cpp
File metadata and controls
42 lines (38 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
// 再帰・分割統治で与えられた整数リストから
// 与えられた整数mが作成できるかを判定する
// 整数リストの各要素に対して、選択する or しない
// の組み合わせを全探索する
bool CanCreateNumber(int i, int m, int n, vector<int> vecNum) {
bool res = true;
if (m==0) return true;
if (i>=n) return false;
// mから与えられた整数リストの各選択要素を引いていき
// 0になった→整数リストでmが作れる
// iがm以上になった→作れない
res = CanCreateNumber(i+1, m, n, vecNum) || CanCreateNumber(i+1, m-vecNum[i], n, vecNum);
return res;
}
int main() {
int n = 0;
int m = 0;
int numTmp = 0;
vector<int> vecA;
scanf("%d",&n);
for (int i = 0; i < n; i++)
{
scanf("%d",&numTmp);
vecA.push_back(numTmp);
}
scanf("%d",&m);
string strRes;
for (int j = 0; j < m; j++)
{
scanf("%d",&numTmp);
strRes = (CanCreateNumber(0, numTmp, n, vecA)) ? "yes" : "no";
printf("%s\n",strRes.c_str());
}
return 0;
}