Off more cattle tenth school field B Coffee Chicken recursive

Meaning of the questions:

To give you a "Fibonacci" series string, item n by splicing the n-1 n 2-item and the item is made by the 10-bit output and an after.

answer:

Recursive solution can be.

#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("");
    }
}

 

Guess you like

Origin www.cnblogs.com/isakovsky/p/11372456.html