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. Redis——常见面试题

    一.memcached与redis的区别? 1.存储方式不同.memcached把数据全部存在内存之中,断电之后会挂掉,而redis虽然也用到了内存,但是会有部分数据存在硬盘中,保证数据持久性. 2. ...

  2. Vue中的v-cloak用法

    v-cloak 的作用和用法 用法: 这个指令保持在元素上直到关联实例结束编译.和 CSS 规则如 [v-cloak] { display: none } 一起用时,这个指令可以隐藏未编译的 Must ...

  3. 第七周PTA作业

    第一题: #include<stdio.h> int main() { ; ; ){ sum=sum+i; i++; } printf("sum = %d\n",sum ...

  4. Week1绪论--抽象数据类型

    一.作业题目 1.构造有理数T,元素e1,e2分别被赋以分子.分母值 2.销毁有理数T 3.用e(引用类型参数)返回有理数T的分子或分母,当入参i为1时返回分子, i为2是返回分母. 4.将有理数T的 ...

  5. python 归并排序

    def merge_sort(alist): if len(alist) <= 1: return alist # 二分分解 num = len(alist)/2 left = merge_so ...

  6. AWS EMR上搭建HBase环境

    0. 概述 AWS的EMR服务为客户提供的托管 Hadoop 框架可以让您轻松.快 速.经济高效地在多个动态可扩展的 Amazon EC2 实例之间分发和处理 大量数据.您还可以运行其他常用的分发框架 ...

  7. Environment.getExternalStorageDirectory()

    Environment.getExternalStorageDirectory()得到的是storage/emulated/0

  8. OO前三次作业总结

    一.第一次作业 1.程序设计分析 ![img](s1.ax1x.com/2018/04/02/CSgoSU.png) 图1 第一次作业类图 ![name](https://images2018.cnb ...

  9. JS中的 map, filter, some, every, forEach, for...in, for...of 用法总结

    1.map 有返回值,返回一个新的数组,每个元素为调用func的结果. let list = [1, 2, 3, 4, 5]; let other = list.map((d, i) => { ...

  10. php的开发的apache的配置及伪静态的应用

    1.Apache之所以能够解析php代码是游览器首先发送数据到模版页面,然后模版页提交数据到php页面,然后php代码经过Apache解析过后生成结果的,所以是 在Apache的配置文件中是可以看到开 ...