 Not understanding the logic of this math problem

[average age](https://www.codechef.com/CWCR2019/problems/AVGAGE)

ll a,b,c;
cin>>a>>b>>c;
ll num=c-a,
den=b-c;
ll gcd=__gcd(num,den);
cout<<den/gcd<<" "<<num/gcd<endl;

I am not understaning the logic,please anybody explain me little bit.

Given, average age of students of class A is a.
average age of students of class B is b.
Let, sum of age of students in class A be Sa, and that in class B be Sb.
And, number of students in class A be p and that in class B be q.
So, Sa/p = a and Sb/q = b (sum/total = average)
And (Sa+Sb)/(p+q) = c (sum of age of students in both
classes/number of students in both
classes = combined average)
Now, the equation can be manipulated as:
(ap + bq)/(p + q) = c
or, q*{a*(p/q) + b}/q*{(p/q) + 1} = c
or, a*(p/q) + b = c*{(p/q) + 1}
or, (p/q)*(a - c) = c - b
or, p/q = (c - b)/(a - c)

Thus, p/q can be simplfied into x/y which will be required ratio.
In order to make p and q co-prime, p and q must be divided by their greatest common divisor.

3 Likes

thanks i understood,