[Codeforces 946F]Fibonacci String Subsequences
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}\) 出现的次数。
来考虑转移,一共有三部分:
- \(l\sim r\) 完全在 \(F(i-1)\) 中。此时若 \(r=n\) ,那么可以在 \(F(i-2)\) 中乱选,则有 \(2^{len(F(i-2))}\) 种;若 \(r\neq n\) ,因为不能在后面乱选,所以贡献只有 \(1\) 倍。
- \(l\sim r\) 完全在 \(F(i-2)\) 中。这种讨论和 1. 中相同。
- 最后分为在不同的两段中,设 \(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的更多相关文章
- codefroces 946F Fibonacci String Subsequences
Description定义$F(x)$为$F(x−1)$与$F(x−2)$的连接(其中$F(0)="0"$,$F(1)="1"$)给出一个长度为$n$的$01$ ...
- HDU 1708 简单dp问题 Fibonacci String
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDOJ(HDU) 1708 Fibonacci String
Problem Description After little Jim learned Fibonacci Number in the class , he was very interest in ...
- hdu 1708 Fibonacci String
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Fibonacci String(hdu 1708)
Fibonacci String Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
- Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力
Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consist ...
- Codeforces 797C - Minimal string
C. Minimal string 题目链接:http://codeforces.com/problemset/problem/797/C time limit per test 1 second m ...
- codeforces 825F F. String Compression dp+kmp找字符串的最小循环节
/** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...
随机推荐
- Leetcode 24——Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2-& ...
- 201621123035 《Java程序设计》第1周学习总结
1.本周学习总结 本周学习内容:Java平台概论.认识JDK规范与操作.了解JVM.JRE与JDK.撰写Java原始码.path是什么 关键词:JVM.JRE.JDK 联系:JVM是Java虚拟机的缩 ...
- 201621123031 《Java程序设计》第5周学习总结
作业05-继承.多态.抽象类与接口 1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键字:接口.继承.多态 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需 ...
- Java 中 compareTo方法问题
compareTo方法原理:先读取出字符串的第一个“字母”进行比较,比较的方法是ascii码表的值(字符所对应的十进制值),如果前面的大那么返回1,后面的大返回-1:此位置相同,继续比较下一位,直到最 ...
- XP实验报告
实验名称:敏捷开发与XP实践 实验人员:20162309邢天岳(结对搭档20162313苑洪铭) 实验日期:2017.5.5 实验内容:1.在IDEA中使用工具(Code->Reformate ...
- 过滤器Filter与监听器Listener
过滤器Filter 过滤器也是一种servlet 它也可以对用户的请求进行处理 , 但是他所做的处理,只是一些轻量级的处理.Fileter就好像jsp页面与servlet之间的一道关卡,如果这个 ...
- JAVA_SE基础——67.System类
System类对大家都不陌生吧! 以前经常需要打印结果时使用的都是"System.out.println()"语句,这句代码中就使用了System类.System类定义了一些与系统 ...
- python端口扫描用多线程+线程安全的队列+Thread类实现
用线程安全的队列Queue实现扫描端口数据存储 用多线程扫描端口 用Thread类实现程序组织 #coding:utf-8 import sys import socket import sys im ...
- Java基础类库简介
Java基础类库简介 一.常用的基础类库:11个jar(Java Archive,Java归档)包 作为java语言使用者,我们可以感受到java语言带来的优势(平台无关.面向对象.多线程.高效易扩展 ...
- c++中模板是什么?为什么要定义模板?
一.c++中模板是什么? 首先: int Max(int x, int y) { return x > y ? x : y; } float Max(float a,float b) { ret ...