[AT2370] Piling Up

Subject to the effect

Beginning with n colors of black and white ball, but do not know how many black and white, respectively, m operations, each to come up with a ball, then placed in each of a black and white ball, a ball and then come, out of the final the ball will form in sequence a sequence of colors, the color sequence how many seek

n, m or less 3000

Resolve

Classic title with fold line.

We might as well as the number of black balls ordinate plane, then every change in the number of operations each step of the black ball is the corresponding polyline rise and fall, every variation in height is 1. However, due to uncertainty of the initial number of black ball, we find some of the fold lines can be obtained by vertical translation, this actually corresponding to the fold line of the sequence of operations is repeated. So, we are forced to the lowest point of each polyline in the x-axis to avoid duplication.

Set \ (f [i] [j ] [0/1] \) represents the operation of the \ (I \) times, with a \ (J \) black balls, whether a \ (X \) axis (0 through 1 means no). We divided four cases discussed the transfer of DP:

  • Both times to take the black ball. Corresponds to the first fold line is decreased, rising, falling, height minus 1.
  • Both times to take the white ball. Corresponds to a fold line is increased, the height plus 1.
  • Then take preemptive black ball white ball. Corresponds to the broken line is the first fall and then rise, the last height unchanged.
  • White pellet was taken go black ball. Corresponds to the broken line is the first rise in the fall, height unchanged.

Note that the border problems during the transfer, the process must be moved in the fold line \ ([0, n] \ ) interval.

As the transfer of the third dimension, if the transfer is only from 0 over 0; if it is either transferred from a 1, or is encountered during movement of the fold line \ (X \) axis. Compare lengthy transfer equation, see specific code.

Code

#include <iostream>
#include <cstdio>
#define int long long
#define N 3002
using namespace std;
const int mod=1000000007;
int n,m,i,j,f[N][N][2];
int read()
{
    char c=getchar();
    int w=0;
    while(c<'0'||c>'9') c=getchar();
    while(c<='9'&&c>='0'){
        w=w*10+c-'0';
        c=getchar();
    }
    return w;
}
signed main()
{
    n=read();m=read();
    for(i=1;i<=n;i++) f[0][i][0]=1;
    f[0][0][1]=1;
    for(i=1;i<=m;i++){
        for(j=0;j<=n;j++){
            if(j<n){
                if(j) f[i][j][0]=(f[i][j][0]+f[i-1][j+1][0])%mod;
                f[i][j][1]=(f[i][j][1]+f[i-1][j+1][1]+(j==0)*f[i-1][j+1][0])%mod;
            }
            if(j>0){
                if(j!=1) f[i][j][0]=(f[i][j][0]+f[i-1][j-1][0])%mod;
                f[i][j][1]=(f[i][j][1]+f[i-1][j-1][1])%mod;
            }
            if(j<n){
                if(j) f[i][j][0]=(f[i][j][0]+f[i-1][j][0])%mod;
                f[i][j][1]=(f[i][j][1]+f[i-1][j][1])%mod;
            }
            if(j>0){
                if(j!=1) f[i][j][0]=(f[i][j][0]+f[i-1][j][0])%mod;
                f[i][j][1]=(f[i][j][1]+f[i-1][j][1]+(j==1)*f[i-1][j][0])%mod;
            }
        }
    }
    int ans=0;
    for(i=0;i<=n;i++) ans=(ans+f[m][i][1])%mod;
    printf("%lld\n",ans);
    return 0;
}

Guess you like

Origin www.cnblogs.com/LSlzf/p/12207892.html