hand Brute force try answer, don't forget case p=0