Description

题库链接

定义 \(F(x)\) 为 \(F(x-1)\) 与 \(F(x-2)\) 的连接(其中 \(F(0) = "0",F(1) = "1"\) )。给出一个长度为 \(n\) 的 \(01\) 字符串 \(s\) ,询问 \(s\) 在 \(F(x)\) 的所有子序列中出现了多少次。

\(1\leq n\leq 100,0\leq x\leq 100\)

Solution

首先 \(F(x)\) 是递归来定义的,显然我们可以递推来计算答案。

记 \(f_{l,r,i}\) 表示 \(F(i)\) 的所有子串中 \(s_{l\sim r}\) 出现的次数。

来考虑转移,一共有三部分:

  1. \(l\sim r\) 完全在 \(F(i-1)\) 中。此时若 \(r=n\) ,那么可以在 \(F(i-2)\) 中乱选,则有 \(2^{len(F(i-2))}\) 种;若 \(r\neq n\) ,因为不能在后面乱选,所以贡献只有 \(1\) 倍。
  2. \(l\sim r\) 完全在 \(F(i-2)\) 中。这种讨论和 1. 中相同。
  3. 最后分为在不同的两段中,设 \(s_{l\sim k}\) 在 \(F(i-1)\) 中;设 \(s_{k+1\sim r}\) 在 \(F(i-2)\) 中。则 \(f_{l,r,i}=f_{l,k,i-1}\times f_{k+1,r,i-2}\) 。

Code

//It is made by Awson on 2018.3.13
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 100, yzh = 1e9+7;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); } int n, x, g[N+5];
int f[N+5][N+5][N+5];
char ch[N+5]; int quick_pow(int a, int b) {
int ans = 1;
while (b) {
if (b&1) ans = 1ll*ans*a%yzh;
b >>= 1, a = 1ll*a*a%yzh;
}
return ans;
}
void work() {
read(n), read(x); scanf("%s", ch+1); g[0] = g[1] = 1;
for (int i = 2; i <= x; i++) g[i] = (g[i-1]+g[i-2])%(yzh-1);
for (int i = 0; i <= x; i++) g[i] = quick_pow(2, g[i]);
for (int i = 1; i <= n; i++)
if (ch[i] == '0') f[i][i][0] = 1; else f[i][i][1] = 1;
for (int i = 2; i <= x; i++) {
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++) {
if (r == n) (f[l][r][i] += 1ll*f[l][r][i-1]*g[i-2]%yzh) %= yzh;
else (f[l][r][i] += f[l][r][i-1]) %= yzh;
if (l == 1) (f[l][r][i] += 1ll*f[l][r][i-2]*g[i-1]%yzh) %= yzh;
else (f[l][r][i] += f[l][r][i-2]) %= yzh;
for (int k = l; k < r; k++)
(f[l][r][i] += 1ll*f[l][k][i-1]*f[k+1][r][i-2]%yzh) %= yzh;
}
}
writeln(f[1][n][x]);
}
int main() {
work(); return 0;
}

[Codeforces 946F]Fibonacci String Subsequences的更多相关文章

  1. codefroces 946F Fibonacci String Subsequences

    Description定义$F(x)$为$F(x−1)$与$F(x−2)$的连接(其中$F(0)="0"$,$F(1)="1"$)给出一个长度为$n$的$01$ ...

  2. HDU 1708 简单dp问题 Fibonacci String

    Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDOJ(HDU) 1708 Fibonacci String

    Problem Description After little Jim learned Fibonacci Number in the class , he was very interest in ...

  4. hdu 1708 Fibonacci String

    Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. Fibonacci String(hdu 1708)

    Fibonacci String Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

  7. Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力

    Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consist ...

  8. Codeforces 797C - Minimal string

    C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...

  9. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

随机推荐

  1. C作业--数据类型

    一.PTA实验作业 题目1:7-3 倒顺数字串 1. 本题PTA提交列表 2. 设计思路(伪代码) (1)本题是要求输入倒顺序数串,首先看到这种题肯定是需要用到循环,那就先定一个整形i来进行循环,n是 ...

  2. C语言博客作业—结构体

    一.PTA实验作业 题目1:结构体数组按总分排序 1. 本题PTA提交列表 2. 设计思路 void calc //函数calc求出p指针所指的结构体数组中 n 名学生各自的总分 { 定义循环变量i: ...

  3. 利用python处理文档中各字段出现的次数并排序

    import string path = 'waldnn' with open(path,'r') as text: words = [raw_word.strip(string.punctuatio ...

  4. git基本语法

    基本用法(上)               一.实验说明 本节实验为 Git 入门第一个实验,可以帮助大家熟悉如何创建和使用 git 仓库. 二.git的初始化 在使用git进行代码管理之前,我们首先 ...

  5. 【iOS】swift 74个Swift标准库函数

    本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swift standard li ...

  6. Codeforces 193 D. Two Segments

    http://codeforces.com/contest/193/problem/D 题意: 给一个1~n的排列,在这个排列中选出两段区间,求使选出的元素排序后构成公差为1的等差数列的方案数. 换个 ...

  7. LeetCode & Q219-Contains Duplicate II

    Array Hash Table Description: Given an array of integers and an integer k, find out whether there ar ...

  8. Code::Blocks出现64-Bit mode not compled in解决方法

    原因是:Settings->compilter你选了Target x86 _64(64bit),选择Target x86 _32(32bit)即可 废了老半天劲才找到原因,希望能让朋友们少走弯路

  9. C# 使用 GDI+ 实现添加中心旋转(任意角度)的文字

    这篇文章是 GDI+ 总结系列的第三篇,如果对 GDI+ 的基础使用不熟悉的朋友可以先看第一篇文章<C# 使用 GDI+ 画图>. 需求 需求是要实现给图片添加任意角度旋转的文字,文字的旋 ...

  10. java获取本类路径

    (1).Test.class.getResource("") 得到的是当前类FileTest.class文件的URI目录.不包括自己! (2).Test.class.getReso ...