MCPcopy Create free account
hub / github.com/E869120/math-algorithm-book / main

Function main

codes/cpp/Code_2_06_1.cpp:6–22  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4long long N, S, A[61];
5
6int main() {
7 cin >> N >> S;
8 for (int i = 1; i <= N; i++) cin >> A[i];
9
10 // 全パターンを探索:(1LL << N) は 2 の N 乗
11 for (long long i = 0; i < (1LL << N); i++) {
12 long long sum = 0;
13 for (int j = 1; j <= N; j++) {
14 // (i & (1LL << (j-1))) != 0LL の場合、i の 2 進法表記の下から j 桁目が 1
15 // (1LL << (j-1)) は C++ では「2 の j-1 乗」を意味します
16 if ((i & (1LL << (j-1))) != 0LL) sum += A[j];
17 }
18 if (sum == S) { cout << "Yes" << endl; return 0; }
19 }
20 cout << "No" << endl;
21 return 0;
22}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected