Submission #5499865


Source Code Expand

pub trait Readable {
    type Output;
    fn words_count() -> usize;
    fn read_words(words: &[&str]) -> Result<Self::Output, String>;
}
#[macro_export]
macro_rules! readable {
    ( $ t : ty , $ words_count : expr , |$ words : ident | $ read_words : expr ) => {
        impl Readable for $t {
            type Output = $t;
            fn words_count() -> usize {
                $words_count
            }
            fn read_words($words: &[&str]) -> Result<$t, String> {
                Ok($read_words)
            }
        }
    };
}
readable!((), 1, |_ss| ());
readable!(String, 1, |ss| ss[0].to_string());
impl Readable for char {
    type Output = char;
    fn words_count() -> usize {
        1
    }
    fn read_words(words: &[&str]) -> Result<char, String> {
        let chars: Vec<char> = words[0].chars().collect();
        if chars.len() == 1 {
            Ok(chars[0])
        } else {
            Err(format!("cannot parse \"{}\" as a char", words[0]))
        }
    }
}
pub struct Chars();
impl Readable for Chars {
    type Output = Vec<char>;
    fn words_count() -> usize {
        1
    }
    fn read_words(words: &[&str]) -> Result<Vec<char>, String> {
        Ok(words[0].chars().collect())
    }
}
macro_rules ! impl_readable_for_ints { ( $ ( $ t : ty ) * ) => { $ ( impl Readable for $ t { type Output = Self ; fn words_count ( ) -> usize { 1 } fn read_words ( words : & [ & str ] ) -> Result <$ t , String > { use std :: str :: FromStr ; <$ t >:: from_str ( words [ 0 ] ) . map_err ( | _ | { format ! ( "cannot parse \"{}\" as {}" , words [ 0 ] , stringify ! ( $ t ) ) } ) } } ) * } ; }
impl_readable_for_ints ! ( i8 u8 i16 u16 i32 u32 i64 u64 isize usize f32 f64 ) ;
macro_rules ! define_one_origin_int_types { ( $ new_t : ident $ int_t : ty ) => { # [ doc = " Converts 1-origin integer into 0-origin when read from stdin." ] # [ doc = "" ] # [ doc = " # Example" ] # [ doc = "" ] # [ doc = " ```no_run" ] # [ doc = " # #[macro_use] extern crate atcoder_snippets;" ] # [ doc = " # use atcoder_snippets::read::*;" ] # [ doc = " // Stdin: \"1\"" ] # [ doc = " read!(a = usize_);" ] # [ doc = " assert_eq!(a, 0);" ] # [ doc = " ```" ] # [ allow ( non_camel_case_types ) ] pub struct $ new_t ( ) ; impl Readable for $ new_t { type Output = $ int_t ; fn words_count ( ) -> usize { 1 } fn read_words ( words : & [ & str ] ) -> Result < Self :: Output , String > { <$ int_t >:: read_words ( words ) . map ( | n | n - 1 ) } } } ; ( $ new_t : ident $ int_t : ty ; $ ( $ inner_new_t : ident $ inner_int_t : ty ) ;* ) => { define_one_origin_int_types ! ( $ new_t $ int_t ) ; define_one_origin_int_types ! ( $ ( $ inner_new_t $ inner_int_t ) ;* ) ; } ; }
define_one_origin_int_types ! ( u8_ u8 ; u16_ u16 ; u32_ u32 ; u64_ u64 ; usize_ usize ) ;
macro_rules ! impl_readable_for_tuples { ( $ t : ident $ var : ident ) => ( ) ; ( $ t : ident $ var : ident ; $ ( $ inner_t : ident $ inner_var : ident ) ;* ) => { impl_readable_for_tuples ! ( $ ( $ inner_t $ inner_var ) ;* ) ; impl <$ t : Readable , $ ( $ inner_t : Readable ) ,*> Readable for ( $ t , $ ( $ inner_t ) ,* ) { type Output = ( <$ t >:: Output , $ ( <$ inner_t >:: Output ) ,* ) ; fn words_count ( ) -> usize { let mut n = <$ t >:: words_count ( ) ; $ ( n += <$ inner_t >:: words_count ( ) ; ) * n } # [ allow ( unused_assignments ) ] fn read_words ( words : & [ & str ] ) -> Result < Self :: Output , String > { let mut start = 0 ; let $ var = <$ t >:: read_words ( & words [ start .. start +<$ t >:: words_count ( ) ] ) ?; start += <$ t >:: words_count ( ) ; $ ( let $ inner_var = <$ inner_t >:: read_words ( & words [ start .. start +<$ inner_t >:: words_count ( ) ] ) ?; start += <$ inner_t >:: words_count ( ) ; ) * Ok ( ( $ var , $ ( $ inner_var ) ,* ) ) } } } ; }
impl_readable_for_tuples ! ( T4 x4 ; T3 x3 ; T2 x2 ; T1 x1 ) ;
pub trait ReadableFromLine {
    type Output;
    fn read_line(line: &str) -> Result<Self::Output, String>;
}
fn split_into_words(line: &str) -> Vec<&str> {
    #[allow(deprecated)]
    line.trim_right_matches('\n').split_whitespace().collect()
}
impl<T: Readable> ReadableFromLine for T {
    type Output = T::Output;
    fn read_line(line: &str) -> Result<T::Output, String> {
        let words = split_into_words(line);
        if words.len() != T::words_count() {
            return Err(format!(
                "line \"{}\" has {} words, expected {}",
                line,
                words.len(),
                T::words_count()
            ));
        }
        T::read_words(&words)
    }
}
impl<T: Readable> ReadableFromLine for Vec<T> {
    type Output = Vec<T::Output>;
    fn read_line(line: &str) -> Result<Self::Output, String> {
        let n = T::words_count();
        let words = split_into_words(line);
        if words.len() % n != 0 {
            return Err(format!(
                "line \"{}\" has {} words, expected multiple of {}",
                line,
                words.len(),
                n
            ));
        }
        let mut result = Vec::new();
        for chunk in words.chunks(n) {
            match T::read_words(chunk) {
                Ok(v) => result.push(v),
                Err(msg) => {
                    let flagment_msg = if n == 1 {
                        format!("word {}", result.len())
                    } else {
                        let l = result.len();
                        format!("words {}-{}", n * l + 1, (n + 1) * l)
                    };
                    return Err(format!("{} of line \"{}\": {}", flagment_msg, line, msg));
                }
            }
        }
        Ok(result)
    }
}
impl<T: Readable, U: Readable> ReadableFromLine for (T, Vec<U>) {
    type Output = (T::Output, <Vec<U> as ReadableFromLine>::Output);
    fn read_line(line: &str) -> Result<Self::Output, String> {
        let n = T::words_count();
        #[allow(deprecated)]
        let trimmed = line.trim_right_matches('\n');
        let words_and_rest: Vec<&str> = trimmed.splitn(n + 1, ' ').collect();
        if words_and_rest.len() < n {
            return Err(format!(
                "line \"{}\" has {} words, expected at least {}",
                line,
                words_and_rest.len(),
                n
            ));
        }
        let words = &words_and_rest[..n];
        let empty_str = "";
        let rest = words_and_rest.get(n).unwrap_or(&empty_str);
        Ok((T::read_words(words)?, Vec::<U>::read_line(rest)?))
    }
}
macro_rules ! impl_readable_from_line_for_tuples_with_vec { ( $ t : ident $ var : ident ) => ( ) ; ( $ t : ident $ var : ident ; $ ( $ inner_t : ident $ inner_var : ident ) ;+ ) => { impl_readable_from_line_for_tuples_with_vec ! ( $ ( $ inner_t $ inner_var ) ;+ ) ; impl <$ t : Readable , $ ( $ inner_t : Readable ) ,+ , U : Readable > ReadableFromLine for ( $ t , $ ( $ inner_t ) ,+ , Vec < U > ) { type Output = ( $ t :: Output , $ ( $ inner_t :: Output ) ,+ , Vec < U :: Output > ) ; fn read_line ( line : & str ) -> Result < Self :: Output , String > { let mut n = $ t :: words_count ( ) ; $ ( n += $ inner_t :: words_count ( ) ; ) + # [ allow ( deprecated ) ] let trimmed = line . trim_right_matches ( '\n' ) ; let words_and_rest : Vec <& str > = trimmed . splitn ( n + 1 , ' ' ) . collect ( ) ; if words_and_rest . len ( ) < n { return Err ( format ! ( "line \"{}\" has {} words, expected at least {}" , line , words_and_rest . len ( ) , n ) ) ; } let words = & words_and_rest [ .. n ] ; let empty_str = "" ; let rest = words_and_rest . get ( n ) . unwrap_or ( & empty_str ) ; let ( $ var , $ ( $ inner_var ) ,* ) = < ( $ t , $ ( $ inner_t ) ,+ ) >:: read_words ( words ) ?; Ok ( ( $ var , $ ( $ inner_var ) ,* , Vec ::< U >:: read_line ( rest ) ? ) ) } } } ; }
impl_readable_from_line_for_tuples_with_vec ! ( T4 t4 ; T3 t3 ; T2 t2 ; T1 t1 ) ;
pub fn read<T: ReadableFromLine>() -> T::Output {
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).unwrap();
    T::read_line(&line).unwrap()
}
#[macro_export]
macro_rules ! read { ( ) => { let mut line = String :: new ( ) ; std :: io :: stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; } ; ( $ pat : pat = $ t : ty ) => { let $ pat = read ::<$ t > ( ) ; } ; ( $ ( $ pat : pat = $ t : ty ) ,+ ) => { read ! ( ( $ ( $ pat ) ,* ) = ( $ ( $ t ) ,* ) ) ; } ; }
#[macro_export]
macro_rules ! readls { ( $ ( $ pat : pat = $ t : ty ) ,+ ) => { $ ( read ! ( $ pat = $ t ) ; ) * } ; }
pub fn readx<T: ReadableFromLine>() -> Vec<T::Output> {
    use std::io::{self, BufRead};
    let stdin = io::stdin();
    let result = stdin
        .lock()
        .lines()
        .map(|line_result| {
            let line = line_result.expect("read from stdin failed");
            T::read_line(&line).unwrap()
        }).collect();
    result
}
#[macro_export]
macro_rules ! readx_loop { ( |$ pat : pat = $ t : ty | $ body : expr ) => { use std :: io :: BufRead ; let stdin = std :: io :: stdin ( ) ; for line in stdin . lock ( ) . lines ( ) { let line = line . expect ( "read from stdin failed" ) ; let $ pat = <$ t >:: read_line ( & line ) . unwrap ( ) ; $ body } } ; ( |$ ( $ pat : pat = $ t : ty ) ,*| $ body : expr ) => { readx_loop ! ( | ( $ ( $ pat ) ,* ) = ( $ ( $ t ) ,* ) | $ body ) ; } ; }
pub fn readn<T: ReadableFromLine>(n: usize) -> Vec<T::Output> {
    use std::io::{self, BufRead};
    let stdin = io::stdin();
    let result: Vec<T::Output> = stdin
        .lock()
        .lines()
        .take(n)
        .map(|line_result| {
            let line = line_result.expect("read from stdin failed");
            T::read_line(&line).unwrap()
        }).collect();
    if result.len() < n {
        panic!(
            "expected reading {} lines, but only {} lines are read",
            n,
            result.len()
        );
    }
    result
}
#[macro_export]
macro_rules ! readn_loop { ( $ n : expr , |$ pat : pat = $ t : ty | $ body : expr ) => { use std :: io :: BufRead ; let stdin = std :: io :: stdin ( ) ; { let mut lock = stdin . lock ( ) ; for _ in 0 ..$ n { let mut line = String :: new ( ) ; lock . read_line ( & mut line ) . expect ( "read from stdin failed" ) ; let $ pat = <$ t >:: read_line ( & line ) . unwrap ( ) ; $ body } } } ; ( $ n : expr , |$ ( $ pat : pat = $ t : ty ) ,*| $ body : expr ) => { readn_loop ! ( $ n , | ( $ ( $ pat ) ,* ) = ( $ ( $ t ) ,* ) | $ body ) ; } ; }
pub trait Words {
    fn read<T: Readable>(&self) -> T::Output;
}
impl<'a> Words for [&'a str] {
    fn read<T: Readable>(&self) -> T::Output {
        T::read_words(self).unwrap()
    }
}
impl<'a> Words for &'a str {
    fn read<T: Readable>(&self) -> T::Output {
        T::read_words(&[self]).unwrap()
    }
}

#[derive(Clone, PartialEq, Eq)]
pub enum ListInner<T: Clone> {
    Nil,
    Cons(T, List<T>),
}
pub use self::ListInner::{Cons, Nil};
#[derive(Clone, PartialEq, Eq)]
pub struct List<T: Clone> {
    inner: std::rc::Rc<ListInner<T>>,
    len: usize,
}
impl<T: Clone> List<T> {
    pub fn nil() -> List<T> {
        List {
            inner: std::rc::Rc::new(Nil),
            len: 0,
        }
    }
    pub fn is_nil(&self) -> bool {
        match self.as_ref() {
            &Nil => true,
            &Cons(_, _) => false,
        }
    }
    pub fn len(&self) -> usize {
        self.len
    }
    pub fn head(&self) -> T {
        match self.as_ref() {
            &Nil => panic!("cannot find head of nil"),
            &Cons(ref head, _) => head.clone(),
        }
    }
    pub fn tail(&self) -> List<T> {
        match self.as_ref() {
            &Nil => panic!("cannot find head of nil"),
            &Cons(_, ref tail) => tail.clone(),
        }
    }
    pub fn iter(&self) -> ListIter<T> {
        ListIter { iter: self.clone() }
    }
    pub fn rev(&self) -> List<T> {
        fn go<T: Clone>(list: &List<T>, acc: List<T>) -> List<T> {
            match list.as_ref() {
                &Nil => acc,
                &Cons(ref head, ref tail) => go(tail, head.clone().cons(acc)),
            }
        }
        go(self, List::nil())
    }
    pub fn append(&self, other: &List<T>) -> List<T> {
        fn go<T: Clone>(list1: &List<T>, list2: &List<T>, list1_rev: List<T>) -> List<T> {
            match list1.as_ref() {
                &Nil => list1_rev.rev_append(list2),
                &Cons(ref head, ref tail) => go(tail, list2, head.clone().cons(list1_rev)),
            }
        }
        go(self, other, List::nil())
    }
    pub fn rev_append(&self, other: &List<T>) -> List<T> {
        match self.as_ref() {
            &Nil => other.clone(),
            &Cons(ref head, ref tail) => tail.rev_append(&head.clone().cons(other)),
        }
    }
    #[cfg(test)]
    fn take(self) -> std::rc::Rc<ListInner<T>> {
        self.inner
    }
}
impl<T: Clone> AsRef<ListInner<T>> for List<T> {
    fn as_ref(&self) -> &ListInner<T> {
        self.inner.as_ref()
    }
}
impl<T: Clone + std::fmt::Debug> std::fmt::Debug for List<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self.inner.as_ref() {
            &Nil => write!(f, "nil"),
            &Cons(ref head, ref tail) => write!(f, "{:?} :: {:?}", head, tail),
        }
    }
}
impl<T: Clone + PartialOrd> PartialOrd for List<T> {
    fn partial_cmp(&self, other: &List<T>) -> Option<std::cmp::Ordering> {
        use std::cmp::Ordering::*;
        match (self.as_ref(), other.as_ref()) {
            (&Nil, &Nil) => Some(Equal),
            (&Nil, &Cons(_, _)) => Some(Less),
            (&Cons(_, _), &Nil) => Some(Greater),
            (&Cons(ref head1, ref tail1), &Cons(ref head2, ref tail2)) => head1
                .partial_cmp(&head2)
                .and_then(|ordering| match ordering {
                    Equal => tail1.partial_cmp(tail2),
                    _ => Some(ordering),
                }),
        }
    }
}
impl<T: Clone + Ord> Ord for List<T> {
    fn cmp(&self, other: &List<T>) -> std::cmp::Ordering {
        use std::cmp::Ordering::*;
        match (self.as_ref(), other.as_ref()) {
            (&Nil, &Nil) => Equal,
            (&Nil, &Cons(_, _)) => Less,
            (&Cons(_, _), &Nil) => Greater,
            (&Cons(ref head1, ref tail1), &Cons(ref head2, ref tail2)) => match head1.cmp(&head2) {
                Equal => tail1.cmp(tail2),
                determined => determined,
            },
        }
    }
}
pub struct ListIter<T: Clone> {
    iter: List<T>,
}
impl<T: Clone> Iterator for ListIter<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        let cons;
        match self.iter.as_ref() {
            &Nil => return None,
            &Cons(ref head, ref tail) => cons = (head.clone(), tail.clone()),
        }
        self.iter = cons.1;
        Some(cons.0)
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.iter.len(), Some(self.iter.len()))
    }
    fn count(self) -> usize {
        self.iter.len()
    }
}
impl<T: Clone> ExactSizeIterator for ListIter<T> {}
impl<T: Clone> IntoIterator for List<T> {
    type Item = T;
    type IntoIter = ListIter<T>;
    fn into_iter(self) -> ListIter<T> {
        ListIter { iter: self }
    }
}
impl<'a, T: Clone> IntoIterator for &'a List<T> {
    type Item = T;
    type IntoIter = ListIter<T>;
    fn into_iter(self) -> ListIter<T> {
        self.iter()
    }
}
impl<T: Clone> std::iter::FromIterator<T> for List<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> List<T> {
        let xs: Vec<T> = iter.into_iter().collect();
        let mut list = List::nil();
        for x in xs.into_iter().rev() {
            list = x.cons(list);
        }
        list
    }
}
pub trait IntoCons<T: Clone, L: std::borrow::Borrow<List<T>>> {
    fn cons(self, tail: L) -> List<T>;
}
impl<T: Clone, L: std::borrow::Borrow<List<T>>> IntoCons<T, L> for T {
    fn cons(self, tail: L) -> List<T> {
        let tail_cloned: List<T> = tail.borrow().clone().into();
        let tail_len = tail_cloned.len;
        List {
            inner: std::rc::Rc::new(Cons(self, tail_cloned)),
            len: tail_len + 1,
        }
    }
}
#[macro_export]
macro_rules ! list { [ ] => { List :: nil ( ) } ; [ $ head : expr ] => { $ head . cons ( List :: nil ( ) ) } ; [ $ head : expr , $ ( $ tail : expr ) ,* ] => { $ head . cons ( list ! [ $ ( $ tail ) ,* ] ) } ; [ $ head : expr , $ ( $ tail : expr ) ,+ , ] => { list ! [ $ head , $ ( $ tail ) ,* ] } ; }

pub trait PrimitiveInteger {
    fn abs_diff(self, rhs: Self) -> Self;
    fn rem_euclid(self, rhs: Self) -> Self;
}
macro_rules ! impl_primitive_integer { ( $ ( $ t : ty ) * ) => { $ ( impl PrimitiveInteger for $ t { fn abs_diff ( self , rhs : $ t ) -> $ t { if self < rhs { rhs - self } else { self - rhs } } # [ allow ( unused_comparisons ) ] fn rem_euclid ( self , rhs : $ t ) -> $ t { let r = self % rhs ; if r < 0 { if rhs < 0 { r - rhs } else { r + rhs } } else { r } } } ) * } }
impl_primitive_integer ! ( u8 u16 u32 u64 usize i8 i16 i32 i64 isize ) ;
pub trait PrimitiveUnsigned {
    fn ceil_div(self, rhs: Self) -> Self;
    fn round_div(self, rhs: Self) -> Self;
}
macro_rules ! impl_primitive_unsigned { ( $ ( $ t : ty ) * ) => { $ ( impl PrimitiveUnsigned for $ t { fn ceil_div ( self , rhs : $ t ) -> $ t { ( self + rhs - 1 ) / rhs } fn round_div ( self , rhs : $ t ) -> $ t { ( self + rhs / 2 ) / rhs } } ) * } }
impl_primitive_unsigned ! ( u8 u16 u32 u64 usize ) ;

const MOD: u64 = 1_000_000_007;

fn solve(n: &List<u8>, d: u8, rem: u8, tight: bool, memo: &mut [Vec<Vec<Option<u64>>>]) -> u64 {
    memo[tight as usize][rem as usize][n.len()].unwrap_or_else(|| {
        match n.as_ref() {
            &Nil => if rem == 0 { 1 } else { 0 },
            &Cons(ref hd, ref tl) => {
                let max = if tight { *hd } else { 9 };
                let ans = (0..max+1).map(|i| {
                    solve(tl, d, (rem + i) % d, tight && i == *hd, memo)
                }).sum::<u64>() % MOD;
                memo[tight as usize][rem as usize][n.len()] = Some(ans);
                ans
            }
        }
    })
}

fn main() {
    read!(d = u8);
    read!(n = String);
    let n: List<u8> = n.chars().map(|c| c.to_digit(10).unwrap() as u8).collect();
    println!("{}", solve(&n, d, 0, true, &mut vec![vec![vec![None; n.len() + 1]; d as usize]; 2]) - 1);
}

Submission Info

Submission Time
Task E - 数
User avtomat
Language Rust (1.15.1)
Score 4
Code Size 18184 Byte
Status AC
Exec Time 166 ms
Memory 39292 KB

Judge Result

Set Name All
Score / Max Score 4 / 4
Status
AC × 5
Set Name Test Cases
All 00, 01, 02, 90, 91
Case Name Status Exec Time Memory
00 AC 3 ms 4352 KB
01 AC 101 ms 27004 KB
02 AC 166 ms 39292 KB
90 AC 2 ms 4352 KB
91 AC 2 ms 4352 KB