参考了其他大佬的题解。
求所有长度小于等于 ,值域在 的序列的元素乘积的因子个数之和。。
显然因子个数跟每个质因子有关,发现值域很小, 以内的质数只有 个,于是可以往状压质数状态的方向去想。记 以内有 个质数。
对于乘积 ,设他的质因子分解得到 ,那么因数个数为 ,如果对他再乘以一个数 ,,则 。
这个乘积的因数个数感觉不能直接从两个数得到,考虑类似乘法分配律的方式分开计算贡献。我们记 表示质因子属于 集合内的 。那么针对乘上一个数 ,因为:
所以令 表示 含有质因子 的个数,则有转移:
上面那个等式可以通过乘法分配律拆出括号得到。然后我们就得到了一个 的 dp。
容易发现, 奇大无比而这个 dp 的状态数比较少,考虑矩阵快速幂即可。矩阵的系数就是上面的递推式后面的系数,即矩阵元素 为:
然后直接快速幂即可。但是所求的是所有长度的方案之和,我们考虑在矩阵最后塞一维求和的即可,记这个求和的元素为 ,dp 转移为:
即:
那么就得到了矩阵中 的转移系数:
以及自己对自己的贡献 ,然后做完了。时间复杂度 。
#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;
}