跳到正文

题解

CF2035E Monster 题解 / Solution

MatrixGroup约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟
获赞
9
题目
CF2035E

版本

  1. v1当前

前言 / Foreword

非常好题目。非常好题目背景。

Great problem, great quote.

扽西 / Analysis

先考虑最优解长什么样子。如果先攻击再增加,并且可以把增加放到攻击前面,那么造成的伤害可以变多 11。因此,一定要尽可能先增加再攻击。故而最优解一定形如,重复 r(r≥0)r(r\ge 0) 次“增加 kk 次,攻击 11 次”,再增加 a(1≤a≤k)a(1\le a\le k) 次,攻击 b(b≥1)b(b\ge 1) 次。这样花费了 rkx+ry+ax+byrkx+ry+ax+by 枚金币,造成了 r(r+1)2k+(rk+a)b\dfrac{r(r+1)}{2}k+(rk+a)b 的伤害。

Consider what form the optimal solution can be in. If one firstly attacks the monster then increases dd, the damage dealt is 11 less than that if one firstly increases dd then attakcs the monster. Therefore, in the optimal solution, the increments must be before attacks as much as possible, which means the optimal solution must be in the following form: repeat “increase dd for kk times, and attack once” for r(r≥0)r(r\ge 0) times, then increase dd for a(1≤a≤k)a(1\le a\le k) times, then attack b(b≥1)b(b\ge 1) times. In this way, damage of r(r+1)2k+(rk+a)b\dfrac{r(r+1)}{2}k+(rk+a)b is dealt by costing rkx+ry+ax+byrkx+ry+ax+by coins.

t≤100,x,y,z,k≤108t\le 100,x,y,z,k\le 10^8,一看就很像根号分治的样子。考虑设立阈值 BB:

t≤100,x,y,z,k≤108t\le 100,x,y,z,k\le 10^8, which is super sqrt-balancing-ish. Consider a threshold BB:

若 k≤Bk\le B,因为 kk 很小,aa 的范围为 1∼k1\sim k 也很小,可以直接考虑枚举 a,ra,r,计算出最小的 bb 更新答案。因为一旦 r(r+1)2k≥z\dfrac{r(r+1)}{2}k\ge z 就可以不用再枚举了,rr 的范围是 O(zk)O\left(\sqrt{\dfrac zk}\right) 的,因此这时的复杂度是 O(zk)O(\sqrt {zk})。

When k≤Bk\le B, since kk is small, and the range of aa is 11 to kk, one can enumerate a,ra,r, then calculate the least possible bb to update the answer. When r(r+1)2k\dfrac{r(r+1)}{2}k already reaches zz, there’s no need to continue the enumeration anymore, so rr is in a range of O(zk)O\left(\sqrt{\dfrac zk}\right), which implies a complexity of O(zk)O(\sqrt{zk}).

若 k≥Bk\ge B,此时 zk\dfrac{z}{k} 很小。此时枚举 rr,枚举 bb 的范围是 O(zrk)O\left(\dfrac{z}{rk}\right) 的,再计算最小的 aa 即可。因为调和级数,这部分的复杂度是 O(zklog⁡z)O\left(\dfrac{z}{k}\log z\right) 的。r=0r=0 时需要特殊用数论分块处理,因此总复杂度 O(zklog⁡z+z)O\left(\dfrac{z}{k}\log z+\sqrt z\right)。

When k≥Bk\ge B, zk\dfrac{z}{k} is small. Therefore, bb is in a range of O(zrk)O\left(\dfrac{z}{rk}\right). By enumerating rr and bb, one can determine the least possible aa and then update the answer. Thanks to the harmonic series, The complexity is O(zklog⁡z)O\left(\dfrac{z}{k}\log z\right). However, when r=0r=0, one needs to use the trick of ”O(z)O(\sqrt z) different values for ⌈zb⌉\left\lceil \dfrac zb\right\rceil” to enumerate bb in a clever way. The total complexity is O(zklog⁡z+z)O\left(\dfrac{z}{k}\log z+\sqrt z\right).

取 B=O(z1/3log⁡2/3z)B=O(z^{1/3}\log^{2/3}z),每组数据的时间复杂度是 O(z2/3log⁡1/3z)O(z^{2/3}\log^{1/3}z),可以通过。

Take B=O(z1/3log⁡2/3z)B=O(z^{1/3}\log^{2/3}z), the time complexity per test is O(z2/3log⁡1/3z)O(z^{2/3}\log^{1/3}z), which can pass the problem easily.

原文