【BZOJ1079】【SCOI2008】Coloring scheme

Time Limit: 10 Sec Memory Limit: 162 MB

Description

  
  There are n wooden blocks lined up in a row, numbered 1~n from left to right. You have k colors of paint, and the i-th color of paint is enough to paint ci wood blocks.
All the paint is just enough to cover all the wood blocks, i.e. c1+c2+...+ck=n. It is ugly to have two adjacent blocks painted the same color, so you want to count
the coloring schemes of any two adjacent blocks with different colors.
  

Input

  
  The first row contains a positive integer k, and the second row contains k integers c1, c2, ... , ck.
  

Output

  
  Output an integer, the result of the total number of scenarios modulo 1,000,000,007.
  

Sample Input

  
  3
  1 2 3
  

Sample Output

  
  10
  

HINT

  
  100% of the data satisfy: 1 ​​<= k <= 15, 1 <= ci <= 5
  
  
  

Solution

  
  ​ Are you still thinking about state pressure? It won’t work, there are too many states.
  
​ See what else is small: \(c_i\) looks small.
  
If you think about it carefully, if two pigments can be used the same number of times, then they can be classified as one type of pigment.
  
  ​ Starting from the classification of the number of times that can be used, set the status \(f[a_1][a_2][a_3][a_4][a_5][x]\) , indicating that the pigments that have been used \(i\) times currently have \(a_i \) kinds, and the next step is to select the total number of pigments that can be used \(x\) times.
  
  ​ Simply use the memory search DP implementation from top to bottom. It is obvious where each state transitions from: you can pick one of 5 types of pigments, then the source of the transition is \(\{a_1-1,a_2,a_3,a_4,a_5\}\) , \(\{ a_1+1,a_2-1,a_3,a_4,a_5\}\) , \(\{a_1,a_2+1,a_3-1,a_4,a_5\}\) , \(\{a_1,a_2,a_3+ 1,a_4-1,a_5\}\) , \(\{a_1,a_2,a_3,a_4+1,a_5-1\}\) .
  
The key is the coefficient of the transition and the recursion parameter. Simply speaking, each type of pigment \(i\)There are \(a_i\) different options, but this does not take into account the restrictions given by the title requirements.
  
​ Note that the \(a_i\) of each state is relative to the information in the current state, while \(x\) is the limit given to you by the previous state . You enumerate which type of paint will be selected in the current step. When recursing, you must pass this type of paint as the next \(x\) , which means that when you choose the next layer, if you want to choose me For this type of paint selected by the layer, the number of choices you have available must be reduced by 1, otherwise it will conflict with me. The upper layer recursively passed \(x\) to this layer , indicating that the paint picked by the upper layer recursion is the first \(x\) class in the upper layer, then in this layer is the first \( x-1\) class (reduce 1 before use). If you enumerate the current layer to select class \(x-1\) pigments, the number of available options must be decremented by 1.
  
​ Need to sort out the thinking and control relationship, and it is not difficult to realize.
  
  
  

#include <cstdio>
#include <cstring>
using namespace std;
const int N=16,MOD=1e9+7;
int n,a[6],c[6];
int f[N][N][N][N][N][6];
int dfs(int a1,int a2,int a3,int a4,int a5,int x){
    int &F=f[a1][a2][a3][a4][a5][x];
    if(F!=-1) return F;
    F=0;
    if(a1)
        (F+=1LL*dfs(a1-1,a2,a3,a4,a5,1)*(a1-(x-1==1))%MOD)%=MOD;
    if(a2)
        (F+=1LL*dfs(a1+1,a2-1,a3,a4,a5,2)*(a2-(x-1==2))%MOD)%=MOD;
    if(a3)
        (F+=1LL*dfs(a1,a2+1,a3-1,a4,a5,3)*(a3-(x-1==3))%MOD)%=MOD;
    if(a4)
        (F+=1LL*dfs(a1,a2,a3+1,a4-1,a5,4)*(a4-(x-1==4))%MOD)%=MOD;
    if(a5)
        (F+=1LL*dfs(a1,a2,a3,a4+1,a5-1,5)*a5%MOD)%=MOD;
    return F;
}
int main(){
    freopen("input.in","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        c[x]++;
    }
    memset(f,-1,sizeof f);
    for(int i=0;i<=5;i++)
        f[0][0][0][0][0][i]=1;
    printf("%d\n",dfs(c[1],c[2],c[3],c[4],c[5],0));
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324461892&siteId=291194637