$$ k = \lfloor \frac{p}{i} \rfloor, j = p \bmod i $$
$$ p = ki + j $$
$$ ki + j \equiv 0 \pmod p $$
$$ kj ^ {-1} + i ^ {-1} \equiv 0 \pmod p $$
$$ i ^ {-1} \equiv -kj ^ {-1} \pmod p $$
$$ i ^ {-1} \equiv -\lfloor \frac{p}{i} \rfloor (p \bmod i) ^ {-1} \pmod p $$
$$ i ^ {-1} \equiv p - \lfloor \frac{p}{i} \rfloor (p \bmod i) ^ {-1} \pmod p $$