結果

問題 No.797 Noelちゃんとピラミッド
ユーザー kusomushikusomushi
提出日時 2019-07-10 05:32:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 361 ms / 2,000 ms
コード長 6,040 bytes
コンパイル時間 3,087 ms
コンパイル使用メモリ 81,160 KB
実行使用メモリ 58,208 KB
最終ジャッジ日時 2024-04-21 09:36:50
合計ジャッジ時間 19,857 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,136 KB
testcase_01 AC 54 ms
50,324 KB
testcase_02 AC 53 ms
49,800 KB
testcase_03 AC 343 ms
56,452 KB
testcase_04 AC 354 ms
56,140 KB
testcase_05 AC 344 ms
56,312 KB
testcase_06 AC 353 ms
56,308 KB
testcase_07 AC 346 ms
56,372 KB
testcase_08 AC 350 ms
56,056 KB
testcase_09 AC 345 ms
56,676 KB
testcase_10 AC 343 ms
56,484 KB
testcase_11 AC 342 ms
56,416 KB
testcase_12 AC 340 ms
58,208 KB
testcase_13 AC 346 ms
45,904 KB
testcase_14 AC 358 ms
46,160 KB
testcase_15 AC 349 ms
46,212 KB
testcase_16 AC 348 ms
46,148 KB
testcase_17 AC 355 ms
46,128 KB
testcase_18 AC 360 ms
46,052 KB
testcase_19 AC 344 ms
46,104 KB
testcase_20 AC 353 ms
46,328 KB
testcase_21 AC 338 ms
46,156 KB
testcase_22 AC 344 ms
46,112 KB
testcase_23 AC 324 ms
45,992 KB
testcase_24 AC 173 ms
41,432 KB
testcase_25 AC 290 ms
45,180 KB
testcase_26 AC 286 ms
44,996 KB
testcase_27 AC 347 ms
46,320 KB
testcase_28 AC 338 ms
45,940 KB
testcase_29 AC 139 ms
40,372 KB
testcase_30 AC 189 ms
42,396 KB
testcase_31 AC 315 ms
45,864 KB
testcase_32 AC 224 ms
43,416 KB
testcase_33 AC 287 ms
45,096 KB
testcase_34 AC 229 ms
44,348 KB
testcase_35 AC 361 ms
46,184 KB
testcase_36 AC 128 ms
40,220 KB
testcase_37 AC 330 ms
45,944 KB
testcase_38 AC 323 ms
45,976 KB
testcase_39 AC 293 ms
44,972 KB
testcase_40 AC 130 ms
40,328 KB
testcase_41 AC 163 ms
41,220 KB
testcase_42 AC 307 ms
45,228 KB
testcase_43 AC 54 ms
37,212 KB
testcase_44 AC 53 ms
37,076 KB
testcase_45 AC 54 ms
37,240 KB
testcase_46 AC 53 ms
37,160 KB
testcase_47 AC 55 ms
37,012 KB
testcase_48 AC 55 ms
37,160 KB
testcase_49 AC 55 ms
37,360 KB
testcase_50 AC 52 ms
37,068 KB
testcase_51 AC 53 ms
37,248 KB
testcase_52 AC 52 ms
37,288 KB
testcase_53 AC 53 ms
37,160 KB
testcase_54 AC 53 ms
36,928 KB
testcase_55 AC 54 ms
37,012 KB
testcase_56 AC 53 ms
37,208 KB
testcase_57 AC 53 ms
37,188 KB
testcase_58 AC 53 ms
36,964 KB
testcase_59 AC 53 ms
37,208 KB
testcase_60 AC 53 ms
37,112 KB
testcase_61 AC 53 ms
37,076 KB
testcase_62 AC 53 ms
37,076 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