cf 1063D Round 516 Candies for Children
Posted zjxxcn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cf 1063D Round 516 Candies for Children相关的知识,希望对你有一定的参考价值。
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.
The only line contains four integers nn, ll, rr and kk (1≤n,k≤10111≤n,k≤1011, 1≤l,r≤n1≤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.
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.
4 1 4 12
2
5 3 4 10
3
10 5 5 1
10
5 4 5 6
-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.
题目大意: 告诉你有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的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #516 (Div. 2&&Div.1)
Codeforces Round #516 (Div. 2) (A~F)