結果

問題 No.118 門松列(2)
ユーザー tenten
提出日時 2020-11-17 20:03:18
言語 Java
(openjdk 23)
結果
AC  
実行時間 625 ms / 5,000 ms
コード長 1,022 bytes
コンパイル時間 2,243 ms
コンパイル使用メモリ 78,136 KB
実行使用メモリ 59,524 KB
最終ジャッジ日時 2024-07-23 08:26:42
合計ジャッジ時間 14,297 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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();
            map.put(x, map.getOrDefault(x, 0) + 1);
        }
        long[] comb3 = new long[n + 1];
        long[] comb2 = new long[n + 1];
        for (int i = 3; i <= n; i++) {
            comb3[i] = 1;
            for (long j = 0; j < 3; j++) {
                comb3[i] *= i - j;
            }
            comb3[i] /= 6;
            comb2[i] = 1;
            for (long j = 0; j < 2; j++) {
                comb2[i] *= i - j;
            }
            comb2[i] /= 2;
        }
        comb2[2] = 1;
        long ans = comb3[n];
        for (int x : map.values()) {
            ans -= comb3[x];
            ans -= comb2[x] * (n - x);
        }
        System.out.println(ans % MOD);
    }
}
0