题意:三种操作:添加0或者1到字符末尾,删除最右字符(若字符为空,则不变) 给出string s 问有多少种长度为N的操作,能得到s n,|s|<=5000 .
长度为L的串 共有2^L种 因为每种出现的概率相同,所以每种的方法数也相同
ans=dp[n][L] / 2^L.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int N=5e3+20;
ll n,m;
string s;
ll dp[N][N];
ll powmod(ll x,ll n)
{
ll s=1;
while(n)
{
if(n&1)
s=(s*x)%mod;
x=(x*x)%mod;
n>>=1;
}
return s;
}
int main()
{
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=0;i<=5000;i++)
{
for(int j=0;j<=5000;j++)
{
if(dp[i][j]==0) continue;
dp[i+1][j+1]=(dp[i+1][j+1]+(2ll*dp[i][j])%mod)%mod;
(dp[i+1][max(0,j-1)]+=dp[i][j]%mod)%=mod;
}
}
while(cin>>n>>s)
{
ll l=s.length();
ll ans=dp[n][l];
ll t=powmod(2ll,l);
ans=(ans*powmod(t,mod-2))%mod;
cout<<ans<<endl;
}
return 0;
}

7万+

被折叠的 条评论
为什么被折叠?



