Why my code is failing at one test case

i tried this with simple brute force and also with the equation given in the geekforgeek for this problem my solution is on https://ide.codingblocks.com/s/216446

code produces wrong answer for p = 1