結果

問題 No.2429 Happiest Tabehodai Ways
ユーザー 👑 chro_96chro_96
提出日時 2023-08-19 22:51:22
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 164 ms / 2,000 ms
コード長 3,999 bytes
コンパイル時間 327 ms
コンパイル使用メモリ 33,280 KB
実行使用メモリ 9,752 KB
最終ジャッジ日時 2023-09-03 03:06:26
合計ジャッジ時間 2,327 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
9,544 KB
testcase_01 AC 4 ms
9,636 KB
testcase_02 AC 4 ms
9,660 KB
testcase_03 AC 4 ms
9,592 KB
testcase_04 AC 3 ms
9,740 KB
testcase_05 AC 4 ms
9,664 KB
testcase_06 AC 3 ms
9,704 KB
testcase_07 AC 4 ms
9,660 KB
testcase_08 AC 4 ms
9,708 KB
testcase_09 AC 3 ms
9,728 KB
testcase_10 AC 3 ms
9,660 KB
testcase_11 AC 4 ms
9,740 KB
testcase_12 AC 4 ms
9,720 KB
testcase_13 AC 4 ms
9,604 KB
testcase_14 AC 4 ms
9,744 KB
testcase_15 AC 6 ms
9,704 KB
testcase_16 AC 4 ms
9,544 KB
testcase_17 AC 3 ms
9,636 KB
testcase_18 AC 3 ms
9,728 KB
testcase_19 AC 4 ms
9,660 KB
testcase_20 AC 4 ms
9,632 KB
testcase_21 AC 4 ms
9,724 KB
testcase_22 AC 3 ms
9,540 KB
testcase_23 AC 3 ms
9,636 KB
testcase_24 AC 3 ms
9,752 KB
testcase_25 AC 3 ms
9,700 KB
testcase_26 AC 4 ms
9,600 KB
testcase_27 AC 3 ms
9,632 KB
testcase_28 AC 4 ms
9,632 KB
testcase_29 AC 6 ms
9,660 KB
testcase_30 AC 5 ms
9,680 KB
testcase_31 AC 3 ms
9,740 KB
testcase_32 AC 3 ms
9,724 KB
testcase_33 AC 7 ms
9,712 KB
testcase_34 AC 7 ms
9,604 KB
testcase_35 AC 4 ms
9,640 KB
testcase_36 AC 14 ms
9,540 KB
testcase_37 AC 4 ms
9,728 KB
testcase_38 AC 4 ms
9,748 KB
testcase_39 AC 4 ms
9,664 KB
testcase_40 AC 4 ms
9,676 KB
testcase_41 AC 15 ms
9,540 KB
testcase_42 AC 4 ms
9,700 KB
testcase_43 AC 3 ms
9,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

long long power_mod (long long a, long long b, long long mod_num) {
  long long ans = 1LL;
  
  if (b > 0LL) {
    ans = power_mod(a, b/2LL, mod_num);
    ans = (ans * ans) % mod_num;
    if (b%2LL == 1LL) {
      ans = (ans * (a % mod_num)) % mod_num;
    }
  }
  
  return ans;
}

int main () {
  int n = 0;
  int k = 0;
  int c[1000] = {};
  int d[1000] = {};

  int res = 0;
  
  long long ans = 0LL;
  long long mod_num = 998244353LL;
  
  int d_max_cnt[1001][2] = {};
  int d_max[1001] = {};
  long long cnt[1001][501] = {};
  
  int max = -1;

  long long pow[1001][501] = {};
  long long fact[1001] = {};
  long long invf[1001] = {};
  
  res = scanf("%d", &n);
  res = scanf("%d", &k);
  for (int i = 0; i < n; i++) {
    res = scanf("%d", c+i);
  }
  for (int i = 0; i < n; i++) {
    res = scanf("%d", d+i);
  }
  
  fact[0] = 1LL;
  for (int i = 0; i < k; i++) {
    fact[i+1] = fact[i];
    fact[i+1] *= (long long)(i+1);
    fact[i+1] %= mod_num;
  }
  
  invf[k] = power_mod(fact[k], mod_num-2LL, mod_num);
  for (int i = k; i > 0; i--) {
    invf[i-1] = invf[i];
    invf[i-1] *= (long long)i;
    invf[i-1] %= mod_num;
  }
  
  for (int i = 0; i < n; i++) {
    if (d_max_cnt[c[i]][0] < d[i]) {
      d_max_cnt[c[i]][0] = d[i];
      d_max_cnt[c[i]][1] = 1;
    } else if (d_max_cnt[c[i]][0] == d[i]) {
      d_max_cnt[c[i]][1]++;
    }
  }
  
  for (int i = 2; i <= k; i++) {
    if (d_max_cnt[i][1] > 0) {
      pow[i][0] = 1LL;
      for (int j = 1; j*i <= k; j++) {
        pow[i][j] = pow[i][j-1]*((long long)d_max_cnt[i][1]);
        pow[i][j] %= mod_num;
      }
      for (int j = 1; j*i <= k; j++) {
        pow[i][j] *= invf[j];
        pow[i][j] %= mod_num;
      }
    }
  }
  
  for (int i = 0; i <= k; i++) {
    d_max[i] = -1;
  }
  
  d_max[0] = 0;
  cnt[0][0] = 1LL;
  for (int i = k; i > 1; i--) {
    if (d_max_cnt[i][1] > 0) {
      int tmpmax = k/i;
      for (int j = k-i; j > i; j--) {
        if (d_max[j] >= 0) {
          int max = (k-j)/i;
          for (int p = 1; p <= max; p++) {
            if (d_max[i*p+j] <= d_max[j]+d_max_cnt[i][0]*p) {
              int tmpmax = j/(i+1);
              if (d_max[i*p+j] < d_max[j]+d_max_cnt[i][0]*p) {
                if (d_max[i*p+j] >= 0) {
                  for (int q = 0; q*i <= i*p+j; q++) {
                    cnt[i*p+j][q] = 0LL;
                  }
                }
                d_max[i*p+j] = d_max[j]+d_max_cnt[i][0]*p;
              }
              for (int q = 1; q <= tmpmax; q++) {
                cnt[i*p+j][p+q] += cnt[j][q]*pow[i][p];
                cnt[i*p+j][p+q] %= mod_num;
              }
            }
          }
        }
      }
      for (int j = 1; j <= tmpmax; j++) {
        if (d_max[i*j] <= d_max_cnt[i][0]*j) {
          if (d_max[i*j] < d_max_cnt[i][0]*j) {
            if (d_max[i*j] >= 0) {
              for (int q = 0; q <= j; q++) {
                cnt[i*j][q] = 0LL;
              }
            }
            d_max[i*j] = d_max_cnt[i][0]*j;
          }
          cnt[i*j][j] += pow[i][j];
          cnt[i*j][j] %= mod_num;
        }
      }
    }
  }
  
  if (d_max_cnt[1][1] > 0) {
    long long pow = 1LL;
    for (int i = k; i >= 0; i--) {
      if (d_max[i] >= 0) {
        int tmp = d_max[i]+(k-i)*d_max_cnt[1][0];
        if (tmp >= max) {
          long long mul = (pow*invf[k-i])%mod_num;
          if (tmp > max) {
            max = tmp;
            ans = 0LL;
          }
          for (int j = 0; j <= i/2; j++) {
            ans += ((cnt[i][j]*mul)%mod_num)*fact[j+k-i];
            ans %= mod_num;
          }
        }
      }
      pow *= (long long)d_max_cnt[1][1];
      pow %= mod_num;
    }
  } else {
    for (int i = 0; i <= k; i++) {
      if (d_max[i] > max) {
        max = d_max[i];
        ans = 0LL;
      }
      if (d_max[i] == max) {
        for (int j = 0; j <= i/2; j++) {
          ans += cnt[i][j]*fact[j];
          ans %= mod_num;
        }
      }
    }
  }
  
  printf("%d\n%lld\n", max, ans);
  return 0;
}
0