macro_rules! input { ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes.by_ref().map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr,) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ($(read_value!($next, $t)),*) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::>() }; ($next:expr, usize1) => (read_value!($next, usize) - 1); ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); read_value!($next, [$t; len]) }}; ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error")); } fn main() { input! { a: String, b: String, } const MOD: i32 = 1_000_000_007; println!("{}", (digit_dp(b, false) - digit_dp(a, true) + MOD) % MOD); } fn digit_dp(s: String, less_only: bool) -> i32 { const MOD: i32 = 1_000_000_007; let mut ds = vec![]; for c in s.chars() { let n = c as i32 - 48; ds.push(n); } let mut dp = vec![vec![vec![vec![vec![0; 8]; 3]; 2]; 2]; ds.len() + 1]; // dp[i][is_less][has_3][mod3][mod8] dp[0][0][0][0][0] = 1; for i in 0..ds.len() { for j in 0..=1 { for k in 0..=1 { for m in 0..3 { for n in 0..8 { let lim = if j == 1 { 9 } else { ds[i] }; for d in 0..=lim { let is_less: usize = if j == 1 || d < lim { 1 } else { 0 }; let has_3: usize = if k == 1 || d == 3 { 1 } else { 0 }; let mod_3: usize = (m + d as usize) % 3; let mod_8: usize = (n * 10 + d as usize) % 8; dp[i + 1][is_less][has_3][mod_3][mod_8] += dp[i][j][k][m][n]; dp[i + 1][is_less][has_3][mod_3][mod_8] %= MOD; } } } } } } let mut answer = 0; for j in 0..=1 { if less_only && j == 0 { continue; } for k in 0..=1 { for m in 0..=2 { for n in 0..8 { if (k == 1 || m == 0) && n != 0 { answer += dp[ds.len()][j][k][m][n]; answer %= MOD; } } } } } answer }