题解:CF2182F2 Christmas Reindeer (hard version)
前言
精神状态 be like:
鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿鹿……
upd on 2026/1/9:感谢 @_illion_ 指出问题,已修改。
题意简述
先给你
- 添加一只能力值为
2^x 的鹿(x \le 60 )。 - 移除一只能力值为
2^x 的鹿(x \le 60 )。 - 记
S 为一群鹿的能力值的列表,将S 按单调不增顺序排序,则该鹿群的总承载量为S_{0}+\lfloor\frac{S_{1}}{2^{1}}\rfloor+\lfloor\frac{S_{2}}{2^{2}}\rfloor+\lfloor\frac{S_{3}}{2^{3}}\rfloor+\lfloor\frac{S_{4}}{2^{4}}\rfloor+\dots+\lfloor\frac{S_{|S|}}{2^{|S|}}\rfloor ,求有多少个选择鹿群的方案使得这群鹿承载量不小于x (1 \le x \le 1 \times 10^{18} )。
保证
Solution
我们只关心鹿的能力值,初始鹿和一二类型操作可以用桶来记录,桶的大小与
现在只需考虑如何数数(回答询问 3)。
记
考虑从大到小枚举能力值为
引理:若选择一只能力值为
:::info[证明]{open}
设现在的系数为
下只鹿的贡献最大为
| 又因为这个数列显然长度有限,往后可能的最大贡献和一定小于 |
|---|
所以,根据引理,我们发现当前的鹿要么「贡献不小于
我们现在设计这样一个流程(记权值系数为
-
检查当前鹿贡献是否小于
x' 且严格大于\lfloor\frac{x'}{2}\rfloor 且这种鹿已选的数量不到cnt_i 。若是,选鹿,令x' \gets x'-2^{i-k} ,k \gets k+1 ;若否,则进入第二步。 -
检查当前鹿是否还有剩余且贡献不小于
x' ,若是,记当前这种鹿选了now 只,答案加上(前面选鹿的方案数)\times (在cnt_i 只中至少选now+1 只的方案数)\times 2^{\sum_{j=0}^{i-1}cnt_j} ;若否,则进入第三步。 -
记当前这种鹿选了
now 只,令pre \gets pre \times \dbinom{cnt_i}{now} 。处理i-1 ,返回第一步。
注意到,第一步最多执行
记计算在
可以通过其现实意义理解,
又发现实际
时间复杂度
记
预处理:
询问:单次最大
整体
Code
#include <bits/stdc++.h>
using namespace std;
namespace FastIO
{
template <typename T>
inline void read(T &x)
{
short neg = 1;
char ch;
while (ch = getchar(), !isdigit(ch))
if (ch == '-')
neg = -1;
x = ch - '0';
while (ch = getchar(), isdigit(ch))
x = (x << 3) + (x << 1) + (ch ^ '0');
x *= neg;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args)
{
read(x);
read(args...);
}
template <typename T>
inline void read(T *begin, T *end)
{
unsigned len = end - begin;
for (unsigned i = 0; i < len; i++)
read(*(begin + i));
}
template <typename T>
inline void write(T x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
template <typename T, typename... Args>
inline void write(T x, Args... args)
{
write(x);
putchar(' ');
write(args...);
}
template <typename T>
inline void write(T *begin, T *end)
{
unsigned len = end - begin;
for (unsigned i = 0; i < len; i++)
write(*(begin + i)), putchar(' ');
}
}
using namespace FastIO;
typedef long long LL;
typedef unsigned long long uLL;
const LL mod = 998244353;
LL n, m;
LL sum[65], cnt[65];
LL fac[600005], inv[600005], ifac[600005];
LL u[600005][65];
void init() // 预处理
{
u[0][0] = 1, fac[0] = 1, inv[0] = 1, ifac[0] = 1;
u[1][0] = 2, u[1][1] = 1, fac[1] = 1, inv[1] = 1, ifac[1] = 1;
for (int i = 2; i <= 600000; i++)
{
inv[i] = (mod - mod / i) * (inv[mod % i]) % mod;
fac[i] = fac[i - 1] * i % mod;
ifac[i] = ifac[i - 1] * inv[i] % mod;
u[i][0] = u[i - 1][0] * 2 % mod;
for (int j = 1; j <= min(i, 60); j++)
{
u[i][j] = (u[i - 1][j - 1] + u[i - 1][j]) % mod;
}
}
}
LL C(int n, int m)
{
if (m > n || m < 0)
return 0;
if (m == n || m == 0)
return 1;
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
void solve()
{
read(n, m);
int xi;
for (int i = 1; i <= n; i++)
{
read(xi);
cnt[xi]++;
}
LL op, x;
while (m--)
{
read(op, x);
if (op == 1)
cnt[x]++;
else if (op == 2)
cnt[x]--;
else
{
sum[0] = cnt[0];
for (int i = 1; i <= 60; i++)
sum[i] = sum[i - 1] + cnt[i]; // 前缀和
int cur = 0;
LL ans = 0, pre = 1; // ans 即答案,pro 即前面选鹿的方案数
for (int i = 60; i >= 0; i--)
{
int now = 0;
while (now < cnt[i] && (((1ll << i) >> cur) > (x >> 1) && ((1ll << i) >> cur) < x)) // Step 1
{
x -= ((1ll << i) >> cur);
cur++;
now++;
}
if (now < cnt[i] && ((1ll << i) >> cur) >= x) // Step 2
{
ans = (ans + (pre * (i ? u[sum[i - 1]][0] /*即2的sum[i - 1]次方*/ : 1) % mod * u[cnt[i]][now + 1])) % mod;
}
pre = pre * C(cnt[i], now) % mod; // Step 3
}
write(ans);
putchar('\n');
}
}
return;
}
signed main()
{
init();
solve();
return 0;
}