Solution (source code)

= Solution

The invariant is <phase-space volume>, or area $dq\,dp$ for one degree of freedom. It does not require an autonomous <Hamiltonian>. The flow vector $v=(H_p,-H_q)$ has <divergence> $\partial_qH_p-\partial_pH_q=0$ whenever the mixed <derivatives> agree. If $M(t)$ is the Jacobian of the flow with respect to initial coordinates, then $\dot M=Dv\,M$. Differentiating its <determinant> gives
$$
\frac{d}{dt}\det M=(\nabla\cdot v)\det M=0,\qquad\det M(0)=1.
$$
Thus the change-of-variables formula shows that any region carried by the flow retains its area. This proves <Liouville theorem in Hamiltonian mechanics> even when $H=H(q,p,t)$ depends on time.