結果

問題 No.1552 Simple Dice Game
ユーザー fukafukatanifukafukatani
提出日時 2021-06-19 00:56:39
言語 Rust
(1.77.0)
結果
AC  
実行時間 2,178 ms / 2,500 ms
コード長 5,714 bytes
コンパイル時間 772 ms
コンパイル使用メモリ 154,748 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 00:35:15
合計ジャッジ時間 26,544 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1,942 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1,640 ms
4,376 KB
testcase_10 AC 1,890 ms
4,376 KB
testcase_11 AC 1,933 ms
4,380 KB
testcase_12 AC 573 ms
4,380 KB
testcase_13 AC 1,629 ms
4,380 KB
testcase_14 AC 942 ms
4,376 KB
testcase_15 AC 1,161 ms
4,384 KB
testcase_16 AC 161 ms
4,380 KB
testcase_17 AC 271 ms
4,380 KB
testcase_18 AC 1,521 ms
4,376 KB
testcase_19 AC 2,147 ms
4,376 KB
testcase_20 AC 2,153 ms
4,376 KB
testcase_21 AC 2,170 ms
4,384 KB
testcase_22 AC 2,133 ms
4,380 KB
testcase_23 AC 2,178 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `set_modulus` is never used
  --> Main.rs:49:8
   |
49 |     fn set_modulus(m: i128) {
   |        ^^^^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#![allow(unused_imports)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;
use std::ops::Bound::*;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn main() {
    let v = read_vec::<i128>();
    let (n, m) = (v[0], v[1]);
    let mut ans = Modulo(0);

    for i in 1..=m {
        ans += (Modulo(i).pow(n) - Modulo(i - 1).pow(n)) * Modulo::new(2 * i - m - 1);
    }

    let avg = Modulo(n) * Modulo(m + 1) * Modulo(2).inv();
    ans *= avg;
    println!("{}", ans);
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Modulo(i128);
static mut MODULUS: i128 = 998244353;
impl Modulo {
    fn set_modulus(m: i128) {
        unsafe {
            MODULUS = m;
        }
    }
    fn get_modulus() -> i128 {
        unsafe { MODULUS }
    }
    fn new(x: i128) -> Modulo {
        let m = Modulo::get_modulus();
        Modulo(x.rem_euclid(m))
    }
    fn pow(self, p: i128) -> Modulo {
        if p == 0 {
            Modulo(1)
        } else {
            let mut t = self.pow(p / 2);
            t *= t;
            if p & 1 == 1 {
                t *= self;
            }
            t
        }
    }
    fn inv(self) -> Modulo {
        self.pow(Modulo::get_modulus() - 2)
    }
}
impl std::fmt::Display for Modulo {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.0.fmt(f)
    }
}
impl std::ops::AddAssign for Modulo {
    fn add_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
impl std::ops::MulAssign for Modulo {
    fn mul_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 *= other.0;
        self.0 %= m;
    }
}
impl std::ops::SubAssign for Modulo {
    fn sub_assign(&mut self, other: Modulo) {
        let m = Modulo::get_modulus();
        self.0 += m - other.0;
        if self.0 >= m {
            self.0 -= m;
        }
    }
}
macro_rules! impl_modulo_ops {
    ($imp:ident, $method:ident, $assign_imp:ident, $assign_method:ident) => {
        impl<'a> std::ops::$assign_imp<&'a Modulo> for Modulo {
            fn $assign_method(&mut self, other: &'a Modulo) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp for Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: Modulo) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a> std::ops::$imp<&'a Modulo> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a Modulo) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b Modulo> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b Modulo) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
        impl std::ops::$assign_imp<i128> for Modulo {
            fn $assign_method(&mut self, other: i128) {
                std::ops::$assign_imp::$assign_method(self, Modulo::new(other));
            }
        }
        impl<'a> std::ops::$assign_imp<&'a i128> for Modulo {
            fn $assign_method(&mut self, other: &'a i128) {
                std::ops::$assign_imp::$assign_method(self, *other);
            }
        }
        impl std::ops::$imp<i128> for Modulo {
            type Output = Modulo;
            fn $method(self, other: i128) -> Modulo {
                let mut x = self;
                std::ops::$assign_imp::$assign_method(&mut x, other);
                x
            }
        }
        impl<'a> std::ops::$imp<&'a i128> for Modulo {
            type Output = Modulo;
            fn $method(self, other: &'a i128) -> Modulo {
                std::ops::$imp::$method(self, *other)
            }
        }
        impl<'a> std::ops::$imp<i128> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: i128) -> Modulo {
                std::ops::$imp::$method(*self, other)
            }
        }
        impl<'a, 'b> std::ops::$imp<&'b i128> for &'a Modulo {
            type Output = Modulo;
            fn $method(self, other: &'b i128) -> Modulo {
                std::ops::$imp::$method(*self, *other)
            }
        }
    };
}
impl_modulo_ops!(Add, add, AddAssign, add_assign);
impl_modulo_ops!(Mul, mul, MulAssign, mul_assign);
impl_modulo_ops!(Sub, sub, SubAssign, sub_assign);

use std::iter::Sum;
impl Sum for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = Modulo>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}

impl<'a> Sum<&'a Modulo> for Modulo {
    fn sum<I>(iter: I) -> Self
    where
        I: Iterator<Item = &'a Self>,
    {
        iter.fold(Modulo(0), |a, b| a + b)
    }
}
0