J. Stone game (knapsack problem)

Title: https: //nanti.jisuanke.com/t/41420

Definition of DP [x] [y] x represents the number of the first program to the last number is y and the composition can

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=303;
const int M=150000;
const int mod=1e9+7;
int dp[N][M];
int a[N];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            sum+=a[i];
        }
        sort(a+1,a+1+n);
        for(int i=0;i<=sum;i++)
            dp[n+1][i]=0;
        int ans=0;
        dp[n+1][0] = . 1 ;
         for ( int I = n-; I> = . 1 ; i-- ) {
             for ( int J = 0 ; J <= SUM; J ++ ) { 
                DP [I] [J] = DP [I + . 1 ] [ J];
                 IF (J> = a [I]) { // just enough to increase the current a [i], and as J 
                    DP [I] [J] + DP = [I + . 1 ] [J- a [I] ];
                     // before addition because dp [i] [j] may have a plurality of sources, the conditions have to select from a plurality of sources meet the 
                    IF (J> = (SUM-J) && ABS (J-(SUM-J )) <= A [I]) 
                        ANS + DP = [I + . 1 ] [J- A [I]];
                        ans%=mod;
                }
                dp[i][j]%=mod;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/starve/p/11525023.html