Submission #35396551


Source Code Expand

use proconio::input;

fn pow_mod(x: usize, mut n: usize, m: usize) -> usize {
    if m == 1 {
        return 0;
    }
    let mut r = 1;
    let mut y = x % m;
    while n != 0 {
        if (n & 1) == 1 {
            r = (r * y) % m;
        }
        y = (y * y) % m;
        n >>= 1;
    }
    r
}

fn f(x: usize, n: usize, m: usize) -> usize {
    if n == 1 {
        return x;
    }
    let r = f(x, n / 2, m);
    let r = (r * pow_mod(10, n / 2, m) + r) % m;
    (if (n & 1) == 0 { r } else { r * 10 + x }) % m
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {
        assert_eq!(f(1, 1, 100), 1);
        // assert_eq!(f(1, 2, 100), 11);
        // assert_eq!(f(1, 3, 100), 11);
        // assert_eq!(f(1, 4, 100), 11);
    }

    #[test]
    fn pow_mod_test() {
        let g = pow_mod;
        assert_eq!(g(1, 1, 100), 1);
        assert_eq!(g(1, 2, 100), 1);
        assert_eq!(g(1, 3, 100), 1);
        assert_eq!(g(2, 1, 100), 2);
        assert_eq!(g(2, 2, 100), 4);
        assert_eq!(g(2, 3, 100), 8);
        assert_eq!(g(2, 1, 5), 2);
        assert_eq!(g(2, 2, 5), 4);
        assert_eq!(g(2, 3, 5), 3);
    }
}

fn main() {
    input! {
        k: usize,
        m: usize,
        cd: [(usize, usize); k],
    };
    let mut sum = 0_usize;
    for (c, d) in cd {
        sum = (sum * pow_mod(10, d, m) + f(c, d, m)) % m;
    }
    let ans = sum;
    println!("{}", ans);
}

Submission Info

Submission Time
Task L - N mod M
User bouzuya
Language Rust (1.42.0)
Score 6
Code Size 1416 Byte
Status AC
Exec Time 1058 ms
Memory 4960 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 6 / 6
Status
AC × 3
AC × 35
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 5 ms 1904 KiB
random_01.txt AC 1055 ms 4828 KiB
random_02.txt AC 1057 ms 4824 KiB
random_03.txt AC 616 ms 3684 KiB
random_04.txt AC 275 ms 2772 KiB
random_05.txt AC 1051 ms 4820 KiB
random_06.txt AC 1057 ms 4756 KiB
random_07.txt AC 984 ms 4724 KiB
random_08.txt AC 175 ms 2264 KiB
random_09.txt AC 1053 ms 4816 KiB
random_10.txt AC 1055 ms 4848 KiB
random_11.txt AC 1015 ms 4792 KiB
random_12.txt AC 468 ms 3120 KiB
random_13.txt AC 1055 ms 4768 KiB
random_14.txt AC 1054 ms 4840 KiB
random_15.txt AC 198 ms 2584 KiB
random_16.txt AC 74 ms 2252 KiB
random_17.txt AC 1054 ms 4740 KiB
random_18.txt AC 1050 ms 4860 KiB
random_19.txt AC 1053 ms 4852 KiB
random_20.txt AC 1050 ms 4880 KiB
random_21.txt AC 592 ms 3608 KiB
random_22.txt AC 363 ms 3076 KiB
random_23.txt AC 202 ms 2616 KiB
random_24.txt AC 596 ms 3684 KiB
random_25.txt AC 969 ms 4916 KiB
random_26.txt AC 969 ms 4900 KiB
random_27.txt AC 967 ms 4960 KiB
random_28.txt AC 966 ms 4820 KiB
random_29.txt AC 1055 ms 4832 KiB
random_30.txt AC 1058 ms 4792 KiB
random_31.txt AC 1055 ms 4808 KiB
sample_01.txt AC 1 ms 1920 KiB
sample_02.txt AC 2 ms 2052 KiB
sample_03.txt AC 1 ms 1992 KiB