結果

問題 No.117 組み合わせの数
ユーザー mikitmikit
提出日時 2018-12-06 01:24:42
言語 Java21
(openjdk 21)
結果
RE  
実行時間 -
コード長 5,755 bytes
コンパイル時間 2,393 ms
コンパイル使用メモリ 80,516 KB
実行使用メモリ 89,980 KB
最終ジャッジ日時 2023-10-11 07:20:19
合計ジャッジ時間 3,250 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.util.Objects;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mikit
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        LightScanner in = new LightScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        D solver = new D();
        solver.solve(1, in, out);
        out.close();
    }

    static class D {
        public void solve(int testNumber, LightScanner in, PrintWriter out) {
            int t = in.ints();
            ModMath m = new ModMath();
            Factorial f = new Factorial(m, 2100000);
            for (int i = 0; i < t; i++) {
                String s = in.string();
                int n = Integer.parseInt(s.substring(2, s.indexOf(',')));
                int r = Integer.parseInt(s.substring(s.indexOf(',') + 1, s.indexOf(')')));
                switch (s.charAt(0)) {
                    case 'C':
                        out.println(f.ncr(n, r));
                        break;
                    case 'P':
                        out.println(f.npr(n, r));
                        break;
                    case 'H':
                        out.println(f.nhr(n, r));
                        break;
                }
            }
        /*long n = in.longs(), k = in.longs();
        long ans = 1;
        for (int i = 0; i < k; i++) {
            ans *= (n + i);
            ans %= MOD;
        }
        ModMath m = new ModMath();
        for (int i = 2; i<= k; i++) {
            System.out.println(m.inv(i));
            ans *= m.inv(i);
            ans %= MOD;
        }
        out.println(ans);*/
        }

    }

    static class ModMath {
        private static final int DEFAULT_MOD = 1_000_000_007;
        private final long mod;

        public ModMath(long mod) {
            this.mod = mod;
        }

        public ModMath() {
            this(DEFAULT_MOD);
        }

        public long mod(long x) {
            x %= mod;
            return x < 0 ? x + mod : x;
        }

        public long inv(long x) {
            return mod(LongEuclidSolver.solve(x, -mod).getX());
        }

    }

    static class LightScanner {
        private BufferedReader reader = null;
        private StringTokenizer tokenizer = null;

        public LightScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

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

        public int ints() {
            return Integer.parseInt(string());
        }

    }

    static class LongEuclidSolver {
        private static final LongEuclidSolver instance = new LongEuclidSolver();
        private long x;
        private long y;

        private long solve(long a, long b, boolean r) {
            if (b == 0) {
                x = 1;
                y = 0;
                return a;
            }
            long d = solve(b, a % b, !r);
            if (r) {
                x -= a / b * y;
            } else {
                y -= a / b * x;
            }
            return d;
        }

        public static Vec2l solve(long a, long b) {
            if (a >= b) {
                instance.solve(a, b, false);
            } else {
                instance.solve(b, a, true);
            }
            return new Vec2l(instance.x, instance.y);
        }

    }

    static class Factorial {
        private final ModMath mod;
        private final int n;
        private final long[] natural;
        private final long[] reverse;

        public Factorial(ModMath mod, int n) {
            this.mod = mod;
            this.n = n;
            this.natural = new long[n];
            this.reverse = new long[n];
            natural[0] = 1;
            for (int i = 1; i < n; i++) {
                natural[i] = mod.mod(natural[i - 1] * i);
            }
            reverse[n - 1] = mod.inv(natural[n - 1]);
            for (int i = n - 1; i > 0; i--) {
                reverse[i - 1] = reverse[i] * i;
            }
        }

        public long npr(int n, int r) {
            return mod.mod(natural[n] * reverse[n - r]);
        }

        public long ncr(int n, int r) {
            return mod.mod(npr(n, r) * reverse[r]);
        }

        public long nhr(int n, int r) {
            return (n | r) == 0 ? 1 : ncr(n + r - 1, r);
        }

    }

    static class Vec2l {
        private final long x;
        private final long y;

        public Vec2l(long x, long y) {
            this.x = x;
            this.y = y;
        }

        public long getX() {
            return x;
        }

        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Vec2l vec2i = (Vec2l) o;
            return x == vec2i.x &&
                    y == vec2i.y;
        }

        public int hashCode() {
            return Objects.hash(x, y);
        }

        public String toString() {
            return "(" + x + ", " + y + ")";
        }

    }
}

0