Educational Codeforces Round 39: F. Fibonacci String Subsequences(区间DP)

time limit per test  3.5 seconds
memory limit per test  256 megabytes
input  standard input
output  standard output

You are given a binary string s (each character of this string is either 0 or 1).

Let's denote the cost of string t as the number of occurences of s in t. For example, if s is 11 and t is 111011, then the cost of t is 3.

Let's also denote the Fibonacci strings sequence as follows:

  • F(0) is 0;
  • F(1) is 1;
  • F(i) = F(i - 1) + F(i - 2) if i > 1, where  +  means the concatenation of two strings.

Your task is to calculate the sum of costs of all subsequences of the string F(x). Since answer may be large, calculate it modulo 109 + 7.

Input

The first line contains two integers n and x (1 ≤ n ≤ 1000 ≤ x ≤ 100) — the length of s and the index of a Fibonacci string you are interested in, respectively.

The second line contains s — a string consisting of n characters. Each of these characters is either 0 or 1.

Output

Print the only integer — the sum of costs of all subsequences of the string F(x), taken modulo 109 + 7.

Examples
input
Copy
2 4
11
output
14
input
Copy
10 100
1010101010
output
553403224


dp[p][x][y]表示s串的[x, y]部分在F(p)的所有子串中出现多少次

那么有dp[p][i][j] = ∑(dp[p-1][i][k]*dp[p-2][k+1][j], i≤k<j)+dp[p-1][i][j]+dp[p-2][i][j]

其中当i=1或者j=n时

dp[p][i][j] += (2^len(p-1)-1)*dp[p-2][i][j] 或者 (2^len(p-2)-1)*dp[p-1][i][j]


#include<stdio.h>
#define mod 1000000007
#define LL long long
char str[105];
LL dp[105][105][105], F[105] = {1,1};
LL Pow(LL x, LL y)
{
	LL ans = 1;
	while(y)
	{
		if(y%2)
			ans = ans*x%mod;
		x = x*x%mod;
		y /= 2;
	}
	return ans;
}
int main(void)
{
	int n, m, i, j, k, p, len;
	scanf("%d%d%s", &n, &m, str+1);
	for(i=1;i<=n;i++)
	{
		if(str[i]=='1')
			dp[1][i][i] = 1;
		else
			dp[0][i][i] = 1;
	}
	for(i=2;i<=m;i++)
		F[i] = (F[i-1]+F[i-2])%(mod-1);
	for(p=2;p<=m;p++)
	{
		for(len=1;len<=n;len++)
		{
			for(i=1;i+len-1<=n;i++)
			{
				j = i+len-1;
				dp[p][i][j] = (dp[p-1][i][j]+dp[p-2][i][j])%mod;
				for(k=i;k<=j-1;k++)
					dp[p][i][j] = (dp[p][i][j]+dp[p-1][i][k]*dp[p-2][k+1][j])%mod;
				if(i==1)
					dp[p][i][j] = (dp[p][i][j]+(Pow(2, F[p-1])-1)*dp[p-2][i][j])%mod;
				if(j==n)
					dp[p][i][j] = (dp[p][i][j]+dp[p-1][i][j]*(Pow(2, F[p-2])-1))%mod;
			}
		}
	}
	printf("%lld\n", dp[m][1][n]);
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值