AT_arc182_c [ARC182C] Sum of Number of Divisors of Product Solution
参考了其他大佬的题解。
求所有长度小于等于
n ,值域在[1,m] 的序列的元素乘积的因子个数之和。n\leq 10^{18},m\leq 16 。
显然因子个数跟每个质因子有关,发现值域很小,
对于乘积
这个乘积的因数个数感觉不能直接从两个数得到,考虑类似乘法分配律的方式分开计算贡献。我们记
所以令
上面那个等式可以通过乘法分配律拆出括号得到。然后我们就得到了一个
容易发现,
然后直接快速幂即可。但是所求的是所有长度的方案之和,我们考虑在矩阵最后塞一维求和的即可,记这个求和的元素为
即:
那么就得到了矩阵中
以及自己对自己的贡献
#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;
}