首页 > 其他分享 >[AtCoder] F - Knapsack for All Subsets

[AtCoder] F - Knapsack for All Subsets

时间:2023-02-21 01:22:18浏览次数:44  
标签:AtCoder subsets Subsets int testNumber nextInt Knapsack dp out

Problem Statement

 

dp[i][j]: the number of subsets of A[0, i] whose sum is j.

dp[0][0] = 1, there is only 1 way of not picking anything from an empty array to achieve sum 0.

Answer is dp[N][S]

 

State Transition:

case 1, not using A[i] : all the subsets of A[0, i - 1] are also subsets of A[0, i], so dp[i][j] += dp[i - 1][j] * 2;

case 2, using A[i], dp[i][j] += dp[i - 1][j - A[i]]; 

 

 

    static void solve(int testCnt) {
        for (int testNumber = 0; testNumber < testCnt; testNumber++) {
            int n = in.nextInt(), s = in.nextInt(), mod = 998244353;
            int[] a = in.nextIntArrayPrimitiveOneIndexed(n);
            long[][] dp = new long[n + 1][s + 1];
            dp[0][0] = 1;
            for(int i = 1; i <= n; i++) {
                for(int j = 0; j <= s; j++) {
                    dp[i][j] = addWithMod(dp[i][j], dp[i - 1][j] * 2, mod);
                    if(j >= a[i]) {
                        dp[i][j] = addWithMod(dp[i][j], dp[i - 1][j - a[i]], mod);
                    }
                }
            }
            out.println(dp[n][s]);
        }
        out.close();
    }

 

标签:AtCoder,subsets,Subsets,int,testNumber,nextInt,Knapsack,dp,out
From: https://www.cnblogs.com/lz87/p/17139523.html

相关文章

  • AtCoder Beginner Contest 289
    E-SwapPlaces题意一个简单无向图,每一个点为黑白色的其中一种。有2个人,x在点1,y在点n。在每一步中,每个人都同时向相邻点移动,要求2人移动到的格子颜色不能相通。......
  • [AtCoder Regular Contest 156][D. Xor Sum 5]
    题目链接:D-XorSum5题目大意:给定一个长度为\(N(1\leN\le1000)\)的数组\(A(0\leA_i\le1000)\),以及一个正整数\(K(1\leK\le10^{12})\)。现在要在这\(N\)个数......
  • The Number of Good Subsets
    TheNumberofGoodSubsetsYouaregivenanintegerarray nums .Wecallasubsetof nums good ifitsproductcanberepresentedasaproductofoneormo......
  • Count the Number of Square-Free Subsets
    CounttheNumberofSquare-FreeSubsetsYouaregivenapositiveinteger0-indexed array nums .Asubsetofthearray nums issquare-free iftheproduct......
  • Atcoder题解:Arc156_c
    数据范围\(10^5\),但是介绍一个\(O(n\logn)\)做法。我们考虑观察样例,发现样例都很小,而且\(\text{LCS}\)的长度都是\(1\),那么我们就猜答案最多为\(1\),并尝试去构造......
  • Toyota Programming Contest 2023 Spring Qual A(AtCoder Beginner Contest 288)[A - F]
    原比赛链接A-ManyA+BProblemsA-AC代码#include<bits/stdc++.h>usingnamespacestd;intt,a,b;intread(){ intx=0,w=1; charch=getchar(); w......
  • AtCoder Beginner Contest 272 题解
    AtCoderBeginnerContest272Solution目录AtCoderBeginnerContest272Solution更好的阅读体验戳此进入题面链接题面Luogu链接abcd跳了[ABC272E]AddandMex题面S......
  • AtCoder Beginner Contest 266 题解
    AtCoderBeginnerContest266Solution目录AtCoderBeginnerContest266Solution更好的阅读体验戳此进入题面链接题面Luogu链接abcd都没什么可说的[ABC266E]Throwi......
  • AtCoder Beginner Contest 289
    A-flip#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr);strings;......
  • Sky Inc, Programming Contest 2023(AtCoder Beginner Contest 289)E
    E-SwapPlaces题链考虑dp[i][j]表示第一个点到达i点第二个点到达j点的min然后bfs即可时间复杂度为状态数intdp[2010][2010],n,m,c[2010];//dp[i][j]表示到达(i,j)的m......