牛客多校第十场 B Coffee Chicken 递归

题意:

给你一个“斐波那契”字符串数列,第n项由第n-1项和第n-2项拼接而成,输出某项的某位及其后10位。

题解:

递归求解即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef long long ll;
typedef pair<int, LL>P;
const int M = 1e2 + 5;
const int N = 1e5 + 5;
const LL mod = 1e9 + 7;
const LL lINF = 0x3f3f3f3f3f3f3f3f;
#define ls (rt<<1)
#define rs (rt<<1|1)
LL cnt[M],n,k;
string s1 = "COFFEE";
string s2 = "CHICKEN";
char ss[N];
int tot;
void solve(LL n,LL k)
{
    if (n > 2 && k > cnt[n - 2])
        solve(n - 1, k - cnt[n - 2]);
    else if (n > 2)
    {
        solve(n - 2, k);
        if (k + 9 - cnt[n - 2] >= 0)
            solve(n - 1, 1);
    }
    if (n == 1)
    {
        if (tot > 20)
            return;
        for (int i = k - 1; i < s1.size(); i++)
        {
            ss[tot++] = s1[i];
        }
    }
    else if (n == 2)
    {
        if (tot > 20)
            return;
        for (int i = k - 1; i < s2.size(); i++)
        {
            ss[tot++] = s2[i];
        }
    }
}
int main()
{
    cnt[1] = 6;
    cnt[2] = 7;
    for (int i = 3; i < 57; i++)
    {
        cnt[i] = cnt[i - 1] + cnt[i - 2];
        //cout << cnt[i] << endl;
    }
    int _;
    scanf("%d", &_);
    while(_--)
    {
        scanf("%lld%lld", &n, &k);
        if (n > 56)
            n = 56;
        tot = 0;
        solve(n, k);
        for (int i = 0; i < min(tot, 10); i++)
        {
            printf("%c", ss[i]);
        }
        puts("");
    }
}

 

posted @ 2019-08-18 15:02  Isakovsky  阅读(176)  评论(0编辑  收藏  举报