Advertisement
JWRuixi

P10881 「KDOI-07」能量场 - I

Aug 17th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | Source Code | 0 0
  1. #ifdef LOCAL
  2. #include "stdafx.h"
  3. #else
  4. #include <bits/stdc++.h>
  5. #define IL inline
  6. #define LL long long
  7. #define eb emplace_back
  8. #define sz(v) ((int) (v).size())
  9. #define L(i, j, k) for (int i = (j); i <= (k); ++i)
  10. #define R(i, j, k) for (int i = (j); i >= (k); --i)
  11. #define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
  12. using namespace std;
  13.  
  14. using vi = vector<int>;
  15. #endif
  16.  
  17. #define me(f, x) memset(f, x, sizeof(f))
  18.  
  19. constexpr int N = 22;
  20. constexpr int M = (1 << 20) + 3;
  21. constexpr int P = 998244353;
  22. int n, a[N], m, A[N][N], id[N], deg[N], pr[N], sf[N];
  23.  
  24. IL int qpow (int b, int k = P - 2) {
  25.   int r = 1;
  26.   for (; k; k >>= 1, b = (LL)b * b % P) if (k & 1) r = (LL)r * b % P;
  27.   return r;
  28. }
  29.  
  30. int iv[N];
  31.  
  32. void init () {
  33.   iv[1] = 1;
  34.   L (i, 2, n) {
  35.     iv[i] = (LL)iv[P % i] * (P - P / i) % P;
  36.   }
  37.   L (i, 3, n) {
  38.     iv[i] = (LL)(P + 1) / 2 * iv[i] % P;
  39.   }
  40. }
  41.  
  42. int T[M], f[M][N];
  43.  
  44. IL int det () {
  45.   L (i, 1, m) {
  46.     L (j, 1, m) {
  47.       A[i][j] = (P - A[i][j]) % P;
  48.     }
  49.   }
  50.   L (i, 1, m) {
  51.     (A[i][i] += deg[i]) %= P;
  52.   }
  53.   int det = 1;
  54.   L (i, 1, m) {
  55.     int t = 0;
  56.     L (j, i, m) {
  57.       if (A[j][i]) {
  58.         t = j;
  59.         break;
  60.       }
  61.     }
  62.     if (!t) {
  63.       return 0;
  64.     }
  65.     if (i ^ t) {
  66.       swap(A[i], A[t]);
  67.       det = P - det;
  68.     }
  69.     det = (LL)det * A[i][i] % P;
  70.     int x = qpow(A[i][i]);
  71.     L (j, i + 1, m) {
  72.       int y = (LL)(P - A[j][i]) * x % P;
  73.       L (k, 1, m) {
  74.         A[j][k] = (A[j][k] + (LL)y * A[i][k]) % P;
  75.       }
  76.     }
  77.   }
  78.   return det % P;
  79. }
  80.  
  81. int main () {
  82.   ios::sync_with_stdio(0), cin.tie(0);
  83.   cin >> n;
  84.   init();
  85.   L (i, 0, n - 1) {
  86.     cin >> a[i];
  87.   }
  88.   L (t, 0, n - 1) {
  89.     me(f, 0);
  90.     f[1 << t][t] = 1;
  91.     L (s, 0, (1 << n) - 2) if (s >> t & 1) {
  92.       vi u, v;
  93.       int x = s;
  94.       while (x) {
  95.         int i = __builtin_ctz(x);
  96.         u.eb(i), x ^= 1 << i;
  97.       }
  98.       x = (1 << n) - 1 - s;
  99.       while (x) {
  100.         int i = __builtin_ctz(x);
  101.         v.eb(i), x ^= 1 << i;
  102.       }
  103.       for (int i : v) {
  104.         int ss = 0;
  105.         for (int j : u) {
  106.           ss = (ss + (LL)f[s][j] * (a[i] + a[j])) % P;
  107.         }
  108.         (f[s | (1 << i)][i] += ss) %= P;
  109.       }
  110.     }
  111.     L (s, 0, (1 << n) - 1) {
  112.       L (i, 0, n - 1) {
  113.         if (s >> i & 1) {
  114.           T[s] = (T[s] + (LL)f[s][i] * (a[i] + a[t])) % P;
  115.         }
  116.       }
  117.     }
  118.   }
  119.   L (s, 0, (1 << n) - 1) {
  120.     T[s] = (LL)T[s] * iv[__builtin_popcount(s)] % P;
  121.   }
  122.   int ns = T[(1 << n) - 1];
  123.   L (s, 0, (1 << n) - 2) {
  124.     int cnt = __builtin_popcount(s);
  125.     if (cnt > 2) {
  126.       m = 0;
  127.       L (i, 0, n - 1) {
  128.         id[i] = (s >> i & 1) ? 0 : ++m;
  129.       }
  130.       me(deg, 0);
  131.       me(A, 0);
  132.       L (i, 0, n - 1) {
  133.         L (j, 0, n - 1) {
  134.           int w = (a[i] + a[j]) % P;
  135.           (A[id[i]][id[j]] += w) %= P;
  136.           (deg[id[i]] += w) %= P;
  137.         }
  138.       }
  139.       pr[0] = 1;
  140.       L (i, 1, m) {
  141.         pr[i] = (LL)pr[i - 1] * deg[i] % P;
  142.       }
  143.       sf[m + 1] = 1;
  144.       R (i, m, 1) {
  145.         sf[i] = (LL)sf[i + 1] * deg[i] % P;
  146.       }
  147.       int ss = pr[m];
  148.       L (i, 1, m) {
  149.         int t = 1;
  150.         ss = (ss + (LL)(P - A[i][i]) * pr[i - 1] % P * sf[i + 1]) % P;
  151.         L (j, i + 1, m) {
  152.           LL w = ((LL)(P - A[i][j]) * A[j][i] + (LL)A[i][i] * A[j][j]) % P;
  153.           ss = (ss + (LL)w * pr[i - 1] % P * sf[j + 1] % P * t) % P;
  154.           t = (LL)t * deg[j] % P;
  155.         }
  156.       }
  157.       ns = (ns + (LL)ss * T[s]) % P;
  158.     }
  159.   }
  160.   cout << ns << '\n';
  161. }
  162. // I love WHQ!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement