[Java] Do it! 코딩테스트 - 나머지 합 구하기(8.1)

EunBi Na·2022년 8월 1일
0

public class 나머지 합 {
	public static void main(String[] args) {
    	Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();
        long[] S = new long[N];
        long[] C = new long[M];
        long answer = 0;
        s[0] = sc.nextInt();
        for (int i = 1; i < N; i++) {
        	s[i] = s[i - 1] + sc.nextInt();
        }
        for (int i = 0; i < N; i++) {
        	int remainder = (int) (S[i] % M);
            if (remainder == 0) answer++;
            C[remainder]++;
          }
          for (int i = 0; i < M; i++) {
          	if (C[i] > 1) {
            	answer = answer + (C[i]*(C[i] - 1 / 2);
              }
           }
           system.out.println(answer);
        }
     }   


profile
This is a velog that freely records the process I learn.

0개의 댓글