From 7300ab1af3640a7762a54489dab32dbb3b35f66f Mon Sep 17 00:00:00 2001 From: myoshizumi Date: Wed, 9 Jul 2025 17:04:48 +0900 Subject: [PATCH 1/2] =?UTF-8?q?atcoder=20B14=20-=20Another=20Subset=20Sum?= =?UTF-8?q?=20=E5=8D=8A=E5=88=86=E5=85=A8=E5=88=97=E6=8C=99=EF=BC=88Meet-i?= =?UTF-8?q?n-the-middle=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Meet-in-the-middle/atcoder/B14/B14.go | 123 +++++++ .../Meet-in-the-middle/atcoder/B14/B14.js | 90 +++++ .../Meet-in-the-middle/atcoder/B14/B14.php | 110 +++++++ .../Meet-in-the-middle/atcoder/B14/B14.py | 93 ++++++ .../Meet-in-the-middle/atcoder/B14/B14.ts | 100 ++++++ .../Meet-in-the-middle/atcoder/B14/README.md | 308 ++++++++++++++++++ 6 files changed, 824 insertions(+) create mode 100644 Algorithm/Meet-in-the-middle/atcoder/B14/B14.go create mode 100644 Algorithm/Meet-in-the-middle/atcoder/B14/B14.js create mode 100644 Algorithm/Meet-in-the-middle/atcoder/B14/B14.php create mode 100644 Algorithm/Meet-in-the-middle/atcoder/B14/B14.py create mode 100644 Algorithm/Meet-in-the-middle/atcoder/B14/B14.ts create mode 100644 Algorithm/Meet-in-the-middle/atcoder/B14/README.md diff --git a/Algorithm/Meet-in-the-middle/atcoder/B14/B14.go b/Algorithm/Meet-in-the-middle/atcoder/B14/B14.go new file mode 100644 index 00000000..b6caca53 --- /dev/null +++ b/Algorithm/Meet-in-the-middle/atcoder/B14/B14.go @@ -0,0 +1,123 @@ +// 以下に、**Go (go 1.20.6)** による解答を提示します。 +// 制約に対応するために、**半分全列挙(Meet-in-the-middle)** を用いた効率的な実装です。 +// 型を明示し、処理時間・メモリ使用量を考慮しています。 + +// ## ✅ 解法概要 + +// * 与えられた整数列を **前半・後半に分割** +// * 各部分の **部分集合和を全列挙**(最大 2^15 = 32,768 通り) +// * 一方の部分和リストに対して **二分探索**を使用し、合計が `K` になる組を探索 + +// ## ✅ Go実装(Go 1.20.6) + +package main + +import ( + "bufio" + "fmt" + "os" + "sort" + "strconv" + "strings" +) + +// getSubsetSums returns all possible subset sums of arr. +func getSubsetSums(arr []int) []int { + n := len(arr) + res := make([]int, 0, 1<> 1; + if (arr[mid] === target) return true; + if (arr[mid] < target) left = mid + 1; + else right = mid - 1; + } + return false; +} + +function main() { + const input = fs.readFileSync('/dev/stdin', 'utf8').trim().split(/\s+/).map(Number); + const N = input[0]; + const K = input[1]; + const A = input.slice(2); + + const mid = Math.floor(N / 2); + const left = A.slice(0, mid); + const right = A.slice(mid); + + const leftSums = getSubsetSums(left); + const rightSums = getSubsetSums(right); + rightSums.sort((a, b) => a - b); + + for (const x of leftSums) { + const remain = K - x; + if (binarySearch(rightSums, remain)) { + console.log("Yes"); + return; + } + } + + console.log("No"); +} + +main(); + +// ### ✅ 実行例 + +// 入力: + +// ``` +// 6 30 +// 5 1 18 7 2 9 +// ``` + +// 出力: + +// ``` +// Yes +// ``` + +// --- + +// ### ✅ 時間・空間計算量 + +// * 時間: `O(2^(N/2) * log(2^(N/2)))` ≒ `2^15 * 15` → 高速(制限内) +// * 空間: `O(2^(N/2))` → 最大 32,768 個(メモリ制限内) + diff --git a/Algorithm/Meet-in-the-middle/atcoder/B14/B14.php b/Algorithm/Meet-in-the-middle/atcoder/B14/B14.php new file mode 100644 index 00000000..25dd8ca5 --- /dev/null +++ b/Algorithm/Meet-in-the-middle/atcoder/B14/B14.php @@ -0,0 +1,110 @@ +