• 문제 링크
17173번: 배수들의 합
신원이는 백준에서 배수에 관한 문제를 풀다가 감명을 받아 새로운 문제를 만들어보았다. 자연수 N과 M개의 자연수 Ki가 주어진다. Ki중 적어도 하나의 배수이면서 1 이상 N 이하인 수의 합을 구하
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), m = read();
boolean[] arr = new boolean[n + 1];
int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0, k = read(); j <= n; j++) {
if (!arr[j] && j % k == 0) {
arr[j] = true;
sum += j;
}
}
}
bw.write(String.valueOf(sum));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 15323 ZigZag - Data Structure / Java (0) | 2023.12.31 |
---|---|
[백준] 29701 모스 부호 - Data Structure / Java (0) | 2023.12.30 |
[백준] 2399 거리의 합 - Brute Force / Java (0) | 2023.12.28 |
[백준] 13410 거꾸로 구구단 - Brute Force / Java (0) | 2023.12.27 |
[백준] 1773 폭죽쇼 - Brute Force / Java (0) | 2023.12.26 |
댓글