递归模拟

递归模拟

题目描述
Dr. JYY has just created the Coffee Chicken strings, denoted as S(n). They are quite similar to the Fibonacci soup --- today's soup is made by mingling yesterday's soup and the day before yesterday's soup:S(1)="COFFEE"; S(2)="CHICKEN";

  • S(n) = S(n-2) :: S(n-1), where :: denotes string concatenation. The length of S(n) quickly gets out of control as n increases, and there would be no enough memory in JYY's game console to store the entire string. He wants you to find 10 contiguous characters in S(n), starting from the kth character.

输入描述: The first line of input is a single integer T (1≤T≤1000), denoting the number of test cases. Each of the following T lines is a test case, which contains two integers n, k (1≤n≤500,1≤k≤min{∣S(n)∣,10^12 }). |S| denotes the length of string S. 输出描述: For each test case, print the answer in one line. If there are no enough characters from the kth character, output all characters until the end of the string. 示例1 输入 2 3 4 2 7 输出 FEECHICKEN N

题解 题目与斐波那契数列有关但是因为Sn的增长到后面过于爆炸,因此不能直接求Sn然后求十个字符 需要转换一下,请观察下面的规律 A 1 B 1 AB 2 BA 3 ABBAB 5 BABABBAB 8 ABBABBABABBAB 13 BABABBABABBABBABABBAB 21 ABBABBABABBABBABABBABABBABBABABBAB 34 BABABBABABBABBABABBABABBABBABABBABBABABBABABBABBABABBAB 45 可以知道当前指向的位置大于a[n-2]就可以求a[n-1]的第k-a[n-2]个数(上面的数一数),如果小于等于a[n-2]就等于求a[n-2]的第几个数

代码示例


#include<iostream>
using namespace std;
using ll = long long;
ll a[700];
//mello函数记录Sn的字符串长度的值
void mello() {
    a[1] = 6;//6是COFFEE的长度
    a[2] = 7;//7是CHICKEN的长度
    for (int i = 3; i <= 60; i++) {
        a[i] = a[i - 1] + a[i - 2];//循环记录Sn的长度
    }
}

int main() {
    mello();
    int t;
    cin >> t;
    while (t--) {
        ll n, k;
        cin >> n >> k;
        //对于大于60的n,我们只需要取小的值替代即可,奇数用59,偶数用58
        //为什么用60?因为k最多到1e12,当Sn大于58时长度就超过了1e12
        if (n >= 60) {
            if (n % 2) n = 59;
            else n = 58;
        }
        for (ll i = k; i <= a[n] && i < k + 10; i++) {
            ll w = i, v = n;//w追踪当前位置,v追踪当前序列项
            while (v != 1 && v != 2) {//不断递归减小,直到v选择COFFEE或CHICKEN
                if (w > a[v - 2]) {
                    w = w - a[v - 2];
                    v -= 1;
                }
                else {
                    v -= 2;
                } 
            }
            if (v == 1) printf("%c", "COFFEE"[w - 1]);
            else if (v == 2) printf("%c", "CHICKEN"[w - 1]);
        }
        printf("\n");
    }
    return 0;
}

更新于 2024/05/01

评论

评论 0

暂无评论
全部文章
music

521755ldsdl.jpg

关于

QAQ

C++离散化

为什么要离散化处理? 数据过于多时难以表示数组下标 离散化是什么? 可以类比哈希表 离散化就像哈

教程超详细xcpc滚榜教程

首先需要导出比赛的表格 1.处理表格 最终效果如下图 自行添加的列有id,judged,solv

C++快速幂

快速幂 用于快速求a^b的算法 思想:拆分 例:求2^13 等价于求 2^1 * 2^4 * 2

C++高精度运算(加减乘除)

在程序中高精度加法和高精度减法的运算本质是模拟我们在小学就学过的运算规则,也是我们平时用的运算规

C++素数筛法(埃氏筛法、线性筛法)

素数筛法:用于筛选出素数 核心思想:用一个数筛选其余的数 普通筛法: 时间复杂度为O(nlogn

C++二叉树的建立与遍历

二叉树是一种非线性数据结构. 二叉树 由节点构成,每个节点包含节点值和指向左节点和右节点的指针.

C++链表

链表是一种逻辑结构上的线性表,物理结构上的非线性表 链表由许多节点组成,每个节点有对应的值和指向

C++C++实现顺序表

顺序表,一种线性表,因此顺序表是线性的. 因为顺序表在物理结构与逻辑结构都是连续的.意味着在内存

PythonPython基础

标识符 Python中标识符由字母,数字,下划线组成. 字母区分大小写,不能以数字开头,以下划线

算法并查集

并查集 :并为合并(合并两个节点),查为查询(查询根节点),集是一个集合. 联通块问题 //各个

K 随时打开ESC 关闭

阅读设置

设置仅保存在当前设备。

主题

背景

文章布局