When is the Euler-Lagrange equation trivially satisfied? In calculus of variations, we seek the functions $y(x)$ extremising the quantity $$I = \int_{x_1}^{x_2} F(x, y, y') \, dx.$$ To find these functions, we solve the Euler-Lagrange equation $$\frac{\partial F}{\partial y} = \frac{d}{dx}\left[\frac{\partial F}{\partial y'} \right].$$
Sometimes the E-L equation is trivially satisfied. This means that given boundary conditions $y(x_1) = y_1$ and $y(x_2)=y_2$, all functions $y(x)$ are extremals, i.e. $I$ is independent of the path $y$.
In such a situation, I understand that the Lagrangian $F$ simplifies to a total derivative. Here are two examples:
1) $I = \int_{x_1}^{x_2} (y^2 + 2xyy') dx$. We compute $\frac{\partial F}{\partial y} = 2y + 2xy'$ and $\frac{d}{dx} \left[ \frac{\partial F}{\partial y'} \right] = \frac{d}{dx} \left[ 2xy \right] = 2y + 2xy'$. We then show path-independence by writing $I = \int_{x_1}^{x_2} (y^2 + 2xyy') dx = \int_{x_1}^{x_2} \frac{d}{dx} \left[ xy^2 \right] dx = \left[ xy^2 \right]_{x_1}^{x_2}$.
2) $I = \int_{x_1}^{x_2} (\frac{y'}{y}) dx$. We compute $\frac{\partial F}{\partial y} = -\frac{y'}{y^2}$ and $\frac{d}{dx} \left[ \frac{\partial F}{\partial y'} \right] = \frac{d}{dx} \left[ \frac{1}{y} \right] = -\frac{y'}{y^2}$. We then show path-independence by writing $I = \int_{x_1}^{x_2} \frac{y'}{y} dx = \int_{x_1}^{x_2} \frac{d}{dx} \left[ \log |y| \right] dx = \left[ \log |y| \right]_{x_1}^{x_2}$.
I want to know how to figure out what the Lagrangian is the total derivative of. In the two examples above I got the total derivatives by trial and error, but I would like to know what the answer is in general.
 A: I) A Lagrangian is a section in a jet bundle.
A Lagrangian is a null-Lagrangian, if its Euler-Lagrange (EL) derivative vanishes identically.  
There is a so-called algebraic Poincare Lemma which states that if the fiber space is contractible, then all null-Lagrangians are total divergences.
II) More explicitly, for the first jetbundle over a 1D basemanifold, the Lagrangian $L$ is a function $(t,q,v)\to L(t,q,v)$. Define momentum 
$$p~:=~\frac{\partial L}{\partial v}.\tag{A}$$ 
Assume $L$ is a null Lagrangian. We want to find $F$ such that$^1$ 
$$L~=~\frac{dF}{dt}.\tag{B}$$ 
Consider a homotopy 
$$ \widetilde{q}(s,t)~=~s (q(t)-q_{\ast})+q_{\ast}, \qquad \widetilde{v}(s,t)~=~s v(t), \qquad s~\in~[0,1],\tag{C}$$
where $q_{\ast}$ is a fiducial position point in the fiber. 
Then a homotopy formula for $F$ is 

$$F~=~ \int_0^1\! ds ~(q(t)-q_{\ast})~ p\left(t,~s (q(t)-q_{\ast})+q_{\ast},~ s v(t)\right)+ \int^t_{t_0}\!dt^{\prime} ~L(t^{\prime},q_{\ast},0).\tag{D}$$ 

Proof: Let us define the shorthand notation
$$ \widetilde{L}~:=~L\left(t,~s (q(t)-q_{\ast})+q_{\ast},~ s v(t)\right), \qquad \widetilde{p}~:=~p\left(t,~s (q(t)-q_{\ast})+q_{\ast},~ s v(t)\right), \qquad \text{etc}. \tag{E}$$
Then
$$ \frac{d\widetilde{L}}{ds}~=~(q-q_{\ast})\frac{\partial \widetilde{L}}{\partial \widetilde{q}}+ v \widetilde{p}~=~\frac{d}{dt} \left[(q-q_{\ast})\widetilde{p}\right].\tag{F}$$
In the last equality of eq. (F) we used that the Euler-Lagrange derivative vanishes identically. Hence
$$ L(t,q,v)-L(t,q_{\ast},0)~=~\int_0^1\! ds~\frac{d\widetilde{L}}{ds}
~\stackrel{(F)}{=}~\frac{d}{dt}  \int_0^1\! ds~(q-q_{\ast})\widetilde{p}.\tag{G}$$ 
On the other hand, let us differentiate the homotopy formula (D):
$$\frac{dF}{dt}
~\stackrel{(D)}{=}~\frac{d}{dt}  \int_0^1\! ds~(q-q_{\ast})\widetilde{p}+L(t,q_{\ast},0).\tag{H}$$ 
Comparison of eqs. (G) & (H) now yields eq. (B). 
$\Box$
OP's examples:


*

*$L= q^2 + 2tqv.$ Then momentum is $p=2tq$. Let $q_{\ast}=0$. Then the homotopy formula (D) yields $F=tq^2$.

*$L= \frac{v}{q}.$ Then momentum is $p=\frac{1}{q}$. The position space $\mathbb{R}\backslash\{0\}$ is not contractible, so let us consider a connected component, say $\mathbb{R}_+$. Choose fiducial position point $q_{\ast}=1$. Then the homotopy formula (D) yields  $F=\ln|q|$.
References:


*

*P.J. Olver, Applications of Lie Groups to Differential Equations, 1986.


Related Math.SE posts: 


*

*Representation for function ("null-Lagrangian")

*If an Euler-Lagrange equation is satisfied by all diffeomorphisms, is it Null-Lagrangian?
--
$^1$ Adequate prolongations are implicitly implied in various places of this answer. 
