2019牛客暑期多校训练营(第十场 B——Coffee Chicken (思维题))

2019牛客暑期多校训练营(第十场 B——Coffee Chicken (思维题))

链接:https://ac.nowcoder.com/acm/contest/890/B
来源:牛客网

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) = \texttt{“COFFEE”}S(1)=“COFFEE”;
  • S(2) = \texttt{“CHICKEN”}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 \leq T \leq 1000)(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 \leq n \leq 500, 1 \leq k \leq \min{|S(n)|, 10^{12}})(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

题目大意:

S(1)=“COFFEE”;
S(2)=“CHICKEN”;
s3 = s1::s2 (s1和s2拼接起来)
………
sn = sn-2 :: sn-1

问:给你个n 和 k让你输出 sn的从第k个开始的前10个,如果不够10个就有多少输出多少。

解题思路:

首先看这个k是<1e12的 所以暴力肯定是不行的

我们打表测试,发现n = 56时 sn的长度已经>=1e12。所以n>58的时候我们考虑到让他退化成57.

然后我们用dp[i] = si的长度,提前处理出dp数组。
在这里插入图片描述
假设要找Sn中的第k个字符
如果k>dp[n-2]的话 那第k个肯定在右半边,然后更新 k -= dp[n-2] n = n-1
k<=dp[n-2] 的话,k就在左半边,更新 n = n-2

一直往下找,找到 n=1||n=2为止。

AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[500];
void init(){
    dp[1] = 6;
    dp[2] = 7;
    for(int i=3;i<=58;i++){
        dp[i] = dp[i-1]+dp[i-2];
    }
}
ll K;
int N;
char s1[10] ="COFFEE";
char s2[10] ="CHICKEN";
int main(){
    int t;
    scanf("%d",&t);
    init();
    while(t--){
        scanf("%d%lld",&N,&K);
        if(N>=58){
            N = 57;
        }
        ll left = K;
        ll right;
        right = min(dp[N],K+9);
        int n;
        for(ll i=left;i<=right;i++){
            n = N;
            ll k = i;
            while(1){  
                if(n==1){
                    cout<<s1[k-1];
                    break;
                }else if(n==2){
                    cout<<s2[k-1];
                    break;
                }
                if(k>dp[n-2]){//右边
                    k -= dp[n-2];
                    n = n-1;
                }else if(k<=dp[n-2]){//左边
                    n = n-2;
                }
            }
        }
        cout<<endl;
    }  
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值