結果

問題 No.1809 Divide NCK
ユーザー StrorkisStrorkis
提出日時 2022-01-15 15:51:19
言語 Rust
(1.57.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 7,666 Byte
コンパイル時間 813 ms
使用メモリ 2,460 KB
最終ジャッジ日時 2022-01-15 15:51:22
合計ジャッジ時間 3,175 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
2,300 KB
testcase_01 AC 1 ms
2,184 KB
testcase_02 AC 1 ms
2,292 KB
testcase_03 AC 1 ms
2,320 KB
testcase_04 AC 1 ms
2,364 KB
testcase_05 AC 1 ms
2,372 KB
testcase_06 AC 1 ms
2,316 KB
testcase_07 AC 1 ms
2,376 KB
testcase_08 AC 1 ms
2,344 KB
testcase_09 AC 1 ms
2,312 KB
testcase_10 AC 1 ms
2,304 KB
testcase_11 AC 1 ms
2,364 KB
testcase_12 AC 1 ms
2,352 KB
testcase_13 AC 1 ms
2,328 KB
testcase_14 AC 1 ms
2,240 KB
testcase_15 AC 1 ms
2,240 KB
testcase_16 AC 10 ms
2,232 KB
testcase_17 AC 1 ms
2,408 KB
testcase_18 AC 10 ms
2,328 KB
testcase_19 AC 11 ms
2,352 KB
testcase_20 AC 1 ms
2,348 KB
testcase_21 AC 1 ms
2,412 KB
testcase_22 AC 1 ms
2,460 KB
testcase_23 AC 1 ms
2,408 KB
testcase_24 AC 1 ms
2,292 KB
testcase_25 AC 1 ms
2,276 KB
testcase_26 AC 1 ms
2,240 KB
testcase_27 AC 1 ms
2,344 KB
testcase_28 AC 1 ms
2,300 KB
testcase_29 AC 7 ms
2,248 KB
testcase_30 AC 1 ms
2,424 KB
testcase_31 AC 1 ms
2,328 KB
testcase_32 AC 1 ms
2,304 KB
testcase_33 AC 2 ms
2,352 KB
testcase_34 AC 1 ms
2,344 KB
testcase_35 AC 1 ms
2,300 KB
testcase_36 AC 1 ms
2,320 KB
testcase_37 AC 1 ms
2,340 KB
testcase_38 AC 1 ms
2,232 KB
testcase_39 AC 1 ms
2,348 KB
testcase_40 AC 1 ms
2,416 KB
testcase_41 AC 1 ms
2,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

pub mod input {
    pub trait Readable {
        type Output;

        fn read<R: std::io::BufRead>(r: &mut R) -> Self::Output;
    }

    macro_rules! impl_readable_for_int {
        ($($t:ty)*) => ($(impl Readable for $t {
            type Output = $t;

            #[inline]
            fn read<R: std::io::BufRead>(r: &mut R) -> Self::Output {
                let mut is_positive = true;
                let mut is_empty = true;
                let mut is_only_sign = true;
                let mut result: Self::Output = 0;
                loop {
                    let buf = match r.fill_buf() {
                        Ok(buf) => buf,
                        Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
                        Err(e) => panic!("{}", e), 
                    };
                    let (done, used, mut buf) = match buf.iter().position(u8::is_ascii_whitespace) {
                        Some(i) => (i > 0 || !is_empty, i + 1, &buf[..i]),
                        None => (buf.is_empty(), buf.len(), buf),
                    };
        
                    if is_empty && buf.len() > 0 {
                        is_empty = false;
                        buf = match buf[0] {
                            b'+' => &buf[1..],
                            b'-' => {
                                is_positive = false;
                                &buf[1..]
                            }
                            _ => buf,
                        };
                    }
                    if buf.len() > 0 {
                        is_only_sign = false;
                    }
        
                    if is_positive {
                        for &c in buf {
                            let x = (c as char).to_digit(10).expect("InvalidDigit");
                            result = result.checked_mul(10).expect("PosOverflow");
                            result = result.checked_add(x as $t).expect("PosOverflow");
                        }
                    } else {
                        for &c in buf {
                            let x = (c as char).to_digit(10).expect("InvalidDigit");
                            result = result.checked_mul(10).expect("NegOverflow");
                            result = result.checked_sub(x as $t).expect("NegOverflow");
                        }
                    }
        
                    r.consume(used);
        
                    if done {
                        if is_empty {
                            panic!("Empty");
                        }
                        if is_only_sign {
                            panic!("InvalidDigit");
                        }        
                        return result;
                    }
                }
            }
        })*)
    }
    impl_readable_for_int! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }

    pub mod marker {
        use super::Readable;
        
        pub enum Usize1 {}

        impl Readable for Usize1 {
            type Output = usize;

            #[inline]
            fn read<R: std::io::BufRead>(r: &mut R) -> Self::Output {
                usize::read(r).checked_sub(1).expect("NegOverflow")
            }
        }

        pub enum Bytes {}

        impl Readable for Bytes {
            type Output = Vec<u8>;

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

    impl Readable for String {
        type Output = String;

        #[inline]
        fn read<R: std::io::BufRead>(r: &mut R) -> Self::Output {
            String::from_utf8(marker::Bytes::read(r)).unwrap()
        }
    }

    macro_rules! impl_readable_for_float {
        ($($t:ty)*) => ($(impl Readable for $t {
            type Output = $t;
            
            #[inline]
            fn read<R: std::io::BufRead>(r: &mut R) -> Self::Output {
                String::read(r).parse().unwrap()
            }
        })*)
    }
    impl_readable_for_float! { f32 f64 }

    macro_rules! impl_readable_for_tuple {
        ($head:ident, $($tail:ident),*) => {
            impl<$head, $($tail),*> Readable for ($head, $($tail),*)
            where
                $head: Readable,
                $($tail: Readable),*,
            {
                type Output = (
                    <$head as Readable>::Output,
                    $(<$tail as Readable>::Output),*,
                );

                #[inline]
                fn read<R: std::io::BufRead>(r: &mut R) -> Self::Output {    
                    (<$head>::read(r), $(<$tail>::read(r)),*)
                }
            }
            impl_readable_for_tuple!($($tail),*);
        };
        ($head:ident) => {};
    }
    impl_readable_for_tuple!(A, B, C, D, E, F, G, H, I, J);

    #[macro_export]
    macro_rules! read {
        ($r:expr, [$t:tt; $n:expr]) => {{
            <[_; $n]>::try_from(read!($r, ($t; $n)).as_slice()).unwrap()
        }};
        ($r:expr, ($t:tt)) => {
            read!($r, ($t; read!($r, usize)))
        };
        ($r:expr, ($t:tt; $n:expr)) => {
            (0..$n).map(|_| read!($r, $t)).collect::<Vec<_>>()
        };
        ($r:expr, $t:ty) => {
            <$t>::read($r)
        };
    }

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

use input::Readable;

struct PrimeFactorize {
    n: u64,
    i: u64,
}

impl PrimeFactorize {
    fn new(n: u64) -> Self {
        Self {
            n,
            i: 1,
        }
    }
}

impl Iterator for PrimeFactorize {
    type Item = (u64, u32);

    fn next(&mut self) -> Option<Self::Item> {
        self.i += 1;
        while self.i.pow(2) <= self.n {
            let mut c = 0;
            while self.n % self.i == 0 {
                c += 1;
                self.n /= self.i;
            }
            if c > 0 {
                return Some((self.i, c));
            }
            self.i += 1;
        }
        if self.n > 1 {
            self.i = self.n;
            self.n = 1;
            return Some((self.i, 1));
        }
        None
    }
}

fn legendre(mut n: u64, p: u64) -> u64 {
    let mut res = 0;
    while n > 0 {
        n /= p;
        res += n;
    }
    res
}

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

    let ans = {
        PrimeFactorize::new(m)
            .map(|(p, c)| {
                (legendre(n, p) - legendre(k, p) - legendre(n - k, p)) / c as u64
            })
            .min().unwrap()
    };
    println!("{}", ans);
}

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