Notice
Recent Posts
Recent Comments
05-18 01:37
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Byeol Lo

N10986 - 나머지 합 본문

Algorithm/Accumulated Sum

N10986 - 나머지 합

알 수 없는 사용자 2024. 2. 13. 12:48
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.HashMap;


public class Main {
    private static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    static final HashMap<Integer, Long> table = new HashMap<>();

    public static void main(String[] args) throws IOException {
        StringTokenizer st = new StringTokenizer(BR.readLine());
        int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
        long result = 0;
        int[] arr = new int[n+1];

        st = new StringTokenizer(BR.readLine());
        for(int i = 1; i <= n; i++) {
            arr[i] = (Integer.parseInt(st.nextToken()) + arr[i - 1]) % m;
            if(!table.containsKey(arr[i])) table.put(arr[i], 1l);
            else table.put(arr[i], table.get(arr[i])+1);
        }

        for(int i : table.keySet()) {
            if(i == 0) result += (table.get(i)+1) * table.get(i) / 2;
            else result += (table.get(i) - 1) * table.get(i) / 2;
        }

        System.out.println(result);
    }
}

'Algorithm > Accumulated Sum' 카테고리의 다른 글

N25682 - 체스판 다시 칠하기 2  (1) 2024.02.13
N11660 - 구간 합 구하기 5  (1) 2024.02.13
Comments