其他分享
首页 > 其他分享> > CF756D Bacterial Melee

CF756D Bacterial Melee

作者:互联网

Description

Julia is conducting an experiment in her lab. She placed several luminescent bacterial colonies in a horizontal testtube. Different types of bacteria can be distinguished by the color of light they emit. Julia marks types of bacteria with small Latin letters "a", ..., "z".

The testtube is divided into nn consecutive regions. Each region is occupied by a single colony of a certain bacteria type at any given moment. Hence, the population of the testtube at any moment can be described by a string of nn Latin characters.

Sometimes a colony can decide to conquer another colony in one of the adjacent regions. When that happens, the attacked colony is immediately eliminated and replaced by a colony of the same type as the attacking colony, while the attacking colony keeps its type. Note that a colony can only attack its neighbours within the boundaries of the testtube. At any moment, at most one attack can take place.

For example, consider a testtube with population "babb". There are six options for an attack that may happen next:

the first colony attacks the second colony ( $1 \rightarrow 2$ ), the resulting population is "bbbb";
$2 \rightarrow 1$ , the result is "aabb";
$2 \rightarrow 3$ , the result is "baab";
$3 \rightarrow 2$ , the result is "bbbb" (note that the result is the same as the first option);
$3 \rightarrow 4$ or $4 \rightarrow 3$ , the population does not change.
The pattern of attacks is rather unpredictable. Julia is now wondering how many different configurations of bacteria in the testtube she can obtain after a sequence of attacks takes place (it is possible that no attacks will happen at all). Since this number can be large, find it modulo $10^9+7$

Solution

设$f_{i,j}$为当前考虑到第$i$位,第$i$位一定选且一共选了$j$位的方案数

可以枚举下一个字母并向最近的下一个该字母的位置累加

发现一个DP值由之前的一段区间之和构成,可以用前缀和维护转移

#include<iostream>
#include<cstdio>
using namespace std;
int n,pos[30],pre[5005],C[5005][5005],dp[5005][5005],ans;
const int mod=1e9+7;
char s[8005];
inline int read(){
    int f=1,w=0;
    char ch=0;
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')w=(w<<1)+(w<<3)+ch-'0',ch=getchar();
    return f*w;
}
int main(){
    n=read(),scanf("%s",s+1);
    for(int i=1;i<=n;i++)pre[i]=pos[s[i]-'a'],pos[s[i]-'a']=i;
    for(int i=0;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=i;j++)C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
    }
    for(int i=0;i<=n;i++){
        dp[i][0]=1;
        for(int j=1;j<=i;j++){
            dp[i][j]=(dp[i-1][j-1]+dp[i-1][j])%mod;
            if(pre[i])(dp[i][j]+=mod-dp[pre[i]][j-1])%=mod;
        }
    }
    for(int i=1;i<=n;i++)(ans+=1ll*dp[n][i]*C[n-1][i-1]%mod)%=mod;
    printf("%d\n",ans);
    return 0;
}
Bacterial Melee

 

标签:ch,5005,colony,Melee,result,CF756D,Bacterial,testtube,rightarrow
来源: https://www.cnblogs.com/JDFZ-ZZ/p/14429868.html