Problem #374
Equation Cascade
Let f : \mathbb{Z}_{\ge0} \rightarrow \mathbb{Z}_{\ge0} be a function satisfying the functional equations: \ f(3x) = f(x), and f(9x+3a+b)=(a+3b) \cdot f(3x+1)-(a+3b-1) \cdot f(x)\ \forall a \in {0,\ 1,\ 2}, b \in {1,\ 2}, a+x > 0. Let f(0) = 0,\ f(1) = 1,\ f(2) = 2 and f(4) = 4.
Let g(x)=f(x)-1\ \forall\ x>0. Find the number of values between 1 and 3^{30} for which f(g^{6}(x))=40. (Here, g^{2}(x)=g(g(x))\ne(g(x))^{2}).