时间限制: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) = \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)∣,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

题意:

s[1]="COFFEE",s[2]="CHICKEN",s[n]由s[n-2]和s[n-1]连接而成,注意s[n-2]在s[n-1]的前面
问长度为s[n]的字符串,从第k个字符开始的连续10个字符是多少(如果还未有10个字符但到达字符串末尾时要停止)

思路:

s[n]=s[n-2]+s[n-1],类似斐波那契数列,现在要看k是在s[n-2]上还是s[n-1]上,所以想到用k在s[n-2]和s[n-1]上搜索,如果k小于等于s[n-2]就向s[n-2]搜索,否则
就向s[n-1]搜索,但注意s[n-1]只是长度而不是位置,而k是原s[n]中的总位置,所以向子串搜索时
要减去多余的位置才能在子串的长度中搜索所以k-s[n-2]才能得到在s[n-1]中的长度,也就是它的相对位置

注意:

有一个坑点,inf要设为1e12+10,因为k最大是1e12,又要输出10位,所以极限要比1e12大10

 #include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll inf=1e12+; ///这是一个坑点,inf要设为1e12+10,因为k最大是1e12,又要输出10位,所以极限要比1e12大10
const int amn=5e2+;
ll s[amn];
char ans1[]="COFFEE",ans2[]="CHICKEN";
char find(ll n,ll k){
if(n==)return ans1[k-]; ///这里k-1是因为ans数组从0开始,而k从1开始
if(n==)return ans2[k-];
if(k>s[n-])return find(n-,k-s[n-]); ///如果k要往右边搜索,则k要减去前面的s[n-2]才能得到s[n-1]的相对位置,注意这里要加返回才能从最底层一直返回到最顶层,不然就只是在最底层返回到上一层
else return find(n-,k); ///如果向左边搜索就直接下传
}
int main(){
int T;
ll n,k;
s[]=,s[]=;
for(int i=;i<=;i++)s[i]=s[i-]+s[i-]<=inf?s[i-]+s[i-]:inf; ///预处理s数组,是每种字符串的长度
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&n,&k);
for(ll i=k;i<k+&&i<=s[n];i++)printf("%c",find(n,i));printf("\n"); ///一个个输出,到10个或超过了字符串总长度就停止
}
}
/**
s[1]="COFFEE",s[2]="CHICKEN",s[n]由s[n-2]和s[n-1]连接而成,注意s[n-2]在s[n-1]的前面
问长度为s[n]的字符串,从第k个字符开始的连续10个字符是多少(如果还未有10个字符但到达字符串末尾时要停止)
s[n]=s[n-2]+s[n-1],类似斐波那契数列,现在要看k是在s[n-2]上还是s[n-1]上,所以想到用k在s[n-2]和s[n-1]上搜索,如果k小于等于s[n-2]就向s[n-2]搜索,否则
就向s[n-1]搜索,但注意s[n-1]只是长度而不是位置,而k是原s[n]中的总位置,所以向子串搜索时
要减去多余的位置才能在子串的长度中搜索所以k-s[n-2]才能得到在s[n-1]中的长度,也就是它的相对位置
注意有一个坑点,inf要设为1e12+10,因为k最大是1e12,又要输出10位,所以极限要比1e12大10
**/

最新文章

  1. MVC中的ActionLink生成的属性名称 中划线的解决办法
  2. Oracle【IT实验室】数据库备份与恢复之五:Flashback
  3. python-可变迭代对象在for循环中的风险Risk in FOR loop while looping mutable iterable object
  4. Device Tree(三):代码分析
  5. FTP方式获取文件步骤
  6. 基于jQuery的AJAX和JSON的实例
  7. 异常捕捉 ( try catch finally ) 你真的掌握了吗?
  8. ios如何实现推送通知
  9. BarChart控件的使用
  10. 远程方法调用(RMI)原理与示例 (转)
  11. 在foreach的判断条件里执行方法会有效率问题吗?
  12. BZOJ4424: Cf19E Fairy
  13. Asp.net中时间格式化的几种方法
  14. Git入门到高级系列1-git安装与基础命令
  15. FZU.Software Engineering1816 &#183;The Second Assignment of the Team
  16. 微软VBS生成Excel内容和图表示例
  17. Ubuntu系统中各种文件颜色的含义
  18. centos 虚拟环境 安装mezzanine cms
  19. js获取height和width总结
  20. php5.5过渡--mysql连接

热门文章

  1. Android中的路径记录
  2. “一亿”的教训:一次Google信箱诈骗是如何得手的?
  3. nginx图片过滤处理模块http_image_filter_module安装配置
  4. Javascript中的局部变量、全局变量的详解与var、let的使用区别
  5. Fetch API与POST请求那些事
  6. 高性能内存队列Disruptor--原理分析
  7. Postgresql存放数组形式的数据
  8. LaTex公式符号
  9. 趣谈编程史第3期-大器晚成的新晋流量Python发展史
  10. Go语言转义字符