Skip to content

Commit

Permalink
백준 10986번 나머지 합 java solution
Browse files Browse the repository at this point in the history
  • Loading branch information
mong3125 committed Feb 24, 2024
1 parent b1d0815 commit 3524476
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions mong3125/수학/BOJ10986.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class BOJ10986 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());

st = new StringTokenizer(br.readLine());
int[] remainders = new int[N + 1]; // 부분 합을 M으로 나눈 리스트
int[] remainders_count = new int[M]; // 나머지 개수
for (int i = 1; i <= N; i++) {
remainders[i] = (remainders[i - 1] + Integer.parseInt(st.nextToken())) % M;
remainders_count[remainders[i]] += 1;
}

long count = 0;
for (int i = 0; i < M; i++) {
count += (long) remainders_count[i] * (remainders_count[i] - 1) / 2; // 조합
}

count += remainders_count[0]; // 나머지가 0일때 (i = j 일때)

System.out.println(count);
}
}

0 comments on commit 3524476

Please sign in to comment.