2019 cattle off the National Day party training day2 Circular Coloring (dynamic programming)

Topic links: https://ac.nowcoder.com/acm/contest/1107/D

The meaning of problems: there is a ring made of balls n + m, n Bobo desirable dyed black balls, the balls m colored white. Bobo right grouping of adjacent balls of the same color, he will re-colored length determined as the product group. He might want to know the color of the sum of weights. The answer to 1e9 + 7 mod.

Problem-solving ideas: personally feel that this question difficult. First, we find the same number painted in black and white areas. Then we can define dp [i] [j] of the j-th ball is divided into blocks of all possible i and the product. Then the state transition equation is:

dp [i] [j] = (ji + 1) * dp [i-1] [i-1] + (ji) * dp [i] [i-1] +. . . + 2 * dp [i-1] [j-2] + dp [i-1] [j-1]. Then we can consider each case the position of the minimum may be painted black piece found a common n + m possible, the final answer can be obtained. Be careful not to arbitrarily modulo, or may time out QAQ. . .

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e3+5;
int dp[maxn][maxn];
int inv[maxn];
const long long mod=1e9+7;
int main(){
    inv[1]=1;
    for(int i=2;i<=5000;i++){
        inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
    }
    dp[0][0]=1;
    for(int i=1;i<=5000;i++){
        int tem1=dp[i-1][i-1];
        int tem2=1ll*dp[i-1][i-1]*(i-1)%mod;
        for(int j=i;j<=5000;j++){
            dp[i][j]=(1ll*tem1*j-tem2+mod)%mod;
            tem1=(tem1+dp[i-1][j])%mod;
            tem2=(tem2+1ll*j*dp[i-1][j])%mod;
        }
    }
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        int tem=min(n,m);
        long long ans=0;
        for(int i=1;i<=tem;i++){
            ans=(ans+1ll*dp[i][n]*dp[i][m]%mod*inv[i])%mod;
        }
        printf("%lld\n",1ll*ans*(n+m)%mod);
    }
    return 0;
}

  

2019 cattle off the National Day party day2 training

Guess you like

Origin www.cnblogs.com/Zhi-71/p/11621118.html