1. 程式人生 > >cf 1063D Round 516 Candies for Children

cf 1063D Round 516 Candies for Children

boa 出發 sse ins member def per index nts

D. Candies for Children time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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.

Input

The only line contains four integers nn, ll, rr and kk (1n,k10111≤n,k≤1011, 1l,rn1≤l,r≤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

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.

Examples input Copy
4 1 4 12
output Copy
2
input Copy
5 3 4 10
output Copy
3
input Copy
10 5 5 1
output Copy
10
input Copy
5 4 5 6
output Copy
-1
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.

題目大意: 告訴你有n個人在圈裏,共計有k個糖果,從起點出發到終點(可能經過好幾圈),每個人只拿一個糖果或者兩個糖果每次,愛吃甜食的小朋友可以拿兩個一次(但是如果糖果不夠了,也只能拿一個),問最多有多少個小朋友愛吃甜食。

n,k<=10^11

思路:

n和k都比較大,所以我們要分類討論。

考慮到經過t+1次的那部分有x個人,經過t次的那部分有y個人。x=(r-l+n)%n+1; x裏面有a個吃甜食,y裏面有b個吃甜食

如果n比較小,那麽我們就暴力枚舉a和b的值。

如果n比較大,此時k/n比較小,也就是圈數比較少,我們可以暴力枚舉t,然後解二元一次方程組。

註意: 最後一個小朋友吃幾個糖果要分類討論(因為最後一個小朋友有可能喜歡吃甜食)。

 1 #include<bits/stdc++.h>
 2 using namespace std; 
 3 
 4 #define F(i,a,b) for(int i=a;i<=b;i++) 
 5 #define D(i,a,b) for(int i=a;i>=b;i--) 
 6 #define ms(i,a)  memset(a,i,sizeof(a)) 
 7 #define LL       long long 
 8 template<class T>void read(T &x){
 9     x=0; char c=getchar(); 
10     while (c<0 || c>9) c=getchar(); 
11     while (c>=0 && c<=9) x=(x<<3)+(x<<1)+(c^48),c=getchar();
12 }
13 template<class T>void write(T x){
14     if(x<0) x=-x,putchar(-); 
15     if(x>9) write(x/10); 
16     putchar(48+x%10);  
17 }
18 template<class T>void chkmax(T &x,T y){ x=x>y? x:y; }  
19 LL n,k,l,r,x,t,s,p,ans,y;  
20 LL const inf=1LL<<50;  
21 LL calc(LL p,LL s,int a,int b){
22     if(a==0 || b==1){
23         if(k-s-x>=0 && (k-s-x)%(n+p)==0)
24             return p;  
25         else return -1;  
26     }else {
27         if(k-s+1-x>=0 && (k-s+1-x)%(n+p)==0)
28             return p;  
29         else return -1;  
30     }
31 }
32 
33 LL exgcd(LL &x,LL &y,LL a,LL b){
34     if(!b){
35         x=1;y=0; return a; 
36     }
37     LL t=exgcd(y,x,b,a%b);  
38     y-=a/b*x; 
39     return t;  
40 }
41 void solve(LL t1,LL t2,LL c,LL add){
42     if(t2==0){
43         if(2*x>=k && k>=x) chkmax(ans,min(x,k-x+1)+y); 
44         return;
45     }
46     LL a,b;
47     LL g=exgcd(a,b,t1,t2); 
48     a=a*c; b=b*c; 
49     LL l=-inf,r=inf;  
50     a-=add; 
51     if (a>=0) r=min(r,a/t2); else  r=min(r,(a-t2+1)/t2); a+=add;  
52     if(a-x>=0) l=max(l,(a-x+t2-1)/t2); else l=max(l,(a-x)/t2);  
53     if(y-b>=0) r=min(r,(y-b)/t1); else r=min(r,(y-b-t1+1)/t1); 
54     if(-b>=0) l=max(l,(-b+t1-1)/t1); else l=max(l,-b/t1);  
55     if(l<=r){
56         chkmax(ans,a+b+l); 
57         chkmax(ans,a+b+r);  
58     }
59 }
60 int main(){
61     read(n); 
62     read(l); 
63     read(r); 
64     read(k); 
65     x=(r-l+n)% n+1;  
66     y=n-x;  
67     ans=-1;  
68     if(k/n>=5000000){
69         F(p,0,n) F(s,max(0LL,x-(n-p)),min((int)x,p)){
70             if(s<x) chkmax(ans,calc(p,s,0,0));  
71             if(s)chkmax(ans,calc(p,s,1,0));  
72             if(s)chkmax(ans,calc(p,s,1,1));  
73         }
74     }else {
75         F(t,0,k/n){
76             solve(t+1,t,k-t*x-x-t*y,0);  
77             solve(t+1,t,k-t*x-x-t*y+1,1);  
78         }
79     }
80     write(ans); 
81     return 0; 
82 }

cf 1063D Round 516 Candies for Children