ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF756D Bacterial Melee

2021-02-22 14:04:18  阅读:188  来源: 互联网

标签:ch 5005 colony Melee result CF756D Bacterial testtube rightarrow


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有