Xy_top
2024-11-20 20:04:17
观察到
具体思路如下:
每一轮取出
时间复杂度
代码:
#include <bits/stdc++.h>
#define For(i, a, b) for (int i = (a); i <= (b); i ++)
#define foR(i, a, b) for (int i = (a); i >= (b); i --)
using namespace std;
int n;
int a[1000005], f[1000005], pre[1000005];
vector <int> z[1000005], zz[1000005];
const int mod = 998244353;
signed main () {
cin >> n;
For (i, 1, n) {
cin >> a[i];
z[i].push_back (a[i]);
int x = sqrt (a[i]);
for (int j = 2; j <= x; j ++) {
if (a[i] % j == 0) {
z[i].push_back (j);
if (j * j != a[i]) z[i].push_back (a[i] / j);
}
}
int t = a[i];
for (int j = 2; j <= x; j ++) {
if (t % j == 0) {
zz[i].push_back (j);
while (t % j == 0) t /= j;
}
}
if (t != 1) zz[i].push_back (t);
}
f[1] = 1;
for (int j : z[1]) pre[j] = (pre[j] + 1) % mod;
For (i, 2, n) {
int sz = zz[i].size ();
for (int j = 1; j < (1 << sz); j ++) {
int num = 1, cnt = 0;
For (k, 0, sz - 1) {
if (j & (1 << k) ) {
num = num * zz[i][k];
++ cnt;
}
}
if (cnt & 1) f[i] = (f[i] + pre[num]) % mod;
else f[i] = (f[i] - pre[num] + mod) % mod;
}
for (int j : z[i]) pre[j] = (pre[j] + f[i]) % mod;
}
cout << f[n];
return 0;
}
卡空间差评,害我吃罚时。