Submission #53066926


Source Code Expand

/*
AC
*/
#include<iostream>
#include<string>
#include<queue>
#include<vector>
#include<cassert>
#include<random>
#include<set>
#include<map>
#include<cassert>
#include<unordered_map>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
const int inf=1<<30;
const ll INF=1LL<<62;
typedef pair<ll,ll> P;
typedef pair<int,P> PP; 


const int mod = 998244353;
struct mint {
  long long x; // typedef long long long long;
  mint(long long x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}//後ろのconstはメンバxを変更しないことを示す
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  bool operator==(const mint a) const {return a.x==x;}
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};

std::istream& operator>>(std::istream& is, mint& a) { return is >> a.x;}
std::ostream& operator<<(std::ostream& os, const mint& a) { return os << a.x;}



struct combination {
  std::vector<mint> fact, ifact;
  combination(int n):fact(n+1),ifact(n+1) {
    assert(n < mod);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
  }
  mint operator()(int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact[n]*ifact[k]*ifact[n-k];
  }
};

mint dp[3005];

int main(){
    int N,S;
    cin>>N>>S;
    vector<int> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }

    mint ans=0;

    for(int i=N-1;i>=0;i--){
        dp[0]+=1;
        for(int j=S;j>=0;j--){
            if(j-A[i]>=0){
                dp[j]+=dp[j-A[i]];
            }
        }
        ans+=dp[S];
    }
    
    cout<<ans<<endl;
}

Submission Info

Submission Time
Task F - Knapsack for All Segments
User HIcoder
Language C++ 20 (gcc 12.2)
Score 600
Code Size 2448 Byte
Status AC
Exec Time 7 ms
Memory 3712 KiB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 23
Set Name Test Cases
sample sample01, sample02, sample03
All 11, 12, 13, 14, 15, 21, 22, 23, 24, 25, 31, 32, 33, 34, 35, 41, 42, 43, 44, 45, sample01, sample02, sample03
Case Name Status Exec Time Memory
11 AC 1 ms 3608 KiB
12 AC 1 ms 3508 KiB
13 AC 1 ms 3544 KiB
14 AC 1 ms 3548 KiB
15 AC 1 ms 3544 KiB
21 AC 1 ms 3520 KiB
22 AC 4 ms 3508 KiB
23 AC 3 ms 3428 KiB
24 AC 4 ms 3644 KiB
25 AC 1 ms 3508 KiB
31 AC 3 ms 3428 KiB
32 AC 4 ms 3560 KiB
33 AC 4 ms 3708 KiB
34 AC 4 ms 3504 KiB
35 AC 4 ms 3708 KiB
41 AC 7 ms 3492 KiB
42 AC 7 ms 3432 KiB
43 AC 7 ms 3512 KiB
44 AC 7 ms 3712 KiB
45 AC 7 ms 3652 KiB
sample01 AC 1 ms 3504 KiB
sample02 AC 1 ms 3548 KiB
sample03 AC 1 ms 3616 KiB