#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 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, [graph1; $len:expr]) => {{ let mut g = vec![vec![]; $len]; let ab = read_value!($next, [(usize1, usize1)]); for (a, b) in ab { g[a].push(b); g[b].push(a); } g }}; ($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")); } // ref: https://kuretchi.github.io/blog/entries/automaton-dp/ /// An (almost) DFA. trans is allowed to return None. /// S: alphabet (the set consisting of letters) trait DFA { fn size(&self) -> usize; fn trans(&self, state: usize, char: S) -> Option; fn init(&self) -> Vec; fn is_final_state(&self, state: usize) -> bool; } trait ActionMonoid { type T; fn add(&self, x: Self::T, y: Self::T) -> Self::T; fn act(&self, x: Self::T, letter: S) -> Self::T; } struct Add; impl ActionMonoid for Add { type T = i64; fn add(&self, x: i64, y: i64) -> i64 { x + y } fn act(&self, x: i64, _y: i32) -> i64 { x } } const N: usize = 30; struct GtZero; impl DFA for GtZero { fn size(&self) -> usize { 8 * N } fn trans(&self, st: usize, c: i32) -> Option { let (delta, zero) = ((st / 2) as i32, st % 2); if zero == 1 && c < 0 { return None; } if delta + c < 0 || delta + c >= 4 * N as i32 { return None; } Some(2 * (delta + c) as usize + (zero & if c == 0 { 1 } else { 0 })) } fn init(&self) -> Vec { vec![4 * N + 1] } fn is_final_state(&self, state: usize) -> bool { state == 4 * N } } fn digital_dp, M: ActionMonoid>( dfa: A, monoid: M, alpha: &[i32], a: &[i32] ) -> [M::T; 2] { let n = dfa.size(); let len = a.len(); let init = dfa.init(); let mut dp = vec![vec![[0; 2]; n]; len + 1]; for &v in &init { dp[0][v][1] = 1; } for i in 0..len { for j in 0..n { for eq in 0..2 { let val = dp[i][j][eq]; for &c in alpha { if eq == 1 && c > a[i] { continue; } if let Some(to) = dfa.trans(j, c) { dp[i + 1][to][eq & if c == a[i] { 1 } else { 0 }] += monoid.act(val, c); } } } } } let mut ans = [0; 2]; for i in 0..n { if dfa.is_final_state(i) { for j in 0..2 { ans[j] += dp[len][i][j]; } } } ans } fn main() { input!(n: i64); const N: usize = 30; let alpha = [-2, -1, 0, 1, 2]; let mut dig = vec![0; N]; let mut v = n; for i in 0..N { let mut r = v % 5; if r >= 3 { r -= 5; } dig[N - 1 - i] = r as i32; v = (v - r) / 5; } let dp = digital_dp(GtZero, Add, &alpha, &dig); println!("{}", dp[0] + dp[1]); }