p-adic affine dynamical systems and applications