結果

問題 No.1956 猫の額
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-05-24 02:32:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 31 ms / 10,000 ms
コード長 3,441 bytes
コンパイル時間 1,806 ms
コンパイル使用メモリ 120,812 KB
実行使用メモリ 13,204 KB
最終ジャッジ日時 2023-10-20 18:42:22
合計ジャッジ時間 3,105 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
4,596 KB
testcase_01 AC 12 ms
4,348 KB
testcase_02 AC 12 ms
4,588 KB
testcase_03 AC 16 ms
13,204 KB
testcase_04 AC 21 ms
13,204 KB
testcase_05 AC 20 ms
13,204 KB
testcase_06 AC 19 ms
13,204 KB
testcase_07 AC 17 ms
13,204 KB
testcase_08 AC 10 ms
4,348 KB
testcase_09 AC 10 ms
4,348 KB
testcase_10 AC 9 ms
4,348 KB
testcase_11 AC 17 ms
13,000 KB
testcase_12 AC 18 ms
13,044 KB
testcase_13 AC 7 ms
12,900 KB
testcase_14 AC 11 ms
12,860 KB
testcase_15 AC 12 ms
4,348 KB
testcase_16 AC 19 ms
5,936 KB
testcase_17 AC 12 ms
4,400 KB
testcase_18 AC 14 ms
5,584 KB
testcase_19 AC 31 ms
10,948 KB
testcase_20 AC 31 ms
10,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx")

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


unsigned M;
void Add(unsigned &t, unsigned f) {
  t += f;
  t = (t >= M) ? (t - M) : t;
}
void Sub(unsigned &t, unsigned f) {
  t -= f;
  t = (t >= M) ? (t + M) : t;
}


unsigned dp[46][50'010];
unsigned ans[100'010];

int main() {
  int N, K;
  int A[110];
  
  for (; ~scanf("%d%u%d", &N, &M, &K); ) {
    for (int i = 0; i < N; ++i) {
      scanf("%d", &A[i]);
    }
    sort(A, A + N);
    
    const bool rev = chmin(K, N - K);
    
    int ASum[110];
    ASum[0] = 0;
    for (int i = 0; i < N; ++i) {
      ASum[i + 1] = ASum[i] + A[i];
    }
    const int S = ASum[N];
    const int T = S / 2;
    
    int lbs[110], ubs[110];
    // K
    for (int i = 0; i <= N; ++i) {
      lbs[i] = max(0, K - (N - i));
      ubs[i] = min(i, K);
    }
#ifdef LOCAL
    memset(dp, 0, sizeof(dp));
#endif
    dp[0][0] = 1 % M;
    for (int i = 0; i < N; ++i) {
      for (int k = ubs[i + 1] - 1; k >= lbs[i]; --k) {
        const int mn = ASum[k];
        const int mx = min(ASum[i] - ASum[i - k], T - A[i]);
        for (int x = mn; x <= mx; ++x) {
          Add(dp[k + 1][x + A[i]], dp[k][x]);
        }
      }
    }
    for (int x = 0; x <= T; ++x) {
      ans[x] = dp[K][x];
    }
    
    // N - K
    if (K == N - K) {
      for (int x = 0; x <= T; ++x) {
        ans[S - x] = dp[K][x];
      }
    } else {
      for (int i = 0; i <= N; ++i) {
        lbs[i] = max(0, (N - K) - (N - i));
        ubs[i] = min(i, (N - K));
      }
      const int len = K + 1;
      memset(dp, 0, sizeof(dp));
      dp[0][0] = 1 % M;
      for (int i = 0; i < N; ++i) {
        for (int k = ubs[i + 1] - 1; k >= lbs[i]; --k) {
          const int pos0 = k % len;
          const int pos1 = (k + 1) % len;
          const int mn = ASum[k];
          const int mx = min(ASum[i] - ASum[i - k], T - A[i]);
          for (int x = mn; x <= mx; ++x) {
            Add(dp[pos1][x + A[i]], dp[pos0][x]);
          }
        }
        for (int k = lbs[i]; k < lbs[i + 1]; ++k) if (k + len <= N - K) {
          const int pos = k % len;
          memset(dp[pos], 0, (T + 1) * sizeof(dp[0][0]));
        }
      }
      {
        const int pos = (N - K) % len;
        for (int x = 0; x <= T; ++x) {
          ans[S - x] = dp[pos][x];
        }
      }
    }
    
    if (rev) {
      reverse(ans, ans + (S + 1));
    }
    
    for (int x = 1; x <= S; ++x) {
      if (x > 1) printf(" ");
      printf("%u", ans[x]);
    }
    puts("");
  }
  return 0;
}
0