链接:https://ac.nowcoder.com/acm/contest/890/B
来源:牛客网
Coffee Chicken
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 524288K,其他语言1048576K
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.
输入描述:
The first line of input is a single integer T (1≤T≤1000)(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≤n≤500,1≤k≤min{∣S(n)∣,1012})(1 \leq n \leq 500, 1 \leq k \leq \min\{|S(n)|, 10^{12}\})(1≤n≤500,1≤k≤min{∣S(n)∣,1012}). |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
题解:因为只输出10个,所以我们暴力找每一个,对于S[n] ,就判断一下是在S[n - 1] 中还是S[n - 2]中,一直递归到1 2 。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[510];
int n;
ll k;
string s[3];
int main() {
s[1] = "COFFEE";
s[2] = "CHICKEN";
dp[1] = 6;
dp[2] = 7;
for(int i = 3; i <= 500; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
if(dp[i] > 1e12) {
// printf("%d\n", i);
while(i <= 500)
dp[i++] = 1e12 + 10;
break;
}
}
int T;
int m; // 10
ll cnt;
int cntn;
int p;
scanf("%d", &T);
while(T--) {
scanf("%d %lld", &n, &k);
m = 1;
cnt = k;
cntn = n;
p = (int)min(1LL * 10, dp[cntn]);
while(1) {
n = cntn;
k = cnt + m - 1;
if(k > dp[n] || m > 10) break;
// cout << n << " " << k << endl;
while(n > 2) {
if(dp[n - 2] >= k) {
n = n - 2;
} else {
k -= dp[n - 2];
n = n - 1;
}
}
// cout << n << " " << k - 1 << endl;
cout << s[n][k - 1];
m++;
}
cout << endl;
}
return 0;
}