CF1063D.Candies for Children

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 kk 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 nn people remaining there. Lena numbered her friends in a clockwise order with positive integers starting with 11 in such a way that index 11 was assigned to her best friend Roma.

Initially, Lena gave the box to the friend with number ll, after that each friend (starting from friend number ll) took candies from the box and passed the box to the next friend in clockwise order. The process ended with the friend number rr 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.

在儿童节上,孩子们围成一个圆圈跳舞。当音乐停止时,孩子们仍然站在圆圈中。这时,莉娜想起她的父母送给她一盒恰好 kk 颗的“Wilky May”糖果。莉娜并不是一个贪心的人,因此她决定将自己所有的糖果都分给圆圈中的朋友们。莉娜知道,她的一些朋友嗜甜,而另一些则不嗜甜。嗜甜的朋友会从盒中取出两颗糖果(若盒中至少还剩两颗糖果),否则只取一颗;其余朋友则总是恰好从盒中取走一颗糖果。

在开始分发糖果前,莉娜先退出了圆圈,此时圆圈中恰好剩下 nn 个人。莉娜按顺时针方向,用正整数为她的朋友们编号,起始编号为 11,并把编号 11 分配给她最好的朋友罗玛(Roma)。

最初,莉娜将糖果盒交给了编号为 ll 的朋友;之后,每位朋友(从编号为 ll 的朋友开始)依次从盒中取走糖果,并将盒子顺时针传递给下一位朋友。该过程终止于编号为 rr 的朋友取走了最后一颗(或最后两颗,谁知道呢?)糖果,随后盒子变为空盒。请注意,某些朋友可能多次从盒中取糖果,即盒子可能绕圆圈转了好几圈后才变空。

莉娜并不知道哪些朋友嗜甜,但她很想知道:在满足上述所有条件的前提下,最多可能有多少位朋友是嗜甜的?如果该情形根本不可能发生(即莉娜的观察有误),请告诉她这一点。

输入格式

The only line contains four integers nn, ll, rr and kk (1≤n,k≤10111 \le n, k \le 10^{11}, 1≤l,r≤n1 \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.

唯一的一行包含四个整数 nn、ll、rr 和 kk(1≤n,k≤10111 \le n, k \le 10^{11},1≤l,r≤n1 \le l, r \le n)—— 分别表示围成一圈的小孩人数、最初拿到糖果盒的小孩编号、拿走最后一颗糖果的小孩编号,以及糖果盒中最初的糖果数量。

输出格式

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.

输出一个整数——Lena 的朋友们中可能存在的最多“甜牙”人数;如果 Lena 错了,则输出 -1(引号仅用于明确显示)。

输入输出样例

  • 输入#1

    4 1 4 12

    输出#1

    2
  • 输入#2

    5 3 4 10

    输出#2

    3
  • 输入#3

    10 5 5 1

    输出#3

    10
  • 输入#4

    5 4 5 6

    输出#4

    -1

说明/提示

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.

在第一个例子中,任意两位朋友都可以是甜食爱好者,这样每个人将两次收到装有糖果的盒子,而最后取走糖果的人将是第四位朋友。

在第二个例子中,甜食爱好者可以是任意三位朋友,但第三位位置上的朋友除外。

在第三个例子中,只有一位朋友会取走糖果,但他仍可以是甜食爱好者,只是无法取走两颗糖果。圆圈中的其他所有朋友也可以是甜食爱好者,但他们甚至一次都无法取走糖果。

在第四个例子中,莉娜错了,这种情况不可能发生。

输入解题思路,AI测评打分。不知道怎么写?

首页