結果

問題 No.1963 Subset Mex
ユーザー RiverHamsterRiverHamster
提出日時 2022-07-13 01:19:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 4,000 ms
コード長 2,329 bytes
コンパイル時間 420 ms
コンパイル使用メモリ 54,048 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 04:36:59
合計ジャッジ時間 2,040 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <vector>
#include <cassert>
using namespace std;

using ll = long long;
using ull = unsigned long long;

#define LOG(f...) fprintf(stderr, f)
// #define DBG(f...) printf(f)
#define DBG(f...) void()
#define all(cont) begin(cont), end(cont)

const int N = 105;
const int M = 998244353;

void inc(int &x, int y) { if ((x += y) >= M) x -= M; }

int a[N];
int dp[2][N][N];
int cost[N];

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0, x; i < n; ++i)
    scanf("%d", &x), ++a[x];

  int (*f)[N] = dp[0], (*g)[N] = dp[1];
  f[0][0] = 1;

  int ans = (a[0] != n) + n, les = n;

  for (int i = 100; i; --i, swap(f, g)) {
    DBG("i = %d\n", i);
    memset(g, 0, sizeof(dp[0]));
    les -= a[i];
    for (int req = 0, li = n / (i + 1); req <= li; ++req)
      for (int cnt = 0, li = a[i] + n / i; cnt <= li; ++cnt) {
        auto tf = [&](int r, int e, int val) {
          if (r <= n && e <= n) inc(g[r][e], val);
        };

        int c = req + cnt;

        if (c <= a[i]) {
          for (int ext = 0; ext <= n; ++ext)
            tf(req, ext + a[i] - c, f[req][ext]);
        }
        else {
          for (int ext = 0; ext <= n; ++ext)
            tf(req + c - a[i], ext, f[req][ext]);
        }
      }

    for (int c = 1; c <= a[i]; ++c)
      for (int ext = 0; ext <= n; ++ext) {
        int e = ext + a[i] - c;
        int c0 = e + 1 + a[0], req = 1;
        for (int j = i - 1; j; --j) {
          if (a[j] >= req)
            c0 += a[j] - req;
          else
            req += req - a[j];
          if (req > n) break;
        }

        DBG("e = %d : %d * %d\n", e, f[0][ext], e + les + (req <= c0));
        DBG("req = %d c0 = %d\n", req, c0);

        ans = (ans + (ll)f[0][ext] * (e + les + (req <= c0))) % M;
        if (!ext && les == a[0] && c == a[i] && (les || (req <= c0 && !a[0])))
          ans = (ans + M - 1) % M;
      }

    for (int req = 0; req <= n; ++req)
      for (int ext = 0; ext <= n; ++ext)
        if (g[req][ext]) DBG("g[%d][%d] = %d\n", req, ext, g[req][ext]);
    DBG("ans = %d\n", ans);
  }

  for (int req = 1; req <= n; ++req)
    for (int ext = max(0, req - a[0]); ext <= n; ++ext)
      ans = (ans + (ll)f[req][ext] * (ext + a[0] - req + 1)) % M;
  printf("%d\n", ans);
}
0