P10323 理性(Rationality) 題解

JiaY19發表於2024-05-24

神秘題。

思路

\[\begin{aligned} RSS&=\sum_{i=1}^n(f(d_i)-v_i)^2\\ &=\sum_{i=1}^n(ad_i+b-v_i)^2\\ &=\sum_{i=1}^nv^2+((-2ad_i)-2b)v+a^2d_i^2+2abd_i+b^2\\ \end{aligned}\]

設:

\[\begin{aligned} S&=\sum_{i=1}^n d_i^2\\ T&=\sum_{i=1}^n d_i\\ U&=\sum_{i=1}^n d_iv_i\\ V&=\sum_{i=1}^n v_i\\ W&=\sum_{i=1}^n v_i^2\\ \end{aligned}\]

所以:

\[\begin{aligned} RSS&=\sum_{i=1}^nv^2+((-2ad_i)-2b)v+a^2d_i^2+2abd_i+b^2\\ &=a^2S+2abT-2aU-2bV+nb^2+W \end{aligned}\]

算這玩意的最值:

\[\begin{aligned} nb^2-2bV+2abT&=nb^2+(2aT-2V)b\\ &\ge -\frac{V^2-2TVa+T^2a^2}{n} \end{aligned}\]

那麼:

\[\begin{aligned} RSS&=a^2S-2aU-\frac{V^2-2TVa+T^2a^2}{n}+W\\ &=(S-\frac{T^2}{n})a^2+(\frac{2TV}{n}-2U)a-\frac{V^2}{n}+W\\ &\ge \frac{(SW-U^2)n-T^2W-SV^2+2TUV}{Sn-T^2} \end{aligned}\]

發現 \(S,T\) 是定值。

我們只需要算 \(W,U^2,V^2,UV\) 的期望即可。

這個可以簡單 dp。

Code

/*
  ! Though life is hard, I want it to be boiling.
  ! Created: 2024/05/03 19:51:38
*/
#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second
#define int long long
#define mp(x, y) make_pair(x, y)
#define eb(...) emplace_back(__VA_ARGS__)
#define fro(i, x, y) for (int i = (x); i <= (y); i++)
#define pre(i, x, y) for (int i = (x); i >= (y); i--)
inline void JYFILE19();

typedef pair<int, int> PII;

bool ST;
const int N = 1e6 + 10;
const int mod = 998244353;

int n, m, S, T, ans, num, d[N], v[N], l[N], r[N], f[N][7];

inline int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod, y /= 2;
  }
  return res;
}
inline int ask1(int x) {
  return x * (x + 1) / 2 % mod;
}
inline int ask1(int l, int r) {
  return (ask1(r) - ask1(l - 1) + mod) % mod;
}
inline int ask2(int x) {
  return (__int128)x * (x + 1) * (2 * x + 1) / 6 % mod;
}
inline int ask2(int l, int r) {
  return (ask2(r) - ask2(l - 1) + mod) % mod;
}

signed main() {
  JYFILE19();
  cin >> n, num = 1;
  fro(i, 1, n) cin >> d[i] >> l[i] >> r[i];
  fro(i, 1, n) (S += d[i] * d[i]) %= mod;
  fro(i, 1, n) (T += d[i]) %= mod;
  f[0][0] = 1;
  fro(i, 1, n) {
    int A1 = ask1(l[i], r[i]);
    int A2 = ask2(l[i], r[i]);
    f[i][0] = f[i - 1][0] * (r[i] - l[i] + 1) % mod;
    f[i][1] = (f[i - 1][1] * (r[i] - l[i] + 1) + A2 * f[i - 1][0]) % mod;
    f[i][2] = (f[i - 1][2] * (r[i] - l[i] + 1) + A1 * f[i - 1][0] % mod * d[i]) % mod;
    f[i][3] = f[i - 1][3] * (r[i] - l[i] + 1) % mod;
    f[i][3] = (A1 * f[i - 1][2] % mod * d[i] + f[i][3]) % mod;
    f[i][3] = (A1 * f[i - 1][2] % mod * d[i] + f[i][3]) % mod;
    f[i][3] = (A2 * f[i - 1][0] % mod * d[i] % mod * d[i] + f[i][3]) % mod;
    f[i][4] = (f[i - 1][4] * (r[i] - l[i] + 1) + A1 * f[i - 1][0]) % mod;
    f[i][5] = f[i - 1][5] * (r[i] - l[i] + 1) % mod;
    f[i][5] = (A1 * f[i - 1][4] + f[i][5]) % mod;
    f[i][5] = (A1 * f[i - 1][4] + f[i][5]) % mod;
    f[i][5] = (A2 * f[i - 1][0] + f[i][5]) % mod;
    f[i][6] = f[i - 1][6] * (r[i] - l[i] + 1) % mod;
    f[i][6] = (A1 * f[i - 1][2] + f[i][6]) % mod;
    f[i][6] = (A1 * f[i - 1][4] % mod * d[i] + f[i][6]) % mod;
    f[i][6] = (A2 * f[i - 1][0] % mod * d[i] + f[i][6]) % mod;
  }
  (ans += (S * f[n][1] - f[n][3]) % mod * n % mod) %= mod;
  (ans -= T * T % mod * f[n][1] % mod) %= mod;
  (ans -= S * f[n][5] % mod) %= mod;
  (ans += 2 * T % mod * f[n][6] % mod) %= mod;
  num = num * (S * n % mod - T * T % mod) % mod;
  num = power(num * f[n][0] % mod, mod - 2) % mod;
  ans = (ans % mod + mod) % mod;
  num = (num % mod + mod) % mod;
  cout << ans * num % mod << "\n";
  return 0;
}

bool ED;
inline void JYFILE19() {
  // freopen("", "r", stdin);
  // freopen("", "w", stdout);
  srand(random_device{}());
  ios::sync_with_stdio(0), cin.tie(0);
  double MIB = fabs((&ED - &ST) / 1048576.), LIM = 512;
  cerr << "MEMORY: " << MIB << endl, assert(MIB <= LIM);
}

相關文章