結果

問題 No.2429 Happiest Tabehodai Ways
ユーザー k1suxuk1suxu
提出日時 2023-08-18 23:57:34
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 9,192 bytes
コンパイル時間 4,331 ms
コンパイル使用メモリ 286,376 KB
実行使用メモリ 72,000 KB
最終ジャッジ日時 2024-05-06 18:00:58
合計ジャッジ時間 10,833 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#include <bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i = 0; i < (int)n; i++)
#define FOR(n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i = (int)a; i < (int)b; i++)
#define all(x) x.begin(),x.end()
//#define mp make_pair
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>

template<typename T>
void chmax(T &a, const T &b) {a = (a > b? a : b);}
template<typename T>
void chmin(T &a, const T &b) {a = (a < b? a : b);}

using ll = long long;
using ld = long double;
using ull = unsigned long long;

const ll INF = numeric_limits<long long>::max() / 2;
const ld pi = 3.1415926535897932384626433832795028;
const ll mod = 998244353;
int dx[] = {1, 0, -1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, -1, 1};

#define int long long

template<long long MOD>
struct Modular_Int {
    long long x;

    Modular_Int() = default;
    Modular_Int(long long x_) : x(x_ >= 0? x_%MOD : (MOD-(-x_)%MOD)%MOD) {}

    long long val() const {
        return (x%MOD+MOD)%MOD;
    }
    long long get_mod() const {
        return MOD;
    }

    Modular_Int<MOD>& operator^=(long long d)  {
        Modular_Int<MOD> ret(1);
        long long nx = x;
        while(d) {
            if(d&1) ret *= nx;
            (nx *= nx) %= MOD;
            d >>= 1;
        }
        *this = ret;
        return *this;
    }
    Modular_Int<MOD> operator^(long long d) const {return Modular_Int<MOD>(*this) ^= d;}
    Modular_Int<MOD> pow(long long d) const {return Modular_Int<MOD>(*this) ^= d;}
    
    //use this basically
    Modular_Int<MOD> inv() const {
        return Modular_Int<MOD>(*this) ^ (MOD-2);
    }
    //only if the module number is not prime
    //Don't use. This is broken.
    // Modular_Int<MOD> inv() const {
    //     long long a = (x%MOD+MOD)%MOD, b = MOD, u = 1, v = 0;
    //     while(b) {
    //         long long t = a/b;
    //         a -= t*b, swap(a, b);
    //         u -= t*v, swap(u, v);
    //     }
    //     return Modular_Int<MOD>(u);
    // }

    Modular_Int<MOD>& operator+=(const Modular_Int<MOD> other) {
        if((x += other.x) >= MOD) x -= MOD;
        return *this;
    }
    Modular_Int<MOD>& operator-=(const Modular_Int<MOD> other) {
        if((x -= other.x) < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator*=(const Modular_Int<MOD> other) {
        long long z = x;
        z *= other.x;
        z %= MOD;
        x = z;
        if(x < 0) x += MOD;
        return *this;
    }
    Modular_Int<MOD>& operator/=(const Modular_Int<MOD> other) {
        return *this = *this * other.inv();
    }
    Modular_Int<MOD>& operator++() {
        x++;
        if (x == MOD) x = 0;
        return *this;
    }
    Modular_Int<MOD>& operator--() {
        if (x == 0) x = MOD;
        x--;
        return *this;
    }
    
    Modular_Int<MOD> operator+(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const Modular_Int<MOD> other) const {return Modular_Int<MOD>(*this) /= other;}
    
    Modular_Int<MOD>& operator+=(const long long other) {Modular_Int<MOD> other_(other); *this += other_; return *this;}
    Modular_Int<MOD>& operator-=(const long long other) {Modular_Int<MOD> other_(other); *this -= other_; return *this;}
    Modular_Int<MOD>& operator*=(const long long other) {Modular_Int<MOD> other_(other); *this *= other_; return *this;}
    Modular_Int<MOD>& operator/=(const long long other) {Modular_Int<MOD> other_(other); *this /= other_; return *this;}
    Modular_Int<MOD> operator+(const long long other) const {return Modular_Int<MOD>(*this) += other;}
    Modular_Int<MOD> operator-(const long long other) const {return Modular_Int<MOD>(*this) -= other;}
    Modular_Int<MOD> operator*(const long long other) const {return Modular_Int<MOD>(*this) *= other;}
    Modular_Int<MOD> operator/(const long long other) const {return Modular_Int<MOD>(*this) /= other;}

    bool operator==(const Modular_Int<MOD> other) const {return (*this).val() == other.val();}
    bool operator!=(const Modular_Int<MOD> other) const {return (*this).val() != other.val();}
    bool operator==(const long long other) const {return (*this).val() == other;}
    bool operator!=(const long long other) const {return (*this).val() != other;}

    Modular_Int<MOD> operator-() const {return Modular_Int<MOD>(0LL)-Modular_Int<MOD>(*this);}

    // friend constexpr istream& operator>>(istream& is, mint& x) noexcept {
    //     long long X;
    //     is >> X;
    //     x = X;
    //     return is;
    // }
    // friend constexpr ostream& operator<<(ostream& os, mint& x) {
    //     os << x.val();
    //     return os;
    // }
};

// const long long MOD_VAL = 1e9+7;
const long long MOD_VAL = 998244353;
using mint = Modular_Int<MOD_VAL>;

vector<mint> f = {1}, rf = {1};
void factor_init(long long n) {
    ++n;
    f.resize(n, 0);
    rf.resize(n, 0);
    f[0] = 1;
    repi(i, 1, n) f[i] = (f[i - 1] * i);
    rf[n-1] = f[n-1].inv();
    for(int i = n-1; i > 0; --i) rf[i-1] = rf[i] * i;
}
mint P(long long n, long long k) {
    if(n<k) {
        cerr << "\nAssertion Failed!!\n";
        cerr << "Expression N >= K, ";
        cerr << "where n=" << n << ",k=" << k << "\n\n";
        return 0;
    }
    while(n > f.size()-1) {
        f.push_back(f.back() * f.size());
        rf.push_back(f.back().inv());
    }
    return f[n] * f[n-k];
}
mint C(long long n, long long k) {
    if(n<k) {
        cerr << "\nAssertion Failed!!\n";
        cerr << "Expression N >= K, ";
        cerr << "where n=" << n << ",k=" << k << "\n\n";
        return 0;
    }
    while(n > f.size()-1) {
        f.push_back(f.back() * f.size());
        rf.push_back(f.back().inv());
    }
    return f[n]*rf[n-k]*rf[k];
}
mint H(long long n, long long k) {
    assert(n>=1);
    return C(n+k-1, k);
}
mint Cat(long long n) {
    return C(2*n, n)-C(2*n, n-1);
}

void solve() {
    int n, k;
    cin >> n >> k;
    vi c(n), d(n);
    FOR(n) cin >> c[i];
    FOR(n) cin >> d[i];

    vector<int> order(n);
    iota(all(order), 0);
    sort(all(order), [&](int i, int j) {
        if(c[i] != c[j]) return c[i] < c[j];
        return d[i] > d[j];
    });
    vector<int> duplicate;
    {
        vi nc, nd;
        for(int i : order) {
            nc.push_back(c[i]);
            nd.push_back(d[i]);
        }
        c = nc, d = nd;
        nc.clear();
        nd.clear();
        nc.push_back(c[0]);
        nd.push_back(d[0]);
        duplicate.push_back(1);
        FOR(n-1) {
            if(c[i] != c[i+1]) {
                nc.push_back(c[i+1]);
                nd.push_back(d[i+1]);
                duplicate.push_back(1);
            }else if(d[i] == d[i+1]) {
                duplicate.back() += 1;
            }
        }
        c = nc;
        d = nd;
        n = (int)c.size();
    }

    //dp[i][j] = i番目までで満腹度がjであって食べた料理の個数がk個であるときの
    //           first: 幸福度の最大値, second: 通り数

    vector<vector<mint>> coeff(n, vector<mint>(1001));
    rep(i, n) {
        coeff[i][0] = 1;
        rep(j, 1000) {
            coeff[i][j+1] = coeff[i][j] * duplicate[i];
        }
    }

    vector<vector<vector<pair<int, mint>>>> dp(2, vector<vector<pair<int, mint>>>(k+1, vector<pair<int, mint>>(k+1, make_pair(0, 0))));
    dp[0][0][0] = make_pair(0, 1);

    rep(i, n) {
        int now = i&1;
        int nxt = 1^now;
        dp[nxt] = dp[now];

        for(int j = 1; j*c[i] <= k; ++j) {
            for(int l = 0; l <= k-j*c[i]; ++l) {
                for(int m = 0; m <= k-j; ++m) if(dp[now][l][m].second != 0) {
                    int happiness = dp[now][l][m].first + j*d[i];

                    if(dp[nxt][l+j*c[i]][m+j].first < happiness) {
                        dp[nxt][l+j*c[i]][m+j].first = happiness;
                        dp[nxt][l+j*c[i]][m+j].second = dp[now][l][m].second * H(m+1, j) * coeff[i][j];
                    }else if(dp[nxt][l+j*c[i]][m+j].first == happiness) {
                        dp[nxt][l+j*c[i]][m+j].second += dp[now][l][m].second * H(m+1, j) * coeff[i][j];
                    }
                }
            }
        }
    }

    int bit_id = n&1;
    int mx = 0;
    mint ans = 0;
    rep(i, k+1) rep(j, k+1) {
        if(mx < dp[bit_id][i][j].first) {
            mx = dp[bit_id][i][j].first;
            ans = dp[bit_id][i][j].second;
        }else if(mx == dp[bit_id][i][j].first) {
            ans += dp[bit_id][i][j].second;
        }
    }

    cout << mx << endl;
    cout << ans.val() << endl;

    // for(auto e : c) cout << e << " "; cout << "\n";
    // for(auto e : d) cout << e << " "; cout << "\n";
}

signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    solve();
    return 0;
}
0