結果

問題 No.1629 Sorting Integers (SUM of M)
ユーザー StrorkisStrorkis
提出日時 2021-07-30 21:52:07
言語 Rust
(1.77.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 6,572 bytes
コンパイル時間 1,156 ms
コンパイル使用メモリ 119,252 KB
最終ジャッジ日時 2023-10-14 04:24:04
合計ジャッジ時間 1,683 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
error: format argument must be a string literal
  --> Main.rs:10:34
   |
10 |                 Err(e) => panic!(e), 
   |                                  ^
   |
help: you might be missing a string literal to format with
   |
10 |                 Err(e) => panic!("{}", e), 
   |                                  +++++

error: aborting due to previous error

ソースコード

diff #

pub mod io {
    use std::io::{BufRead, ErrorKind};

    pub fn scan<R: BufRead>(r: &mut R) -> Vec<u8> {
        let mut res = Vec::new();
        loop {
            let buf = match r.fill_buf() {
                Ok(buf) => buf,
                Err(e) if e.kind() == ErrorKind::Interrupted => continue,
                Err(e) => panic!(e), 
            };
            let (done, used, buf) = {
                match buf.iter().position(u8::is_ascii_whitespace) {
                    Some(i) => (i > 0 || res.len() > 0, i + 1, &buf[..i]),
                    None => (buf.is_empty(), buf.len(), buf),
                }
            };
            res.extend_from_slice(buf);
            r.consume(used);
            if done { return res; }
        }
    }

    #[macro_export]
    macro_rules! scan {
        ($r:expr, [$t:tt; $n:expr]) => {
            (0..$n).map(|_| scan!($r, $t)).collect::<Vec<_>>()
        };
        ($r:expr, [$t:tt]) => {
            scan!($r, [$t; scan!($r, usize)])
        };
        ($r:expr, ($($t:tt),*)) => {
            ($(scan!($r, $t)),*)
        };
        ($r:expr, Usize1) => {
            scan!($r, usize) - 1
        };
        ($r:expr, Bytes) => {
            io::scan($r)
        };
        ($r:expr, String) => {
            String::from_utf8(scan!($r, Bytes)).unwrap()
        };
        ($r:expr, $t:ty) => {
            scan!($r, String).parse::<$t>().unwrap()
        };
    }

    #[macro_export]
    macro_rules! input {
        ($r:expr, $($($v:ident)* : $t:tt),* $(,)?) => {
            $(let $($v)* = scan!($r, $t);)*
        };
    }
}

pub mod mod_int {
    use std::fmt;
    use std::ops::*;

    #[derive(Clone, Copy, Debug)]
    pub struct ModInt(u64);

    impl ModInt {
        const MOD: u64 = 1_000_000_007;

        pub fn new(mut x: u64) -> Self {
            if x >= Self::MOD {
                x %= Self::MOD;
            }
            Self(x)
        }

        pub fn zero() -> Self {
            Self(0)
        }

        pub fn one() -> Self {
            Self(1)
        }
    }

    impl From<usize> for ModInt {
        fn from(x: usize) -> Self {
            Self::new(x as u64)
        }
    }

    impl From<i64> for ModInt {
        fn from(mut x: i64) -> Self {
            let m = Self::MOD as i64;
            if x.abs() >= m {
                x %= m;
            }
            if x < 0 {
                x += m
            }
            Self::new(x as u64)
        }
    }

    impl Add for ModInt {
        type Output = Self;

        fn add(self, other: Self) -> Self {
            let mut x = self.0 + other.0;
            if x >= Self::MOD {
                x -= Self::MOD;
            }
            Self(x)
        }
    }

    impl AddAssign for ModInt {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }

    impl Sub for ModInt {
        type Output = Self;

        fn sub(mut self, other: Self) -> Self {
            if self.0 < other.0 {
                self.0 += Self::MOD;
            }
            Self(self.0 - other.0)
        }
    }

    impl SubAssign for ModInt {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }

    impl Mul for ModInt {
        type Output = Self;

        fn mul(self, other: Self) -> Self {
            Self::new(self.0 * other.0)
        }
    }

    impl MulAssign for ModInt {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }

    impl fmt::Display for ModInt {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.0)
        }
    }

    impl ModInt {
        pub fn pow(mut self, mut n: u64) -> Self {
            let mut res = Self::one();
            while n > 0 {
                if n & 1 == 1 {
                    res *= self;
                }
                self *= self;
                n >>= 1;
            }
            res
        }
        
        pub fn inv(self) -> Self {
            self.pow(Self::MOD - 2)
        }
    }
}

pub mod combination {
    use super::mod_int::ModInt;

    pub struct Combination {
        fact: Vec<ModInt>,
        inv_fact: Vec<ModInt>,
        inv: Vec<ModInt>,
    }
    
    impl Combination {
        pub fn new(n: usize) -> Combination {
            let mut fact = vec![ModInt::zero(); n + 1];
            let mut inv_fact = vec![ModInt::zero(); n + 1];
            let mut inv = vec![ModInt::zero(); n + 1];
            fact[0] = ModInt::one();
            for i in 1..=n {
                fact[i] = fact[i - 1] * ModInt::from(i);
            }
            inv_fact[n] = fact[n].inv();
            for i in (0..n).rev() {
                inv_fact[i] = inv_fact[i + 1] * ModInt::from(i + 1);
            }
            for i in 1..=n {
                inv[i] = inv_fact[i] * fact[i - 1];
            }
            Combination {
                fact,
                inv_fact,
                inv,
            }
        }

        pub fn fact(&self, i: usize) -> ModInt {
            self.fact[i]
        }

        pub fn inv_fact(&self, i: usize) -> ModInt {
            self.inv_fact[i]
        }

        pub fn inv(&self, i: usize) -> ModInt {
            self.inv[i]
        }

        pub fn p(&self, n: usize, r: usize) -> ModInt {
            if n < r {
                ModInt::zero()
            } else {
                self.fact[n] * self.inv_fact[n - r]
            }
        }
    
        pub fn c(&self, n: usize, r: usize) -> ModInt {
            if n < r {
                ModInt::zero()
            } else {
                self.fact[n] * self.inv_fact[n - r] * self.inv_fact[r]
            }
        }
    }
}

use mod_int::ModInt;
use combination::Combination;

fn run<R: std::io::BufRead, W: std::io::Write>(reader: &mut R, writer: &mut W) {
    input! {
        reader,
        n: usize,
        c: [usize; 9],
    }

    let comb = Combination::new(n);

    let mut sum = ModInt::zero();
    let mut p = ModInt::new(1);
    for _ in 1..=n {
        for (i, c) in c.iter().enumerate() {
            sum += ModInt::from(i + 1) * p * ModInt::from(*c)
        }
        p *= ModInt::new(10);
    }

    let ans = sum * comb.fact(n - 1) * c.iter().fold(ModInt::one(), |acc, c| acc * comb.inv_fact(*c));
    writeln!(writer, "{}", ans).ok();
}

fn main() {
    let (stdin, stdout) = (std::io::stdin(), std::io::stdout());
    let ref mut reader = std::io::BufReader::new(stdin.lock());
    let ref mut writer = std::io::BufWriter::new(stdout.lock());
    run(reader, writer);
}
0