Linear dp - hdu6578 classic dp

 The first multi-school field the first question, did the two questions before this type of dp, the state transition is generally pushed back from the current state of

Classic dp, but very slow time

/ * 
Define the location dp [t] [i] [ j] [k] t after filling out a representative of the former position, {0, 1, 2, 3} the last four numbers appear, 
after the order of t, i , (t> i> j> k) the number of program j, k, according to the four options of digits of t, can be obtained 
or four transfer. 
t is selected from the number of t-1 in this position: DP [t] [i] [j] [K] 
t selected from the number i of the position: DP [t] [t-1] [j] [K] 
t selected from the group j the positions of: DP [T] [T-. 1] [I] [k] 
T k the number of selected locations: dp [t] [t- 1] [i] [j] 
enumeration r [l] == t + 1 of all the conditions, if and only if all conditions are satisfied for the transfer 

last program number = sum {dp [n]} 
of the total time complexity of O (n4). Rolling one-dimensional, spatial complexity of O (N3)
 * / 
#include <bits / STDC ++ H.>
 The using  namespace STD;
 #define MAXN 110
 #define LL Long Long 
 #define MOD 998 244 353 
DP LL [ 2 ] [MAXN] [MAXN] [MAXN];
 int n-, m;
 struct Node{
    int l,x;
    Node(){}
    Node(int l,int x):l(l),x(x){}
};
vector<Node>v[maxn];

inline void update(ll &a,ll b){
    a=(a+b);
    while(a>=mod)a-=mod; 
}
int c;
void solve(){
    c=0;
    dp[c][0][0][0]=1;
    for(int t=1;t<=n;t++){
        c^=1;
        for(int i=0;i<=t;i++)
            for(int j=0;j<=i;j++)
                for(int k=0;k<=j;k++)
                    dp[c][i][j][k]=0;
        
        for(int i=0;i<t;i++)
            for(int j=0;j<=i;j++)
                for(int k=0;k<=j;k++){
                    update(dp[c][i][j][k],dp[c^1][i][j][k]);
                    update(dp[c][t-1][j][k],dp[c^1][i][j][k]); 
                    update(dp[c][t-1][i][k],dp[c^1][i][j][k]);
                    update(dp[c][t-1][i][j],dp[c^1][i][j][k]);
                }
        for(int p=0;p<v[t].size();p++){//枚举每个条件 
            int l=v[t][p].l,x=v[t][p].x;
            for(int i=0;i<t;i++)
                for(int j=0;j<=i;j++)
                    for(int k=0;k<=j;k++){
                        int cnt=1;
                        if(i>=l)cnt++;
                        if(j>=l)cnt++;
                        if(k>=l)cnt++;
                        if(cnt!=x)dp[c][i][j][k]=0;
                    }
        }
    }
}

int main(){
    //ios::sync_with_stdio(false);
    int t;cin>>t;
    while(t--){
        for(int i=0;i<maxn;i++)v[i].clear();
        
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            int l,r,x;
            scanf("%d%d%d",&l,&r,&x);
            v[r].push_back(Node(l,x));
        }
        solve();
        ll ans=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<=i;j++)
                for(int k=0;k<=j;k++)
                    ans+=dp[c][i][j][k],ans%=mod;
        cout<<ans<<'\n';
    }
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/11330169.html
DP