結果

問題 No.2176 LRM Question 1
ユーザー StrorkisStrorkis
提出日時 2023-01-06 23:58:08
言語 Rust
(1.77.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,463 bytes
コンパイル時間 11,830 ms
コンパイル使用メモリ 407,092 KB
実行使用メモリ 9,776 KB
最終ジャッジ日時 2024-05-07 23:59:35
合計ジャッジ時間 13,197 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 25 ms
9,672 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 15 ms
9,656 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 14 ms
9,636 KB
testcase_09 AC 13 ms
9,460 KB
testcase_10 AC 20 ms
8,432 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 15 ms
9,736 KB
testcase_14 AC 33 ms
9,776 KB
testcase_15 AC 20 ms
8,040 KB
testcase_16 AC 17 ms
6,944 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 14 ms
8,900 KB
testcase_19 AC 10 ms
6,944 KB
testcase_20 AC 4 ms
6,944 KB
testcase_21 AC 10 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 10 ms
6,940 KB
testcase_24 AC 7 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    pub trait ReadBytes {
        fn read_bytes(&mut self) -> Vec<u8>;
    }

    impl<R: BufRead> ReadBytes for R {
        #[inline]
        fn read_bytes(&mut self) -> Vec<u8> {
            let mut res = Vec::new();
            loop {
                let buf = match self.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);
                self.consume(used);
                if done {
                    return res;
                }
            }
        }
    }

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

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

use input::ReadBytes;

fn run<R: std::io::BufRead, W: std::io::Write>(reader: &mut R, _writer: &mut W) {
    input! {
        reader,
        l: u64, r: u64, m: u64,
    }

    let mut a = Vec::with_capacity(m as usize + 1);
    a.push(1);
    let mut fact = 1;
    let mut prod = 1;
    for i in 1..=m {
        fact = fact * i % m;
        prod = prod * fact % m;
        a.push(prod);
    }

    let mut sum = 0;
    for n in l..=r {
        if n % m == 0 || m <= n {
            break;
        }
        sum = (sum + a[n as usize]) % m;
        
    }
    println!("{}", sum);
}

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