Problem Link :- Sell The Candies
My Approach :-
Let us say that Alice buys r1 candies and Jhon buys r2 candies. Than for a valid distribution r1x + r2 y = c . Now for r1 equals to 0 to n we will find r2 using this equation r2 = (c - r1*x)/y. A valid distribution will be achieved when r2 is an integer and r1+r2 <= n. Total no of valid distribution will be our answer.
My Code :-
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
//write your code here
int t ; cin>>t;
for(int i=0;i<t;i++)
{
ll count=0;
ll n,c,x,y; cin>>n>>c>>x>>y;
for(int r=0;r<=n;r++)
{
ll z=-1;
if((c-x*r)<0)
break;
if((c-x*r)%y==0)
{
z=(c-x*r)/y;
}
if(z>=0)
{
if(r+z<=n)
{
count++;
// cout<<r<<" "<<z<<endl;
}
}
}
cout<<count<<endl;
}
return 0;
}