HackerEarth Code Apocalypse 3.0 Coming SOON !Easy-Medium problem solution

In this HackerEarth Code Apocalypse 3.0 ..Coming SOON ![Easy-Medium] problem solution Cypher guys are ready for the grand event Code Apocalypse!!.
Questions are prepared, Promotions just begun ( from this question, LOL! ). But there is one problem they are facing. What should they do if there is a tie on the final day.
To solve this dilemma, Ayush came up with a tie breaker problem (although this can't be disclosed), give a try to solve it :

You have an army of N unsullied soldiers, you can upgrade a soldier to be stronger by feeding him X magical apples (found beyond the wall!). Initially, you have M magical apples.
You can buy extra magical apples ( from the Iron Bank! :P ), by selling one of your soldiers for Y apples.
Calculate what is maximum number of upgraded soldiers you can have for the long night.

HackerEarth Code Apocalypse 3.0 ..Coming SOON ![Easy-Medium] problem solution.

`#include<bits/stdc++.h>#define ll long long intusing namespace std ;int main(){ios_base::sync_with_stdio(false) ;cin.tie(NULL) ;cout.tie(NULL) ;ll t ; cin >> t ;while(t--){ll n , m , x ,y ; cin >> n >> m >> x >> y ;ll l = 0 , r = n , mid , ans ; while( l <= r ){mid = l + (r-l)/2 ; ll temp = m+mid*y ;ll g = temp/x ;if( g >= n-mid ){ans = mid ;r = mid-1 ; }else{l = mid+1 ;} }cout << n-ans <<"\n" ;}return 0 ;}`