Candies for Children
Contest: Codeforces Round 516 (Div. 1, by Moscow Team Olympiad)
Difficulty rating: 2600
Tags: brute force, math

At the children's festival, children were dancing in a circle. When music stopped playing, the children were still standing in a circle. Then Lena remembered, that her parents gave her a candy box with exactly $$$k$$$ candies "Wilky May". Lena is not a greedy person, so she decided to present all her candies to her friends in the circle. Lena knows, that some of her friends have a sweet tooth and others do not. Sweet tooth takes out of the box two candies, if the box has at least two candies, and otherwise takes one. The rest of Lena's friends always take exactly one candy from the box.

Before starting to give candies, Lena step out of the circle, after that there were exactly $$$n$$$ people remaining there. Lena numbered her friends in a clockwise order with positive integers starting with $$$1$$$ in such a way that index $$$1$$$ was assigned to her best friend Roma.

Initially, Lena gave the box to the friend with number $$$l$$$, after that each friend (starting from friend number $$$l$$$) took candies from the box and passed the box to the next friend in clockwise order. The process ended with the friend number $$$r$$$ taking the last candy (or two, who knows) and the empty box. Please note that it is possible that some of Lena's friends took candy from the box several times, that is, the box could have gone several full circles before becoming empty.

Lena does not know which of her friends have a sweet tooth, but she is interested in the maximum possible number of friends that can have a sweet tooth. If the situation could not happen, and Lena have been proved wrong in her observations, please tell her about this.

Input format:
The only line contains four integers $$$n$$$, $$$l$$$, $$$r$$$ and $$$k$$$ ($$$1 \le n, k \le 10^{11}$$$, $$$1 \le l, r \le n$$$) — the number of children in the circle, the number of friend, who was given a box with candies, the number of friend, who has taken last candy and the initial number of candies in the box respectively.

Output format:
Print exactly one integer — the maximum possible number of sweet tooth among the friends of Lena or "-1" (quotes for clarity), if Lena is wrong.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
In the first example, any two friends can be sweet tooths, this way each person will receive the box with candies twice and the last person to take sweets will be the fourth friend.

In the second example, sweet tooths can be any three friends, except for the friend on the third position.

In the third example, only one friend will take candy, but he can still be a sweet tooth, but just not being able to take two candies. All other friends in the circle can be sweet tooths as well, they just will not be able to take a candy even once.

In the fourth example, Lena is wrong and this situation couldn't happen.

Example input:
4 1 4 12

Example output:
2

Example input:
5 3 4 10

Example output:
3

Editorial:
Solution works in $$$min(n^2, k / n)$$$ time. Also I will drop the case about the last person being sweet tooth and eating one candy instead of two.
This adds few more cases but the idea stays the same. Basically in the formulas below just few $$$-1$$$ will appear, however there must be condition that there is at least one sweet tooth in corresponding part of the circle.
So how to solve problem in $$$n^2$$$?
Note, that basically we have two parts of the circle — the part between $$$[l; r]$$$ which get's candies one times more than the rest and the other one.
Since we don't care about absolute values we can only worry about the lengths of this parts, let's denote them as $$$x$$$ and $$$y$$$.
To solve in $$$n^2$$$ let's bruteforce the number of sweet tooth on first part ($$$a$$$) and the number of sweet tooth on the second part ($$$b$$$).
Suppose that there were full $$$t$$$ loops. This way the number of eaten candies is $$$a * 2 * (t + 1) + (x - a) * 1 * (t + 1) + b * 2 * t + (y - b) * 1 * t$$$.
This should be equal to $$$k$$$. Since we just bruteforced the values of $$$a$$$ and $$$b$$$ we now just have linear equation. If it is solvable, consider relaxing answer with $$$a + b$$$.
How to solve problem in $$$k / n$$$?
So as the asymptotic suggests the amount of turns $$$\le k / n$$$, so we can bruteforce it instead.
Also it is worthy to handle case of $$$0$$$ turns specifically here since it produces some unpleasant effects otherwise.
So each person in "$$$x$$$" part of the circle eats candies $$$t + 1$$$ times (so each person contributes $$$t + 1$$$ or $$$2t + 2$$$) and the other persons have eaten candies $$$t$$$ times (so each person contributes $$$t$$$ or $$$2t$$$).
Let's account all persons as if they are not sweet tooths.
So now each person in $$$x$$$ contributes $$$0$$$ or $$$t + 1$$$ and each person in $$$y$$$ contributes $$$0$$$ or $$$t$$$.
So we basically have $$$(t + 1) a + t b = \gamma$$$. A Diophantine equation.
Careful analysis or bits of theory suggest that the solutions are
$$$a = a_0 - t z$$$, $$$b = b_0 + (t + 1) z$$$, for all integer $$$z$$$.
Where $$$a_0$$$ and $$$b_0$$$ some arbitrary solutions which we can get with formulas.
Also we need to have $$$0 \le a \le x$$$, $$$0 \le b \le y$$$, and the $$$a + b \to max$$$.
These bounds imply that $$$t$$$ takes values only in some range $$$[t_1; t_2]$$$.
Since the $$$a + b$$$ is linear function we can only consider $$$t_1$$$ and $$$t_2$$$ while searching for maximum $$$a + b$$$.