Efficient adjoint methods for steady and unsteady flow optimization