Codeforces 1063D Candies for Children

题目大意

给定整数 $n, k, l, r$,$1\le n, k \le 10^{11}$,$1\le l, r \le n$ 。
令 $ m = r - l + 1$,若 $m \le 0$,$m\gets m + n$ 。
未知数 $x\in \mathbb{Z}$ 满足 $ 0 \le x \le n$,且满足
$ k \bmod (n + x) = 0$ 且 $m = n$ ;或者
$ k \bmod (n + x) \ne 0$ 且 $0 \le k \bmod (n + x) - m \le \min(m, x) $

求 $x$ 的最大值,若不存在符合条件的 $x$ 则输出 -1。



换一种思路,基本上是用自己的话将官方题解描述了一遍。

Note, that basically, we have two parts of the circle — the part between $[l;r]$ which gets candies one time more than the other part.

为了便于描述,将 $[l;r]$ 称作第一段,其余的称作第二段。注意:第一段长度一定大于零,而第二段长度可能等于零。

另外假设 $r$ 最后一次取糖时,恰取到了他想要的数量的糖(换言之若 $r$ 是 sweet tooth,那么他最后一次得到 2 块糖)。如果不是这种情况,只要将 $k$ 变成 $k+1$ 并且保证第一段内至少有 $1$ 个 sweet tooth。

设第一段长度为 $x$,其中有 $a$ 个 sweet tooth,第二段长度为 $y$ 且其中有 $b$ 个 sweet tooth,设第二段经历了 $t$ 次分糖,则第一段经历了 $t+1$ 次分糖。可以建立如下的不定方程:

$ (2a + (x- a)) (t + 1) + (2b + (y - b) ) t = k$
化简得

\begin{equation}
(a + x) (t + 1) + (b + y) t = k \label{E:1}
\end{equation}

约束条件:$ 0\le a \le x$,$0\le b \le y$ 。

(下面是这道题最精髓的地方。)

当 $n$ 比较小时,我们可以暴力枚举 $a$,$b$,看方程 \eqref{E:1} 是否有解,复杂度 $O(n^2)$

当 $n$ 比较大时,我们可以暴力枚举 $t$,显然有 $0 \le t \le k / n$ 。此时方程 \eqref{E:1} 变成了关于 $a,b$ 不定方程,确切地说是丢番图方程(Diophantine equation)
\begin{equation}
(t+ 1) a + tb = \gamma \label{E:2}
\end{equation}
其中 $\gamma = k - nt - x$ 。
方程 \eqref{E:2} 的通解
$ a = a_0 - tz, b = b_0 + (t+1) z$
其中 $a_0, b_0$ 是方程 \eqref{E:2} 的一组特解,可以由扩展欧几里得算法得到,$z$ 是任意整数。
由 $0 \le a \le x$,$0\le b \le y$ 可得到 $z$ 的取值范围 $[z_1, z_2]$ 。显然,$z = z_2$ 时 $a+ b$ 最大。

事实上,我们有 $\gcd(t+1, t) = 1$,此时可取特解 $a_0 = \gamma, b_0 = - \gamma$ 。

原文地址:https://www.cnblogs.com/Patt/p/9789235.html

时间: 2024-08-01 21:41:21

Codeforces 1063D Candies for Children的相关文章

codeforces 776E The Holmes Children

目录 codeforces 776E The Holmes Children 题意 题解 Code codeforces 776E The Holmes Children 题目传送门 题意 定义\(f(n)\)为满足\(x+y=n\)并且\(gcd(x,y)=1\)的有序正整数对个数,定义\(g(n)=\sum_{d|n}f(\frac{n}{d})\),定义\(F_k(n)\)如下: \[ F_k(n)=\begin{cases} f(g(n)) & k=1 \g(F_{k-1}(n)) &am

Codeforces1063D Candies for Children 【分类讨论】【暴力】

题目分析: 首先要想两个暴力,一个的时间复杂度是$O(n^2)$,另一个是$O([\frac{n}{k}])$的. $n^2$的暴力可以枚举两段,一段有$i$个取两个的小朋友,一段有$j$个取两个的小朋友. 你就可以算出每轮选取他们的代价,假设为$alpha$和$beta$.你要做的只是解$ (x+1)*alpha+x*beta=k $,不难解决. 然后是$O([\frac{n}{k}])$的暴力,枚举选举的轮数,也就是上面的$x$.首先假设每个小朋友选一个糖果,然后问题变为小朋友选或不选糖果.

[Codeforces]Round 516

A Make a triangle! 题意:给定三根线段,问最少要延长多少才能拼成一个三角形. 数学题. B Equations of Mathematical Magic 题意:求$a - (a \oplus b)-x=0 $的非负整数解的个数. 打表发现是\(a\)在二进制下\(1\)的数量. C Oh Those Palindromes 题意:求字符串\(S\)重新排列后的回文子串的最大数量. 猜想一样的放在一起会最多,然后就对了. D Labyrinth 题意:给定一个迷宫,有一些障碍,

Codeforces Round #516 (Div. 1) 题解

A.Oh Those Palindromes 直接排序之后输出就行了. 证明的话直接跟据同一种字符最多能在多少个回文串中贡献答案就行了. #include <bits/stdc++.h> #define for1(a,b,i) for(int i=a;i<=b;++i) #define FOR2(a,b,i) for(int i=a;i>=b;--i) using namespace std; typedef long long ll; #define M 500005 int n

Codeforces Round #257 (Div. 2) A. Jzzhu and Children(简单题)

题目链接:http://codeforces.com/problemset/problem/450/A ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces Round #257 (Div. 2/A)/Codeforces450A_Jzzhu and Children

解题报告 没什么好说的,大于m的往后面放,,,re了一次,,, #include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; struct node { int x,cd; }num[1000000]; int main() { int n,m,c; cin>>n>>m; int i; for(i=0;i&l

Codeforces Round #400 E. The Holmes Children

题目链接:Codeforces Round #400 E. The Holmes Children 题意: 定义f(1)=1,f(n),n>1的值为满足x+y=n且gcd(x,y)=1的(x,y)个数:定义g(n)=Σd|n f(n/d):定义Fk(n)满足k=1时Fk(n)=f(g(n)),k>1且k mod 2=0时Fk(n)=g(Fk-1(n)),k>1且k mod 2=1时Fk(n)=f(Fk-1(n)) .给出n,k,求Fk(n)mod 1000000007.(1<=n,

Codeforces C - Om Nom and Candies

C - Om Nom and Candies 思路:贪心+思维(或者叫数学).假设最大值max(wr,wb)为wr,当c/wr小于√c时,可以枚举r糖的数量(从0到c/wr),更新答案,复杂度√c:否则,假设hr/wr<hb/wr,得到hr*wb<hb*wr,由这个等式可知,在有wb*wr重量限制的情况下,买wb个r糖没有买wr个b糖划算,当需要买超过wb个r糖时,不如去买b糖,可以枚举r糖的数量(从0到wb-1),更新答案,复杂度√c. 代码: #include<bits/stdc++

Codeforces 526C - Om Nom and Candies

A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place? One day,