@amitqy hey your logic is not correct,use this logic ,its simply maths:
Our task is to calculate the number of days needed to make the dollar amount to at least X in the bank account.
The first obvious case is if amount of dollar we are interested for - X is less than or equal to initial dollar ( D ) , then we do not need to wait for further days. So, the answer will be 0 in this case.
In other cases, If you think straight forward bruteforce apporach then, it would be taking initial summation as D, keep on adding value A for first M month and add value B on M+1 th day and repeat similar scenario again untill we reach amount greater than or equal to X. This will not definitely set into our time limit as there are lots of testcases.
For better approach , we will first subtract D from X which makes our calculation easier. Now, we can first take out every (M+1) days segments in a chunks. So, we need (MA+B) amount of money in a particular chunk. To find total such chunt needed , we can divide current X by (MA+B) and take integer part. the remaining amount for making X will be will be filled up using amount A.
Also for reference here is code with comments: