luogu P1373 Small a and uim big escape dynamic programming

Due to the decadent behavior of only punching in and not doing questions in Luogu for a few days, my Luogu account has been successfully dropped to a green name...
so I will post some questions on Luogu in the past few days to make a move and practice hands
.
Use dp [i] [j] [k] [0/1] dp[i][j][k][0/1]D P [ I ] [ J ] [ K ] [ 0 / . 1 ] represents the current comeiii linejjThe number of two magic liquids in the j column? ? DifferencekkWhen there are k , it is the turn of the small A/small uim to take the number of plans is
a bit long...actually it isi ji\,\,jij means wherekkk represents the difference in the number of magic liquids between the two. 0 is small A and 1 is small uim.
It is easy to seedp dpdp式:
d p [ i ] [ j ] [ l ] [ 0 ] + = d p [ i − 1 ] [ j ] [ ( l − s q r [ i ] [ j ] + k ) % k ] [ 1 ] ; dp[i][j][l][0]+=dp[i-1][j][(l-sqr[i][j]+k)\%k][1]; dp[i][j][l][0]+=dp[i1 ] [ j ] [ ( lsqr[i][j]+k)%k][1];
d p [ i ] [ j ] [ l ] [ 0 ] + = d p [ i ] [ j − 1 ] [ ( l − s q r [ i ] [ j ] + k ) % k ] [ 1 ] ; dp[i][j][l][0]+=dp[i][j-1][(l-sqr[i][j]+k)\%k][1]; dp[i][j][l][0]+=dp[i][j1][(lsqr[i][j]+k)%k][1];
d p [ i ] [ j ] [ l ] [ 1 ] + = d p [ i − 1 ] [ j ] [ ( l + s q r [ i ] [ j ] + k ) % k ] [ 0 ] ; dp[i][j][l][1]+=dp[i-1][j][(l+sqr[i][j]+k)\%k][0]; dp[i][j][l][1]+=dp[i1 ] [ j ] [ ( l+sqr[i][j]+k)%k][0];
d p [ i ] [ j ] [ l ] [ 1 ] + = d p [ i ] [ j − 1 ] [ ( l + s q r [ i ] [ j ] + k ) % k ] [ 0 ] ; dp[i][j][l][1]+=dp[i][j-1][(l+sqr[i][j]+k)\%k][0]; dp[i][j][l][1]+=dp[i][j1][(l+sqr[i][j]+K ) % K ] [ 0 ] ;
smallbbwaste directly on the code length

#include<bits/stdc++.h>
using namespace std;
#define reg register
const int mod=1e9+7;
inline void read(int &x){
    
    
    int s=0,w=1;char ch=getchar();
    while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){
    
    s=(s<<3)+(s<<1)+(ch&15);ch=getchar();}
    x=s*w;
}
int n,m,k,ans,dp[801][801][16][2],sqr[801][801];
int main(){
    
    
    read(n),read(m),read(k);k++;
    for(reg int i=1;i<=n;i++){
    
    
        for(reg int j=1;j<=m;j++){
    
    
            read(sqr[i][j]);
            sqr[i][j]%=k;
            dp[i][j][sqr[i][j]][0]=1;
        }
    }
    for(int i=1;i<=n;i++){
    
    
        for(int j=1;j<=m;j++){
    
    
            for(int l=0;l<k;l++){
    
    
                if(i>1)(dp[i][j][l][0]+=dp[i-1][j][(l-sqr[i][j]+k)%k][1])%=mod;
                if(j>1)(dp[i][j][l][0]+=dp[i][j-1][(l-sqr[i][j]+k)%k][1])%=mod;
                if(i>1)(dp[i][j][l][1]+=dp[i-1][j][(l+sqr[i][j]+k)%k][0])%=mod;
                if(j>1)(dp[i][j][l][1]+=dp[i][j-1][(l+sqr[i][j]+k)%k][0])%=mod;
            }
        }
    }
    for(int i=1;i<=n;i++){
    
    
        for(int j=1;j<=m;j++){
    
    
            (ans+=dp[i][j][0][1])%=mod;
        }
    }
    printf("%d\n",ans);
}

Guess you like

Origin blog.csdn.net/dhdhdhx/article/details/102490197