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| + --- 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