結果

問題 No.2866 yuusaan's Knapsack
ユーザー GOTKAKOGOTKAKO
提出日時 2024-08-30 21:52:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 2,867 bytes
コンパイル時間 2,201 ms
コンパイル使用メモリ 213,936 KB
実行使用メモリ 35,328 KB
最終ジャッジ日時 2024-08-31 01:27:27
合計ジャッジ時間 3,607 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 32 ms
33,920 KB
testcase_07 AC 25 ms
27,904 KB
testcase_08 AC 31 ms
31,872 KB
testcase_09 AC 28 ms
29,824 KB
testcase_10 AC 29 ms
31,488 KB
testcase_11 AC 21 ms
23,296 KB
testcase_12 AC 26 ms
28,416 KB
testcase_13 AC 24 ms
25,216 KB
testcase_14 AC 28 ms
28,672 KB
testcase_15 AC 31 ms
32,128 KB
testcase_16 AC 24 ms
25,344 KB
testcase_17 AC 30 ms
30,336 KB
testcase_18 AC 21 ms
22,144 KB
testcase_19 AC 34 ms
34,688 KB
testcase_20 AC 26 ms
27,264 KB
testcase_21 AC 28 ms
28,800 KB
testcase_22 AC 26 ms
27,136 KB
testcase_23 AC 32 ms
33,408 KB
testcase_24 AC 25 ms
26,496 KB
testcase_25 AC 29 ms
30,464 KB
testcase_26 AC 29 ms
35,328 KB
testcase_27 AC 3 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long mod = 998244353;
//入力が必ず-mod<a<modの時.
struct mint{
    long long v = 0;
    mint(){} mint(int a){v = a<0?a+mod:a;} mint(long long a){v = a<0?a+mod:a;}
    mint(unsigned long long a){v = a;}
    long long val(){return v;}
    void modu(){v %= mod;}
 
    mint repeat2mint(const long long a,long long b){
        mint ret = 1,p = a;
        while(b){
            if(b&1) ret *= p;
            p *= p; b >>= 1;
        }
        return ret;
    };
 
    mint &operator=(const mint &b) = default;
    mint operator-() const {return mint(0)-(*this);}
    mint operator+(const mint b){return mint(v)+=b;}
    mint operator-(const mint b){return mint(v)-=b;}
    mint operator*(const mint b){return mint(v)*=b;}
    mint operator/(const mint b){return mint(v)/=b;}
 
    mint operator+=(const mint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    mint operator-=(const mint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    mint operator*=(const mint b){v = v*b.v%mod; return *this;}
    mint operator/=(mint b){
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }
 
    mint operator++(int){*this += 1; return *this;}
    mint operator--(int){*this -= 1; return *this;}
    bool operator==(const mint b){return v == b.v;}
    bool operator!=(const mint b){return v != b.v;}
    bool operator>(const mint b){return v > b.v;}
    bool operator>=(const mint b){return v >= b.v;}
    bool operator<(const mint b){return v < b.v;}
    bool operator<=(const mint b){return v <= b.v;}
 
    mint pow(const long long x){return repeat2mint(v,x);}
    mint inv(){return mint(1)/v;}
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,W; cin >> N >> W;

    vector<vector<pair<int,mint>>> dp(N+1,vector<pair<int,mint>>(20009,{-1001001001,0}));
    auto update = [&](pair<int,mint> &a,pair<int,mint> &b,int v) -> void {
        if(b.first+v == a.first) a.second += b.second;
        else if(b.first+v > a.first) a = {b.first+v,b.second};
    };
    int zero = 10000;
    vector<pair<int,int>> WV(N);
    for(auto &[w,v] : WV) cin >> v >> w;
    sort(WV.begin(),WV.end());

    dp.at(0).at(zero) = {0,1};
    for(int i=0; i<N; i++){
        auto [w,v] = WV.at(i);
        for(int k=0; k<20009; k++){
            if(dp.at(i).at(k).second == 0) continue;
            update(dp.at(i+1).at(k),dp.at(i).at(k),0);
            if(k+w < 0) continue;
            if(k+w >= 20009) continue;
            update(dp.at(i+1).at(k+w),dp.at(i).at(k),v);
        }
    }
    pair<int,mint> ans = {-1001001001,0};
    for(int i=0; i<=zero+W; i++) update(ans,dp.at(N).at(i),0);
        
    cout << ans.first << " " << ans.second.v << endl;
}
0