use std::{collections::HashMap, hash::Hash}; use proconio::input; fn main() { input! { p: usize, } let solve = |p| { let mut v = vec![b'0'; p + 1]; v[0] = b'1'; count(Or(MultipleOf(3), Seen(b'3')), p, b'0'..=b'9') }; println!("{}", solve(p) - solve(0)); } fn count(dfa: A, len: usize, sigma: impl Iterator + Clone) -> u64 where A: Dfa, A::State: Eq + Hash, { let mut dp = HashMap::new(); dp.insert(dfa.init(), 1); for _ in 0..len { let mut ndp = HashMap::new(); for (q, v) in dp { for c in sigma.clone() { *ndp.entry(dfa.next(&q, &c)).or_insert(0) += v; } } dp = ndp; } let mut res = 0; for (q, v) in dp { if dfa.accept(&q) { res += v; } } res } trait Dfa { type State; type Alphabet; fn init(&self) -> Self::State; fn next(&self, q: &Self::State, c: &Self::Alphabet) -> Self::State; fn accept(&self, q: &Self::State) -> bool; } struct Or(A, B); impl> Dfa for Or { type State = (A::State, B::State); type Alphabet = A::Alphabet; fn init(&self) -> Self::State { (self.0.init(), self.1.init()) } fn next(&self, q: &Self::State, c: &Self::Alphabet) -> Self::State { (self.0.next(&q.0, c), self.1.next(&q.1, c)) } fn accept(&self, q: &Self::State) -> bool { self.0.accept(&q.0) || self.1.accept(&q.1) } } struct MultipleOf(u64); impl Dfa for MultipleOf { type State = u64; type Alphabet = u8; fn init(&self) -> Self::State { 0 } fn next(&self, q: &Self::State, c: &Self::Alphabet) -> Self::State { (q * 10 + (c - b'0') as u64) % self.0 } fn accept(&self, q: &Self::State) -> bool { *q == 0 } } struct Seen(u8); impl Dfa for Seen { type State = bool; type Alphabet = u8; fn init(&self) -> Self::State { false } fn next(&self, q: &Self::State, c: &Self::Alphabet) -> Self::State { *q || *c == self.0 } fn accept(&self, q: &Self::State) -> bool { *q } }