I have linear nonhomogeneous reccurence equation:
$$pa_n-a_{n=1}+(1-p)a_{n-2}+1=0$$
I solve homogeneous equation first and I get: $a_n=A+B(\frac{1-p}{p})^n$
Now I want to guess particular solution, so I try constant one $K$ because nonhomogeneous term is constant, but
$$pK-K+(1-p)K+1=1$$
Why constant solution does not work? How to solve this reccurence relation?
A constant doesn't work because it is a solution to the homogeneous equation. For this specific equation, one can find a particular solution in the form $a_n=Cn$, where $C$ is a constant to be determined: $$ pCn-C(n-1)+(1-p)C(n-2)+1=0\implies C=\frac{1}{1-2p}. $$
Remark: I have assumed that $p\neq\frac{1}{2}$. If $p=\frac{1}{2}$, even the solution to the homogeneous equation must be modified. The general solution to the recurrence equation, in this case, is $$ a_n=A+Bn-n^2\qquad\left(p=\frac{1}{2}\right). $$