2020 Summer #4

0x01 Bear and Prime Numbers

注意到 \(a_i \leqslant 10^7\),故只需求 \(10^7\) 以内的素数。

不难想到记录每个数出现的次数,再对每个素数分别枚举倍数,将次数累计,最后前缀和。


0x02 Power Products

\(a_i = \displaystyle\prod_{\iota=1}^m p_{\iota}^{\alpha_{\iota}}\),则 \(a_j = \displaystyle\prod_{\iota=1}^m p_{\iota}^{kr-\alpha_{\iota}}\),拆成数组存储和查找,用 map 实现。


0x03 Prime Number

\(S = a_1 + a_2 + \cdots + a_n\),则分子为 \(\displaystyle\sum_{i=1}^n x^{S - a_i}\)

注意到只需求次数,再分子分母取最小值即为 \(\gcd\),又可能出现形如 \(x^i + x^i + \cdots + x^i = x^{i+1}\) 的情况,故将分子中每一项升序排序求。


0x04 Multipliers

熟知 \(n = \displaystyle\prod_{i=1}^m p_i^{\alpha_i}\) 的因数个数为 \(\displaystyle\prod_{i=1}^m (a_i+1)\),不妨记为 \(N\)。设 \(n\) 的因数乘积为 \(P_n\)

考虑 \(n' = n \cdot p_{m+1}^{\alpha_{m+1}}\),则对 \(n\) 的每个因数,其与 \(p_{m+1}^{j} \ (0 \leqslant j \leqslant \alpha_{m+1})\) 分别相乘产生 \(n'\)\(\alpha_{m+1} + 1\) 个因数,故 \(\large P_{n+1} = P_n^{\alpha_{m+1}} \cdot p_{m+1}^{\frac {\alpha_{m+1}(\alpha_{m+1}+1)} 2 \cdot N}\)


0x05 Double Happiness

费马平方和定理知,只需求形如 \(4k+1\) 的素数个数。

为避免 MLE,改用 bitset


0x06 Anniversary

在 Fibonacci 数列中,有引理\(\large\gcd(f_i, f_j) = f_{\gcd(i, j)}\),则此题显然。


0x07 Empire Strikes Back

引理:对于质数 \(p\)\(n!\) 中因子 \(p\) 的个数为 \(\displaystyle\sum_{k=1}^{\infty} \left\lfloor \frac n {p^k} \right\rfloor\)

质因数分解 + 二分。


0x08 Magic Five

预处理 \(a\) 串作为前缀和在中间的答案。


0x09 Partial Sums

\(\{ a_n \}\)\(k\) 阶前缀和为 \(\{ s_n^k \}\),其中 \(s_i^0 = a_i, \ s_i^k = s_{i-1}^k + s_{i}^{k-1}\)

注意到此递推式形式与杨辉三角相同,不难得出 \(s_i^k = \displaystyle\sum_{j=1}^i (C_{k+i-j}^{i-j})\)

\(k\) 很大而 \(i-j \le n \le 2000\),则可 \(O(n^2)\)\(i-j\) 的每种取值暴力预处理出对应的组合数。


0x0A Vasily the Bear and Beautiful Strings

注意到 1110 修改后都是 0,故第一个 1 后的所有字符对结果均无影响。

枚举第一个 1 的位置,剩下的用组合数计算。


0x0B Iahub and Permutations

容斥原理,设共有 \(m\)-1,其中有 \(k\) 个可满足 \(p_i = i\)\(f(i) = \binom{k}{i} \cdot (m-i)!\),则 \(ans = m! - \displaystyle\sum_{i=1}^m (-1)^i f(i)\)


0x0C Devu and Birthday Celebration

\(g = \gcd(a1, a_2, \ldots, a_f)\),则

\[\begin{aligned} ans &= \sum_{a_1, a_2, \ldots, a_f} \left( [g=1] \cdot \left[ \sum a_i = n \right]\right) \\ &= \sum_{a_1, a_2, \ldots, a_f} \left( \sum_{d|g} \mu(d) \cdot \left[ \sum a_i = n \right]\right) \\ &= \sum_{d|n} \mu(d) \sum_{a_1, a_2, \ldots, a_f} \left[ \sum a_i = \frac n d \right] \\ &= \sum_{d|n} \mu(d) \cdot \binom{\frac n d - 1}{f-1} \end{aligned} \]


0x0D Pluses everywhere

显然可以分别算每一位数对答案的贡献,这取决于其后第一个 + 的位置。

设第 \(i\) 位数后的第一个 +\(j-1\)\(j \ (\leqslant n+1)\) 之间,则其贡献为 \(\large 10^{j-i-1} \cdot \binom{n-j+i-[j>n]}{k-[j>n]}\)


0x0E Fibonacci Sums

引理:任意正整数 \(n\) 均可表示为若干 Fibonacci 数的和。

易构造一种方案,每次选取最大的 \(f_i \leqslant n\),对 \(n - f_i\) 递归。对任意一种方案,可用一 01 串表示,第 \(i\) 位为 1 表示方案中包含 \(f_i\),否则不包含。

显然若存在形如 001 的字串,可将其变为 110,从而得到另一种方案,每段 0 的个数决定作此变换的次数上限,根据乘法原理计算答案。


0x0F Bear in the Fields

矩阵快速幂。


0x10 Mahmoud and Ehab and yet another xor task

线性基。

设处理到第 \(i\) 个询问时,线性基中共有 \(m\) 个数,则对于不在线性基内的数共有 \(2^{l-m}\) 种选法。

再设选出来的这些数异或和为 \(y\),则显然用那 \(m\) 个数可以唯一地构造出异或和为 \(x_i \oplus y\) 的子集,故总方案数即为 \(2^{l-m}\)

posted @ 2022-04-09 21:42  johnsmith0x3f  阅读(13)  评论(0)    收藏  举报