結果

問題 No.1126 SUM
ユーザー ikdikd
提出日時 2020-07-25 00:23:04
言語 Rust
(1.77.0)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 3,406 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 147,056 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-08 06:39:24
合計ジャッジ時間 2,193 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 4 ms
4,380 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 4 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::Read;

fn read<T: std::str::FromStr>() -> T {
    let token: String = std::io::stdin()
        .bytes()
        .map(|c| c.ok().unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}

mod mint {
    use std::ops::{Add, Div, Mul, Sub};

    type Int = i64;
    pub const MOD: Int = 1_000_000_000 + 7;
    #[derive(Clone, Copy)]
    pub struct Mint {
        x: Int,
    }
    impl Mint {
        pub fn new(x: Int) -> Mint {
            Mint {
                x: (x % MOD + MOD) % MOD,
            }
        }
        pub fn val(&self) -> Int {
            self.x
        }
    }
    impl Add<Int> for Mint {
        type Output = Mint;
        fn add(self, rhs: Int) -> Mint {
            Mint::new(self.val() + (rhs % MOD))
        }
    }
    impl Add for Mint {
        type Output = Mint;
        fn add(self, rhs: Mint) -> Mint {
            self + rhs.val()
        }
    }
    impl Sub<Int> for Mint {
        type Output = Mint;
        fn sub(self, rhs: Int) -> Mint {
            Mint::new(self.val() - (rhs % MOD))
        }
    }
    impl Sub for Mint {
        type Output = Mint;
        fn sub(self, rhs: Mint) -> Mint {
            self - rhs.val()
        }
    }
    impl Mul<Int> for Mint {
        type Output = Mint;
        fn mul(self, rhs: Int) -> Mint {
            Mint::new(self.val() * (rhs % MOD))
        }
    }
    impl Mul for Mint {
        type Output = Mint;
        fn mul(self, rhs: Mint) -> Mint {
            self * rhs.val()
        }
    }
    impl Mint {
        pub fn pow(x: Mint, y: Int) -> Mint {
            assert!(y >= 0);
            let mut res = Mint::new(1);
            let mut base = x;
            let mut exp = y;
            while exp > 0 {
                if (exp & 1) == 1 {
                    res = res * base;
                }
                base = base * base;
                exp = exp / 2;
            }
            res
        }
        pub fn inv(self) -> Mint {
            Mint::pow(self, MOD - 2)
        }
    }
    impl Div<Int> for Mint {
        type Output = Mint;
        fn div(self, rhs: Int) -> Mint {
            self * Mint::new(rhs).inv()
        }
    }
    impl Div for Mint {
        type Output = Mint;
        fn div(self, rhs: Mint) -> Mint {
            self * rhs.val()
        }
    }
    impl std::fmt::Display for Mint {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.val())
        }
    }
}

use mint::Mint;

struct Cmb {
    fac: Vec<Mint>,
    fac_inv: Vec<Mint>,
}

impl Cmb {
    pub fn new(n: usize) -> Cmb {
        let mut fac = vec![Mint::new(0); n];
        let mut fac_inv = vec![Mint::new(0); n];
        fac[0] = Mint::new(1);
        for i in 1..n {
            fac[i] = fac[i - 1] * (i as i64);
        }
        fac_inv[n - 1] = fac[n - 1].inv();
        for i in (0..n - 1).rev() {
            fac_inv[i] = fac_inv[i + 1] * ((i + 1) as i64)
        }
        Cmb { fac, fac_inv }
    }
    pub fn binom(&self, a: usize, b: usize) -> Mint {
        if a < b {
            return Mint::new(0);
        }
        self.fac[a] * self.fac_inv[b] * self.fac_inv[a - b]
    }
}

fn main() {
    let n: usize = read();
    let m: usize = read();

    let cmb = Cmb::new(m + 2);
    println!("{}", cmb.binom(m + 1, m - n));
}
0