Colliding Race | CodeChef

Can anyone help me with the solution to this problem?

int t=in.nextInt();
while(t–>0){
long x=in.nextLong();
long r=in.nextLong();
long a=in.nextLong();
long b=in.nextLong();
long max=a>b?a:b;
long min=a<b?a:b;
double v=((double)(max-min)*x)/max;
double w=((max-min)*x)/max;
if(v!=w)
System.out.println((int)w);
else
System.out.println((int)w-1);
}
w and w-1 since we have to output ans before end of race

Do some math and all you get is this
x * abs(a - b) / max(a, b) - (x * abs(a - b) % max(a, b) == 0)

1 Like

@sayanmedya can you explain how you got that formula. Means the logic behind deducing it.

@sayanmedya even I wanted to know how you go the mathematical formula?

Find out the time when they will first meet. if they first meet at time t and the race ends in T then they will meet total T/t times. When they first meet the person who has more speed completes one round extra than the slower one. So, max(a,b)*t=min(a,b)*t+2 \pi r and we get T from the equation, max(a,b)*T=2\pi rx. Now the answer will be T/t.

2 Likes

Thank you for explanation. I got it