結果

問題 No.502 階乗を計算するだけ
ユーザー tubo28tubo28
提出日時 2017-04-26 16:49:03
言語 Rust
(1.77.0)
結果
AC  
実行時間 451 ms / 1,000 ms
コード長 3,664 bytes
コンパイル時間 5,650 ms
コンパイル使用メモリ 140,912 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-11 13:52:20
合計ジャッジ時間 5,451 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 1 ms
4,356 KB
testcase_05 AC 1 ms
4,352 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 1 ms
4,352 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 1 ms
4,352 KB
testcase_21 AC 1 ms
4,368 KB
testcase_22 AC 5 ms
4,352 KB
testcase_23 AC 2 ms
4,356 KB
testcase_24 AC 4 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 3 ms
4,352 KB
testcase_29 AC 1 ms
4,352 KB
testcase_30 AC 5 ms
4,352 KB
testcase_31 AC 3 ms
4,352 KB
testcase_32 AC 163 ms
4,352 KB
testcase_33 AC 451 ms
4,356 KB
testcase_34 AC 312 ms
4,352 KB
testcase_35 AC 136 ms
4,352 KB
testcase_36 AC 295 ms
4,348 KB
testcase_37 AC 360 ms
4,356 KB
testcase_38 AC 290 ms
4,348 KB
testcase_39 AC 446 ms
4,352 KB
testcase_40 AC 8 ms
4,368 KB
testcase_41 AC 1 ms
4,352 KB
testcase_42 AC 1 ms
4,352 KB
testcase_43 AC 1 ms
4,352 KB
testcase_44 AC 1 ms
4,352 KB
testcase_45 AC 1 ms
4,352 KB
testcase_46 AC 1 ms
4,348 KB
testcase_47 AC 1 ms
4,348 KB
testcase_48 AC 1 ms
4,348 KB
testcase_49 AC 1 ms
4,352 KB
testcase_50 AC 1 ms
4,352 KB
testcase_51 AC 1 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let memo = [
        1,
        927880474,
        933245637,
        668123525,
        429277690,
        733333339,
        724464507,
        957939114,
        203191898,
        586445753,
        698611116,
    ];
    const MOD: u64 = 1000000007;
    const M: u64 = 100000000;
    let mut sc = cin();
    let n: u64 = sc.next();
    let ans = if n >= MOD {
        0
    } else {
        let mut res = memo[(n / M) as usize];
        for i in (n-n%M+1)..n+1 {
            res *= i;
            res %= MOD;
        }
        res
    };
    println!("{}", ans);
}

#[allow(dead_code)]
fn cin() -> Scanner<std::io::Stdin> {
    Scanner::new(std::io::stdin())
}

#[allow(dead_code)]
pub struct Scanner<T> {
    buf: Vec<u8>,
    len: usize,
    idx: usize,
    reader: T,
}

#[allow(dead_code)]
impl<Reader: std::io::Read> Scanner<Reader> {
    fn new(r: Reader) -> Scanner<Reader> {
        Scanner {
            buf: vec![0; 8192],
            len: 0,
            idx: 0,
            reader: r,
        }
    }

    fn next<T: std::str::FromStr>(&mut self) -> T {
        self.wrapped::<T>().unwrap()
    }

    fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.next()).collect()
    }

    fn mat<T: std::str::FromStr>(&mut self, r: usize, c: usize) -> Vec<Vec<T>> {
        (0..r).map(|_| self.vec(c)).collect()
    }

    fn vec_char(&mut self) -> Vec<char> {
        self.next_token().take().unwrap().chars().collect()
    }

    fn mat_char(&mut self, r: usize) -> Vec<Vec<char>> {
        (0..r).map(|_| self.vec_char()).collect()
    }

    fn wrapped<T: std::str::FromStr>(&mut self) -> Option<T> {
        self.next_token().and_then(|s| s.parse::<_>().ok())
    }

    fn next_token(&mut self) -> Option<String> {
        let mut res = String::with_capacity(16);
        while let Some(c) = self.get_u8() {
            let d = c as char;
            if !d.is_whitespace() {
                res.push(d);
            } else if res.len() != 0 {
                self.unget_u8(c);
                break;
            }
        }
        if res.len() == 0 { None } else { Some(res) }
    }

    fn next_line(&mut self) -> String {
        self.next_line_wrapped().unwrap()
    }

    fn next_line_wrapped(&mut self) -> Option<String> {
        let c = self.get_u8();
        if c.is_none() {
            return None;
        }
        let mut line = String::with_capacity(20);
        line.push(c.unwrap() as char);
        loop {
            let c = self.get_u8();
            if c.is_none() || c.unwrap() == b'\n' {
                // コメントはC++等での仕様
                // if c.is_some() {
                //     self.unget_u8(b'\n');
                // }
                return Some(line);
            }
            line.push(c.unwrap() as char);
        }
    }

    fn has_next(&mut self) -> bool {
        loop {
            let c = self.get_u8();
            if c.is_none() {
                return false;
            }
            let c = c.unwrap();
            if !(c as char).is_whitespace() {
                self.unget_u8(c);
                return true;
            }
        }
    }

    fn get_u8(&mut self) -> Option<u8> {
        if self.idx == self.len {
            match self.reader.read(&mut self.buf[..]) {
                Ok(l) if l > 0 => {
                    self.idx = 0;
                    self.len = l;
                }
                _ => return None,
            }
        }
        self.idx += 1;
        Some(self.buf[self.idx - 1])
    }

    fn unget_u8(&mut self, c: u8) {
        self.idx = self.idx - 1;
        self.buf[self.idx] = c;
    }
}
0