版本比较
【题解】[Math×Girl²] 英雄变奏曲
正文
增加 0 行删除 234 行
309 行未改动全部展开310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547309 行未改动全部展开故总复杂度为 $O(N^{5/4}\log N)$。注意常数因子对程序用时的影响,比如使用题目提示中给出的除法优化、类欧计算使用迭代而非递归等。同时注意到所有与 $g,h$ 的值无关的求和均不可能超过 `long long` 范围(大小大致为 $O(N^2\operatorname{polylog})$),可以酌情减少取模次数。::::info[std]```cpp#include <utility>#include <cstdio>#include <vector>#include <cmath>using std::vector;using ll = long long;using pii = std::pair < int, int >;const int MOD = 998244353;#define toLL(x) static_cast < ll > (x)#define mkp(x, y) std::make_pair(x, y)#define mkpp(f) mkp(f(g), f(h))vector < int > spf, mu, mu_S, mu2, mu2_S;vector < ll > tri;vector < double > inv;inline int add(int x, int y) { x += y; return x >= MOD ? x - MOD : x; }inline int sub(int x, int y) { return add(x, MOD - y); }inline int mul(int x, int y) { return toLL(x) * y % MOD; }inline int dv(int x, int d) { return static_cast < int > (x * inv[d] + 1e-9); }inline int norm(ll x) { x %= MOD; return x < 0 ? x + MOD : x; }template < class T > inline T min(T a, T b) { return a < b ? a : b; }template < class T > inline T max(T a, T b) { return a > b ? a : b; }inline pii operator + (pii x, pii y) { return mkp(add(x.first, y.first), add(x.second, y.second)); }inline pii operator - (pii x, pii y) { return mkp(sub(x.first, y.first), sub(x.second, y.second)); }struct Poly {vector < int > val, Bq;Poly(int _N) { val.resize(_N + 1); }void Init(int N) {Bq.resize(N + 1);int val_S = val[0];for(int x = 1; x <= N; x ++)Bq[x] = norm(val_S - toLL(x) * val[x]), val_S = add(val_S, val[x]);}inline int operator () (const int x) const { return val[x]; }inline friend int B(Poly& p, int x) { return p.Bq[x]; }inline friend int pref(Poly& p, int A, int d, int q) {return (toLL(A + 1) * p(q) + toLL(d) * B(p, q)) % MOD;}inline friend int pref(Poly& p, int A, int d) { return pref(p, A, d, dv(A, d)); }};void init(int N) {inv.resize(N + 1);for(int i = 1;i <= N; i ++) inv[i] = 1.0 / i;spf.resize(N + 1);for(int i = 2; i <= N; i ++)if(!spf[i]) for(int j = 2; j <= N / i; j ++) spf[i * j] = i;mu.resize(N + 1), mu[1] = 1;for(int i = 2; i <= N; i ++) {if(!spf[i]) mu[i] = -1;else {int ip = i / spf[i];mu[i] = (ip % spf[i]) ? -mu[ip] : 0;}}mu_S.resize(N + 1);for(int i = 1; i <= N; i ++) mu_S[i] = mu_S[i - 1] + mu[i];mu2.resize(N + 1);for(int i = 1; i <= N; i ++)for(int j = 1; j <= N / i; j ++)mu2[i * j] += mu[i] * mu[j];mu2_S.resize(N + 1);for(int i = 1; i <= N; i ++) mu2_S[i] = mu2_S[i - 1] + mu2[i];tri.resize(N + 1);for(int i = 1; i <= N; i ++) tri[i] = toLL(i) * (i + 1) / 2;}inline ll R(int M, Poly& g, Poly& h) {ll res = 0;for(int x = 1; (x << 1 | 1) <= M; x ++) {int rho = (x == 1) ? g(1) : sub(g(x - 1), g(x));if(!rho) continue;int Wm = dv(M - x, x + 1);for(int w = 1; w <= Wm; w ++) {int Zm = dv(M - w, x);res = (res + toLL(rho) * (pref(h, Zm, w) + MOD - h(1))) % MOD;}}return res;}inline ll f(int n, int a, int b, int c) {ll res = 0;for(;;) {if(a >= c) {int q = dv(a, c);res += tri[n - 1] * q, a -= q * c;}if(b >= c) {int q = dv(b, c);res += toLL(n) * q, b -= q * c;}int m = a * n + b;if(m < c) break;n = dv(m, c);b = m - n * c;std::swap(a, c);}return res;}inline pii G(Poly& g, Poly& h, int M, int X, int Y, int L, int R, int T) {pii res = mkp(0, 0);if(L > R) return res;T = min(T, R);for(int w = L; w <= T; w ++) {int Aw = dv(M - Y * w, X), q = dv(Aw, w);auto cop = [&](Poly& p) { return (toLL(Aw + 1) * p(q) + toLL(w) * B(p, q)) % MOD; };res = res + mkpp(cop);}for(int q, r, l = max(L, T + 1); l <= R; l = r + 1) {q = dv(M - Y * l, X * l), r = min(R, dv(M, X * q + Y));int len = r - l + 1;int c1 = norm(f(len, Y, M - Y * r, X) + len);int c2 = norm(toLL(l + r) * len / 2);auto cop = [&](Poly& p) { return (toLL(c1) * p(q) + toLL(c2) * B(p, q)) % MOD; };res = res + mkpp(cop);}return res;}inline ll C(int M, Poly& g, Poly& h) {ll res = 0;int Xm = static_cast < int > (sqrt(M));vector < int > Fg(Xm + 1), Fh(Xm + 1);vector < int > Qg(Xm + 1), Qh(Xm + 1);for(int x = 2; x <= Xm; x ++) {for(int i = 1; i <= x; i ++) {int xi = dv(x, i);Fg[i] = add(Fg[i - 1], pref(g, x, i, xi)), Fh[i] = add(Fh[i - 1], pref(h, x, i, xi));Qg[i] = add(Qg[i - 1], g(xi)), Qh[i] = add(Qh[i - 1], h(xi));}int T = 6 * static_cast < int > (sqrt(dv(M, x)));for(int y = 1; y < x; y ++) {pii coeff = mkp(0, 0);int wg = dv(x, y), R = M - x * (x + 1);if(R > 0) {int w0 = min(x, dv(R, y));coeff = coeff + G(g, h, M, x, y, 1, w0, T) - mkp(Fg[w0], Fh[w0]);}int w1 = dv(M - x, x + y);if(w1 > x) {coeff = coeff + G(g, h, M, x, y, x + 1, w1, T);int len = w1 - x;coeff = coeff - mkp(mul(len, g(1)), mul(len, h(1)));}int w2 = min(x - 1, dv(M - x * x, y));if(w2 > y) coeff = coeff + mkp(sub(Qg[w2], Qg[y]), sub(Qh[w2], Qh[y]));res = (res + toLL(g(wg)) * coeff.second + toLL(h(wg)) * coeff.first) % MOD;if(x * x + y * y <= M) res = (res + toLL(g(wg)) * h(wg)) % MOD;}}return res;}int solve(int N, Poly& g, Poly& h) {int res = 0;for(int m, r, l = 1; l <= N; l = r + 1) {m = dv(N, l), r = dv(N, m);int c1 = mu2_S[r] - mu2_S[l - 1], c2 = mu_S[r] - mu_S[l - 1];if(c1) res = norm(res + 2LL * c1 * C(m, g, h));if(c2) res = norm(res + toLL(c2) * R(m, g, h));}return res;}int main() {int N; scanf("%d", &N);Poly g(N), h(N);for(int i = 1; i <= N; i ++) scanf("%d", &g.val[i]);for(int i = 1; i <= N; i ++) scanf("%d", &h.val[i]);if(N < 3) { puts("0"); return 0; }g.Init(N), h.Init(N);init(N);int ans = solve(N, g, h);printf("%d\n", ans);return 0;}::::309 行未改动全部展开310311312313309 行未改动全部展开故总复杂度为 $O(N^{5/4}\log N)$。注意常数因子对程序用时的影响,比如使用题目提示中给出的除法优化、类欧计算使用迭代而非递归等。同时注意到所有与 $g,h$ 的值无关的求和均不可能超过 `long long` 范围(大小大致为 $O(N^2\operatorname{polylog})$),可以酌情减少取模次数。