Microscopic mechanism of the tunable band gap in potassium-doped few-layer black phosphorus