#[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; } struct Prod { dfa_a: A, dfa_b: B, } impl, B: DFA> DFA for Prod { fn size(&self) -> usize { self.dfa_a.size() * self.dfa_b.size() } fn trans(&self, state: usize, char: S) -> Option { let w = self.dfa_b.size(); let (x, y) = (state / w, state % w); if let Some(to1) = self.dfa_a.trans(x, char) { if let Some(to2) = self.dfa_b.trans(y, char) { return Some(to1 * w + to2); } } None } fn init(&self) -> Vec { let w = self.dfa_b.size(); let b_init = self.dfa_b.init(); let mut ans = vec![]; for av in self.dfa_a.init() { for &bv in &b_init { ans.push(av * w + bv); } } ans } fn is_final_state(&self, state: usize) -> bool { let w = self.dfa_b.size(); let (x, y) = (state / w, state % w); self.dfa_a.is_final_state(x) && self.dfa_b.is_final_state(y) } } 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: S) -> 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 } } struct DeltaZero(usize); impl DFA for DeltaZero { fn size(&self) -> usize { 2 * self.0 } fn trans(&self, delta: usize, c: i32) -> Option { let delta = delta as i32; if delta + c < 0 || delta + c >= 2 * self.0 as i32 { return None; } Some((delta + c) as usize) } fn init(&self) -> Vec { vec![self.0] } fn is_final_state(&self, state: usize) -> bool { state == self.0 } } fn dfa_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 } // TODO: subopt 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 = dfa_dp(Prod { dfa_a: GtZero, dfa_b: DeltaZero(2 * N) }, Add, &alpha, &dig); println!("{}", dp[0] + dp[1]); }