2019牛客暑期多校训练营(第十场) Coffee Chicken
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
64bit IO Format: %lld
题目描述
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(1) = \texttt{"COFFEE"}S(1)="COFFEE";
- S(2)="CHICKEN"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.
- S(1)="COFFEE"S(1) = \texttt{"COFFEE"}S(1)="COFFEE";
- S(2)="CHICKEN"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≤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.
输入
2
3 4
2 7
输出
FEECHICKEN
N
题意:s[1]="COFFEE",s[2]="CHICKEN",s[n]=s[n-2]+s[n-1],有T组数据,每组数据求出s[n]从k开始以后的连续十个字符。
题解:递归。打表长度到f[57],因为k最大为1e12,我们发现f[56],f[57]都已经大于1e12,所以当n>57时,用到的还是f[56]或f[57]。
代码:
#include<bits/stdc++.h>
using namespace std;
char s1[10]=" COFFEE",s2[10]=" CHICKEN";
long long f[60]={0,6,7};
void dfs(int n,long long k);
int main()
{
int i,T,n;
long long k;
for(i=3;i<=57;i++) f[i]=f[i-1]+f[i-2];
scanf("%d",&T);
while(T--)
{
scanf("%d%lld",&n,&k);
if(n>57)
{
if(n&1) for(i=0;i<10;i++) dfs(57,k+i);
else for(i=0;i<10;i++) dfs(56,k+i);
printf("\n");
}
else
{
for(i=0;i<10;i++) dfs(n,k+i);
printf("\n");
}
}
system("pause");
return 0;
}
void dfs(int n,long long k)
{
if(k>f[n]) return ;
if(n==1)
{
printf("%c",s1[k]);
return ;
}
else if(n==2)
{
printf("%c",s2[k]);
return ;
}
else
{
if(k>f[n-2]) dfs(n-1,k-f[n-2]);
else dfs(n-2,k);
}
}