結果

問題 No.1685 One by One
ユーザー 👑 hitonanodehitonanode
提出日時 2021-08-14 11:49:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,169 ms / 3,000 ms
コード長 3,458 bytes
コンパイル時間 2,564 ms
コンパイル使用メモリ 74,292 KB
実行使用メモリ 59,968 KB
最終ジャッジ日時 2023-09-12 06:24:52
合計ジャッジ時間 28,361 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,864 KB
testcase_01 AC 127 ms
56,048 KB
testcase_02 AC 135 ms
55,556 KB
testcase_03 AC 511 ms
57,900 KB
testcase_04 AC 326 ms
57,608 KB
testcase_05 AC 604 ms
57,320 KB
testcase_06 AC 542 ms
57,444 KB
testcase_07 AC 560 ms
57,028 KB
testcase_08 AC 182 ms
56,740 KB
testcase_09 AC 250 ms
57,032 KB
testcase_10 AC 1,107 ms
55,924 KB
testcase_11 AC 262 ms
57,092 KB
testcase_12 AC 202 ms
57,056 KB
testcase_13 AC 569 ms
57,368 KB
testcase_14 AC 466 ms
58,040 KB
testcase_15 AC 727 ms
55,480 KB
testcase_16 AC 257 ms
57,860 KB
testcase_17 AC 205 ms
57,244 KB
testcase_18 AC 240 ms
57,500 KB
testcase_19 AC 137 ms
55,808 KB
testcase_20 AC 393 ms
56,592 KB
testcase_21 AC 253 ms
56,576 KB
testcase_22 AC 443 ms
56,968 KB
testcase_23 AC 365 ms
57,120 KB
testcase_24 AC 1,016 ms
57,752 KB
testcase_25 AC 978 ms
57,536 KB
testcase_26 AC 1,094 ms
59,968 KB
testcase_27 AC 898 ms
56,928 KB
testcase_28 AC 1,117 ms
57,224 KB
testcase_29 AC 934 ms
56,892 KB
testcase_30 AC 1,169 ms
57,692 KB
testcase_31 AC 928 ms
57,408 KB
testcase_32 AC 913 ms
57,076 KB
testcase_33 AC 904 ms
56,936 KB
testcase_34 AC 131 ms
55,736 KB
testcase_35 AC 129 ms
55,572 KB
testcase_36 AC 127 ms
55,244 KB
testcase_37 AC 127 ms
53,540 KB
testcase_38 AC 399 ms
57,252 KB
testcase_39 AC 397 ms
56,320 KB
testcase_40 AC 218 ms
57,580 KB
testcase_41 AC 213 ms
56,744 KB
testcase_42 AC 128 ms
53,900 KB
testcase_43 AC 127 ms
55,740 KB
testcase_44 AC 129 ms
55,796 KB
testcase_45 AC 128 ms
55,572 KB
testcase_46 AC 525 ms
55,864 KB
testcase_47 AC 434 ms
57,208 KB
testcase_48 AC 580 ms
57,504 KB
testcase_49 AC 418 ms
56,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;


public class Main {

    static int pow(long x, int k, int md) {
        long ret = 1;
        while (k > 0) {
            if ((k & 1) == 1) {
                ret = ret * x % md;
            }
            x = x * x % md;
            k >>= 1;
        }
        return (int)ret;
    }

    public class Acl_fac {
        long facs[];
        long facinvs[];
        int md;
        public Acl_fac(int N, int md) {
            this.facs = new long[N + 1];
            this.facinvs = new long[N + 1];
            this.md = md;
            this.facs[0] = this.facinvs[0] = 1;
            for (int i = 1; i <= N; i++) this.facs[i] = this.facs[i - 1] * i % md;
            this.facinvs[N] = pow(this.facs[N], md - 2, md);
            for (int i = N; i > 0; i--) this.facinvs[i - 1] = this.facinvs[i] * i % md;
        }
        long ncr(int n, int r) {
            if (n < 0 || r < 0 || n < r) return 0;
            return this.facs[n] * this.facinvs[r] % this.md * this.facinvs[n - r] % this.md;
        }
    }

    final static int md = 1000000007;

    public long run() {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt();
        final int M = sc.nextInt();
        Acl_fac fac = new Acl_fac(N + M, md);

        final long precalc[] = new long[M + 1];

        long ret = 0;

        for (int nnonzero = 0; nnonzero <= N; nnonzero++) {
            if (nnonzero == 0) precalc[0] = 1;
            for (int m = 0; m <= M; m++) {
                if (nnonzero + m - 1 >= 0) precalc[m] = fac.ncr(nnonzero + m - 1, nnonzero - 1);
                if (m >= 2) {
                    precalc[m] += precalc[m - 2];
                    if (precalc[m] >= md) precalc[m] -= md;
                }
            }

            if (N % 2 == 0) {
                int half = (N - 1) / 2;
                if (nnonzero > half * 2) continue;
                long coeff = 0;
                for (int npos = 0; npos <= half; npos++) {
                    int nneg = nnonzero - npos;
                    if (nneg < 0 || nneg > half) continue;
                    coeff += fac.ncr(N - 1, npos) * fac.ncr(N - 1 - npos, nneg);
                    coeff %= md;
                }
                long tmp = 0;
                for (int b0 = 0; b0 <= M; b0++) {
                    if (M - nnonzero - b0 < 0) continue;
                    tmp += precalc[M - nnonzero - b0] * (b0 == 0 ? 1 : 2);
                    tmp %= md;
                }
                ret += tmp * coeff;
                ret %= md;
            } else {
                long sum = 0;
                for (int npos = 0; npos <= N / 2; npos++) {
                    int nneg = nnonzero - npos;
                    if (nneg < 0 || nneg > N / 2) continue;
                    long coeff = 0;
                    if (M - nnonzero >= 0) coeff += precalc[M - nnonzero];
                    int m = M - npos - (N - nneg);
                    if (m >= 0) coeff += precalc[m] * 2;
                    m = M - (N - nnonzero) - (npos > nneg ? npos : nneg) * 2;
                    if (m >= 0) coeff += md - precalc[m];
                    sum += fac.ncr(nnonzero, npos) * coeff;
                    sum %= md;
                }
                ret += fac.ncr(N, nnonzero) * sum;
                ret %= md;
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        long ret = new Main().run();
        System.out.println(ret);
    }
}
0