AGC024E

The meaning of problems

Given \ (n-\) , \ (m \) , \ (MU \) , the number of sequence groups Q \ ((A_0, A_1, \ dots, A_n) \) is satisfied:

  • Sequence \ (Ai \) length exactly \ (I \)
  • All the elements are in the \ ([1, m] \ )
  • \ (A_ {i-1} \) is \ (A_i \) of the sequence
  • \ (A_i \) is lexicographically larger than \ (A_ {i-1} \)

The answer mode \ (mu \) output.
\ (n, k \ le 300 \)

Thinking

Is a fairy \ (dp \)
is a very important idea: the number of small to large insert
when we insert \ (i \) time, because the number of columns in all \ (\ le i \) , so \ ( I \) is inserted in all positions are possible,
for example: \ (1323 \) , consider inserting \ (3 \)
top: \ (31,323 \) ; a: \ (13,323 \) ; II: \ (13323 \) ; three: \ (13233 \) ; four: \ (13233 \)
but we also found that: will be counted weight. And when inserted \ (i \) in front when
we impose the same number must be plugged in behind it.
We recorded \ (dp [i] [j ] [k] \) represents the current conducted to the second \ (I \) operations, into a digital \ (J \) , there are \ (K \) after the number can be placed ( Note that this means that \ (k + 1 \) species, because at the beginning you can also put).
Transfer:

  • \ (dp [i] [j ] [k - 1] + = dp [i] [j] [k] (k> 0) \) represents the number of hold this position
  • \ (DP [I] [J +. 1] [I] + = DP [I] [J] [K] (K = 0) \) \ (J \) can not put, from \ (j + 1 \ ) new start to put (the absence of the same, so the number can all put)
  • \ (dp [i + 1] [j] [k] + = dp [i] [j] [k] * (k + 1) \) represents we place this number, put this number with a \ (k + 1 \ ) is selected.

The code is very short
reference

#include <bits/stdc++.h>
#define upd(x,y) x=(x+y>=mu?x+y-mu:x+y)
int n,m,mu,dp[305][305][305];
int main(){
    scanf("%d%d%d",&n,&m,&mu);
    dp[0][1][0]=1;
    for (int i=0;i<=n;i++)
        for (int j=1;j<=m;j++)
            for (int k=i;k>=0;k--){
                if (k) upd(dp[i][j][k-1],dp[i][j][k]);
                else upd(dp[i][j+1][i],dp[i][j][k]);
                upd(dp[i+1][j][k],1ll*dp[i][j][k]*(k+1)%mu);
            } 
    printf("%d",dp[n][m][0]);
}

Guess you like

Origin www.cnblogs.com/flyfeather6/p/11778415.html