<s>[INST] Integrate $\int {\dfrac{x^2+\left(n-1\right)n}{\left(x\sin x+n\cos x\right)^2}}dx $ 
$$\int {\dfrac{x^2+\left(n-1\right)n}{\left(x\sin\left(x\right)+n\cos\left(x\right)\right)^2}}dx $$

My Try:
I multiple $x^{2n-2}$ to both N and D, then took D as $u$ and then solved to get $\dfrac{n\sin\left(x\right)-x\cos\left(x\right)}{x\sin\left(x\right)+n\cos\left(x\right)} + C$ as answer.
My teacher told that this would have been much easier if we had applied linearity and written question as 
$={\displaystyle\int}\dfrac{x\sin\left(x\right)+\left(n-1\right)\cos\left(x\right)}{x\sin\left(x\right)+n\cos\left(x\right)}\,\mathrm{d}x-{\displaystyle\int}\dfrac{\left(\left(1-n\right)\sin\left(x\right)+x\cos\left(x\right)\right)\left(n\sin\left(x\right)-x\cos\left(x\right)\right)}{\left(x\sin\left(x\right)+n\cos\left(x\right)\right)^2}\,\mathrm{d}x$
I didn't get it, how did we write the above statement? I mean, please explain the method or steps for reaching to this part.
 [/INST] Use shorthands $s= \sin x$ and $c=\cos x$ below. Decompose the integrand with $s^2+c^2 = 1$
\begin{align}
 & {\dfrac{x^2+(n-1)n}{(x\sin x+n\cos x)^2}} \\
=&\frac{x^2+(n-1)n}{(xs+nc)^2} 
= \frac{x^2(s^2+c^2)+(n-1)n(s^2+c^2)}{(xs+nc)^2} \\
=& \frac{[xs+(n-1)c](xs+nc)-[(1-n)xs+xc](ns-xc) }{(xs+nc)^2}\\
= & \frac{xs+(n-1)c}{xs+nc}-
\dfrac{[(1-n)xs+xc](ns-xc)}{\left(xs+nc\right)^2} \\
= &\dfrac{x\sin x+(n-1)\cos x} {x\sin x+n\cos x}-\dfrac{[(1-n)\sin x+x\cos x](n\sin x-x\cos x)}{(x\sin x+n\cos x)^2} \\
= & \frac{d}{dx}\left( \dfrac{n\sin x-x\cos x}{x\sin x+n\cos x} \right) \\
\end{align}
Thus
$$\int {\dfrac{x^2+(n-1)n}{(x\sin x+n\cos x)^2}} 
= \dfrac{n\sin x-x\cos x}{x\sin x+n\cos x} +C$$
</s>