O(n3) 的暴力,不知道哪里写挂了,求调/kel
#include <bits/stdc++.h>
using namespace std;
#define int long long
int mod = 998244353;
int k[100005], a[100005], cnt, z[100005], l[100005], r[100005];
int f[100005][10], g[100005][10], q[100005][10], er[25], st[25][100005];
int n;
int ask(int l, int r) {
int x = r - l + 1;
int lgg = log2(x);
int ans = 0;
if ((l + er[lgg] - 1) <= n)
ans = st[lgg][l];
ans = __gcd(ans, st[lgg][r - er[lgg] + 1]);
return ans;
}
signed main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i], st[0][i] = a[i];
int lg = log2(n);
er[0] = 1;
for (int i = 1; i <= 20; i++)
er[i] = er[i - 1] * 2;
for (int i = 1; i <= lg; i++)
for (int j = 1; j + er[i] - 1 <= n; j++)
st[i][j] = __gcd(st[i - 1][j], st[i - 1][j + er[i - 1]]);
int now = 0;
for (int i = 1; i <= n; i++) {
int g = __gcd(a[i], now);
if (g != now)
r[cnt] = i - 1, k[i] = ++cnt, l[cnt] = i;
else
k[i] = cnt, z[cnt] = g;
now = g;
}
r[cnt] = n;
for (int i = 1; i <= n; i++) {
f[i][1] = ask(1, i), g[i][1] = 1;
for (int j = 2; j <= 7; j++) {
for (int y = 1; y < i; y++) {
//if (g[y][j - 1])
g[i][j] += g[y][j - 1], f[i][j] += f[y][j - 1] + g[y][j - 1] * ask(y + 1, i), f[i][j] %= mod;
}
}
}
cout << f[n][7];
}