From 0cfb5e90ceff5da124699874587791c2237b8cce Mon Sep 17 00:00:00 2001 From: Moo Date: Fri, 11 Apr 2025 16:08:36 +0900 Subject: [PATCH 1/2] =?UTF-8?q?chore:=20README.md=20=EC=97=85=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MuchanKim/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/MuchanKim/README.md b/MuchanKim/README.md index 4fc7e8a..0c15da2 100644 --- a/MuchanKim/README.md +++ b/MuchanKim/README.md @@ -3,4 +3,6 @@ | 차시 | 날짜 | 문제유형 | 링크 | 풀이 | |:----:|:---------:|:----:|:-----:|:----:| | 1차시 | 2025.03.29 | 구현 | [개미](https://www.acmicpc.net/problem/3048)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/3| +| 2차시 | 2025.04.11 | 에라토스테네스의채 | [소-난다!](https://www.acmicpc.net/problem/19699)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/10| + --- From 4fb164bd8e9bc971f429813ef07f9de00e7356cb Mon Sep 17 00:00:00 2001 From: Moo Date: Sat, 12 Apr 2025 00:06:58 +0900 Subject: [PATCH 2/2] =?UTF-8?q?2-MuchanKim=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...354\206\214\353\202\234\353\213\244.swift" | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 "MuchanKim/\354\227\220\353\235\274\355\206\240\354\212\244\355\205\214\353\204\244\354\212\244\354\235\230\354\262\264/\354\206\214\353\202\234\353\213\244.swift" diff --git "a/MuchanKim/\354\227\220\353\235\274\355\206\240\354\212\244\355\205\214\353\204\244\354\212\244\354\235\230\354\262\264/\354\206\214\353\202\234\353\213\244.swift" "b/MuchanKim/\354\227\220\353\235\274\355\206\240\354\212\244\355\205\214\353\204\244\354\212\244\354\235\230\354\262\264/\354\206\214\353\202\234\353\213\244.swift" new file mode 100644 index 0000000..b543e7b --- /dev/null +++ "b/MuchanKim/\354\227\220\353\235\274\355\206\240\354\212\244\355\205\214\353\204\244\354\212\244\354\235\230\354\262\264/\354\206\214\353\202\234\353\213\244.swift" @@ -0,0 +1,63 @@ +import Foundation + +// 에라토스테네스의 체 +func getPrimes(_ maxNum: Int) -> [Bool] { + var isPrime = Array(repeating: true, count: maxNum + 1) + isPrime[0] = false + isPrime[1] = false + + for i in 2...Int(Double(maxNum).squareRoot()) { + if isPrime[i] { + for j in stride(from: i * i, through: maxNum, by: i) { + isPrime[j] = false + } + } + } + + return isPrime +} + +func solution(_ n: Int, _ m: Int, _ cowsWeight: [Int]) -> String { + if m > n || n <= 0 || m <= 0 { + return "-1" + } + + // 모든 무게 합의 최대값 계산 + var maxPossibleSum = 0 + for weight in cowsWeight { + maxPossibleSum += weight + } + + let isPrime = getPrimes(maxPossibleSum) + + var result: Set = [] + // 비트마스크를 이용한 조합 생성 + let totalCombinations = 1 << n + + for mask in 0..= 2 && isPrime[sum] { + result.insert(sum) + } + } + + let sortedResult = Array(result).sorted() + return sortedResult.isEmpty ? "-1" : sortedResult.map { String($0) }.joined(separator: " ") +} + +let input = readLine()!.split(separator: " ").map { Int($0)! } +let cowWeightArray = readLine()!.split(separator: " ").map { Int($0)! } + +let res = solution(input[0], input[1], cowWeightArray) +print(res) \ No newline at end of file