結果

問題 No.118 門松列(2)
ユーザー htensai
提出日時 2019-12-05 12:46:31
言語 Java
(openjdk 23)
結果
AC  
実行時間 556 ms / 5,000 ms
コード長 949 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 78,248 KB
実行使用メモリ 59,636 KB
最終ジャッジ日時 2024-12-17 18:42:58
合計ジャッジ時間 13,414 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    static final int MOD = 1000000007;
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 0; i < n; i++) {
		    int x = sc.nextInt();
		    if (map.containsKey(x)) {
		        map.put(x, map.get(x) + 1);
		    } else {
		        map.put(x, 1);
		    }
		}
		int length = map.size();
		int[] arr = new int[length + 1];
		int[] sums = new int[length + 1];
		int[] totals = new int[length + 1];
		int idx = 1;
		for (int x : map.values()) {
		    arr[idx] = x;
		    sums[idx] = sums[idx - 1] + arr[idx];
		    sums[idx] %= MOD;
		    totals[idx] = (int)((long)(arr[idx]) * sums[idx - 1] + totals[idx - 1] % MOD);
		    idx++;
		}
		long ans = 0;
		for (int i = length; i >= 1; i--) {
		    ans += (long)(arr[i]) * totals[i - 1];
		    ans %= MOD;
		}
	    System.out.println(ans);
	}
}
	
0