AT_arc182_c [ARC182C] Sum of Number of Divisors of Product Solution

· · 题解

参考了其他大佬的题解。

求所有长度小于等于 n,值域在 [1,m] 的序列的元素乘积的因子个数之和。n\leq 10^{18},m\leq 16。

显然因子个数跟每个质因子有关,发现值域很小,16 以内的质数只有 6 个,于是可以往状压质数状态的方向去想。记 m 以内有 t 个质数。

对于乘积 S,设他的质因子分解得到 S=\prod_{i=1}^t p_i^{c_i},那么因数个数为 d(S)=\prod_{i=1}^t(c_i+1),如果对他再乘以一个数 x,x=\prod_{i=1}^t g_i,则 d(Sx)=\prod(c_i+g_i+1)。

这个乘积的因数个数感觉不能直接从两个数得到,考虑类似乘法分配律的方式分开计算贡献。我们记 f_S 表示质因子属于 S 集合内的 \prod (c_i+1)。那么针对乘上一个数 x,因为:

\prod_{i\in S} (c_i+g_i+1)=\sum_{T\subseteq S}\left(\prod_{i\in T} (c_i+1)\right)\left(\prod_{i\in S\wedge i\notin T}g_i\right).

所以令 c_{x,i} 表示 x 含有质因子 p_i 的个数,则有转移:

f'_S=\sum_{T\subseteq S}f_T\sum_{x=1}^m\prod_{d\in S\wedge d\notin T}^tc_{x,d}.

上面那个等式可以通过乘法分配律拆出括号得到。然后我们就得到了一个 O(nm\cdot \pi(m)\cdot3^{\pi(m)}) 的 dp。

容易发现,n 奇大无比而这个 dp 的状态数比较少,考虑矩阵快速幂即可。矩阵的系数就是上面的递推式后面的系数,即矩阵元素 A_{T,S} 为:

A_{T,S}=\sum_{x=1}^m\prod_{d\in S\wedge d\notin T}^tc_{x,d}.

然后直接快速幂即可。但是所求的是所有长度的方案之和,我们考虑在矩阵最后塞一维求和的即可,记这个求和的元素为 P,dp 转移为:

P_i=P_{i-1}+\sum_{S}f_{i,S}=P_{i-1}+\sum_{S}\sum_{T\subseteq S}f_{i-1,T}\cdot A_{T,S}.

即:

P_i=P_{i-1}+\sum_{T}f_{i-1,T}\sum_{S\supseteq T}A_{T,S}.

那么就得到了矩阵中 P 的转移系数:

A_{T,P}=\sum_{S\supseteq T}A_{T,S}.

以及自己对自己的贡献 A_{P,P}=1,然后做完了。时间复杂度 O(m\cdot\pi(m)\cdot3^{\pi(m)}+2^{3\pi(m)}\log n)。

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 65;
const LL MOD = 998244353;
int m, tot, len, prime[N]; LL n;
bool check(int x) {
    for (int i = 2; i * i <= x; i ++) if (x % i == 0) return 0;
    return 1;
}
struct Matrix {
    LL a[N][N];
    void clear() { memset(a, 0, sizeof a); }
};
const Matrix operator*(const Matrix& a, const Matrix& b) {
    Matrix c; c.clear();
    for (int i = 0; i < len; i ++) for (int k = 0; k < len; k ++) for (int j = 0; j < len; j ++)
        c.a[i][j] = (c.a[i][j] + a.a[i][k] * b.a[k][j] % MOD) % MOD;
    return c;
}
Matrix mat; int cnt[N];
Matrix Qpow(Matrix x, LL k) {
    Matrix ret, tmp = x; for (int i = 0; i < len; i ++) ret.a[i][i] = 1;
    while (k) {
        if (k & 1) ret = ret * tmp;
        tmp = tmp * tmp; k >>= 1;
    } return ret;
}

int main() {
    freopen(".in", "r", stdin); freopen(".out", "w", stdout);
    ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 2; i <= m; i ++) if (check(i)) prime[tot ++] = i;
    len = (1 << tot) + 1;
    for (int i = 1; i <= m; i ++) {
        for (int j = 0, tmp = i; j < tot; j ++) while (tmp % prime[j] == 0) cnt[j] ++, tmp /= prime[j];
        for (int j = 0; j < 1 << tot; j ++) {
            for (int k = j; ; k = (k - 1) & j) {
                LL prod = 1;
                for (int l = 0; l < tot; l ++) if (!((k >> l) & 1) && ((j >> l) & 1)) prod *= cnt[l];
                mat.a[k][j] = (mat.a[k][j] + prod) % MOD;
                if (!k) break;
            }
        }
        for (int j = 0, tmp = i; j < tot; j ++) while (tmp % prime[j] == 0) cnt[j] --, tmp /= prime[j];
    } mat.a[1 << tot][1 << tot] = 1;
    for (int j = 0; j < 1 << tot; j ++) for (int k = j; ; k = (k - 1) & j) { 
        mat.a[k][1 << tot] = (mat.a[k][1 << tot] + mat.a[k][j]) % MOD; if (!k) break; 
    } mat = Qpow(mat, n); cout << mat.a[0][1 << tot];
    return 0;
}