結果

問題 No.797 Noelちゃんとピラミッド
ユーザー kusomushikusomushi
提出日時 2019-07-10 05:32:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 6,040 bytes
コンパイル時間 2,522 ms
コンパイル使用メモリ 81,192 KB
実行使用メモリ 46,572 KB
最終ジャッジ日時 2024-10-13 08:29:47
合計ジャッジ時間 18,606 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
37,268 KB
testcase_01 AC 49 ms
37,276 KB
testcase_02 AC 49 ms
36,820 KB
testcase_03 AC 323 ms
46,332 KB
testcase_04 AC 335 ms
46,128 KB
testcase_05 AC 314 ms
46,292 KB
testcase_06 AC 339 ms
46,248 KB
testcase_07 AC 336 ms
46,180 KB
testcase_08 AC 323 ms
46,488 KB
testcase_09 AC 332 ms
46,096 KB
testcase_10 AC 338 ms
46,404 KB
testcase_11 AC 337 ms
46,240 KB
testcase_12 AC 322 ms
46,056 KB
testcase_13 AC 328 ms
46,296 KB
testcase_14 AC 335 ms
46,132 KB
testcase_15 AC 322 ms
46,108 KB
testcase_16 AC 335 ms
46,192 KB
testcase_17 AC 328 ms
46,148 KB
testcase_18 AC 308 ms
46,572 KB
testcase_19 AC 326 ms
45,988 KB
testcase_20 AC 320 ms
46,292 KB
testcase_21 AC 334 ms
46,344 KB
testcase_22 AC 327 ms
46,380 KB
testcase_23 AC 306 ms
46,104 KB
testcase_24 AC 161 ms
41,316 KB
testcase_25 AC 293 ms
45,248 KB
testcase_26 AC 290 ms
45,032 KB
testcase_27 AC 339 ms
46,148 KB
testcase_28 AC 292 ms
46,336 KB
testcase_29 AC 139 ms
40,596 KB
testcase_30 AC 186 ms
42,576 KB
testcase_31 AC 300 ms
46,064 KB
testcase_32 AC 190 ms
43,580 KB
testcase_33 AC 273 ms
45,040 KB
testcase_34 AC 242 ms
44,876 KB
testcase_35 AC 338 ms
46,364 KB
testcase_36 AC 132 ms
40,236 KB
testcase_37 AC 305 ms
46,040 KB
testcase_38 AC 319 ms
46,148 KB
testcase_39 AC 274 ms
45,152 KB
testcase_40 AC 131 ms
40,476 KB
testcase_41 AC 166 ms
42,172 KB
testcase_42 AC 278 ms
45,276 KB
testcase_43 AC 49 ms
37,344 KB
testcase_44 AC 51 ms
37,332 KB
testcase_45 AC 51 ms
37,284 KB
testcase_46 AC 48 ms
37,048 KB
testcase_47 AC 52 ms
37,356 KB
testcase_48 AC 50 ms
37,164 KB
testcase_49 AC 49 ms
37,284 KB
testcase_50 AC 48 ms
36,908 KB
testcase_51 AC 51 ms
36,900 KB
testcase_52 AC 48 ms
37,284 KB
testcase_53 AC 51 ms
37,520 KB
testcase_54 AC 49 ms
36,776 KB
testcase_55 AC 50 ms
37,316 KB
testcase_56 AC 51 ms
37,248 KB
testcase_57 AC 50 ms
36,892 KB
testcase_58 AC 50 ms
36,984 KB
testcase_59 AC 50 ms
37,180 KB
testcase_60 AC 51 ms
37,284 KB
testcase_61 AC 50 ms
37,284 KB
testcase_62 AC 50 ms
37,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.Arrays;
import java.util.StringJoiner;
import java.util.StringTokenizer;
import java.util.function.Function;

public class Main {

    static int N;
    static int[] A;

    public static void main(String[] args) {
        FastScanner sc = new FastScanner(System.in);
        N = sc.nextInt();
        A = sc.nextIntArray(N);

        System.out.println(solve());
    }

    static int solve() {
        FermatCombination fc = new FermatCombination(N+1);

        int ans = 0;

        for (int i = 0; i < N; i++) {
            ans = add(ans, mul(A[i], fc.comb(N-1, i)));
        }

        return ans;
    }

    static int MOD = 1_000_000_007;

    static int pow(int base, long exp) {
        if (exp == 0) return 1;

        int ans = 1;
        base %= MOD;
        while (exp > 0) {
            if ((exp & 1) == 1) {
                ans = mul(ans, base);
            }

            base = mul(base, base);
            exp = exp >> 1;
        }
        return ans;
    }

    static int sub(int a, int b) {
        int c = a - b;
        if (c < 0) c += MOD;
        return c;
    }

    static int div(int a, int b) {
        return mul(a, pow(b, MOD - 2));
    }

    static int add(int a, int b) {
        int c = a + b;
        if (c >= MOD) c %= MOD;
        return c;
    }

    static int mul(int a, int b) {
        long c = (long) a * b;
        if (c >= MOD) c %= MOD;
        return (int) c;
    }

    static class FermatCombination {
        private final int size;
        private final int[] factorial; // n -> factorial(n)
        private final int[] inverse;   // n -> inverse(factorial(n))

        FermatCombination(int size) {
            this.size = size;
            factorial = new int[size + 1];
            inverse = new int[size + 1];

            init();
        }

        private void init() {
            factorial[0] = 1;
            factorial[1] = 1;
            inverse[0] = 1;
            inverse[1] = 1;
            for (int i = 2; i <= size; i++) {
                factorial[i] = mul(factorial[i - 1], i);
                inverse[i] = pow(factorial[i], MOD - 2);
            }
        }

        int comb(int n, int k) {
            if (n > size) throw new RuntimeException("wtf : size=" + size + " n=" + n);
            return mul(mul(factorial[n], inverse[k]), inverse[n - k]);
        }

        // 重複組み合わせ
        // k種類からn個選ぶ場合の数
        int hcomb(int k, int n) {
            return comb(k + n - 1, n);
        }

        int group(int n, int g, int k) {
            // C(n, g) * C(n-g, g)... / k!
            // n! / (n-gk)! / g! ^ k / k!
            int ret = factorial[n];             // n!
            ret = mul(ret, inverse[n - g * k]);   // 1 / (n-gk)!
            ret = mul(ret, pow(inverse[g], k)); // 1 / (g! ^ k)
            ret = mul(ret, inverse[k]);         // 1 / k!
            return ret;
        }
    }

    @SuppressWarnings("unused")
    static class FastScanner {
        private BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
            tokenizer = null;
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        String nextLine() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    return reader.readLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken("\n");
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        int[] nextIntArray(int n, int delta) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt() + delta;
            return a;
        }

        long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }

    static <A> void writeLines(A[] as, Function<A, String> f) {
        PrintWriter pw = new PrintWriter(System.out);
        for (A a : as) {
            pw.println(f.apply(a));
        }
        pw.flush();
    }

    static void writeLines(int[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (int a : as) pw.println(a);
        pw.flush();
    }

    static void writeLines(long[] as) {
        PrintWriter pw = new PrintWriter(System.out);
        for (long a : as) pw.println(a);
        pw.flush();
    }

    static int max(int... as) {
        int max = Integer.MIN_VALUE;
        for (int a : as) max = Math.max(a, max);
        return max;
    }

    static int min(int... as) {
        int min = Integer.MAX_VALUE;
        for (int a : as) min = Math.min(a, min);
        return min;
    }

    static void debug(Object... args) {
        StringJoiner j = new StringJoiner(" ");
        for (Object arg : args) {
            if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
            else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
            else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
            else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
            else j.add(arg.toString());
        }
        System.err.println(j.toString());
    }
}
0