Big String
Time Limit: 2 Seconds Memory Limit: 65536 KB
We will construct an infinitely long string from two short strings: A = "^__^" (four characters), and B = "T.T" (three characters). Repeat the following steps:
- Concatenate A after B to obtain a new string C. For example, if A = "^__^" and B = "T.T", then C = BA = "T.T^__^".
- Let A = B, B = C -- as the example above A = "T.T", B = "T.T^__^".
Your task is to find out the n-th character of this infinite string.
Input
The input contains multiple test cases, each contains only one integer N (1 <= N <= 2^63 - 1). Proceed to the end of file.
Output
For each test case, print one character on each line, which is the N-th (index begins with 1) character of this infinite string.
Sample Input
1
2
4
8
Sample Output
T
.
^
T
首先,我们可以通过递推式len[n]=len[n-1]+len[n-2]得出每个长度
又,最终的字符串是从前往后递推出来的,所以再求解时,我们可以把这个过程逆过去,直到字符串的长度不超过7的时候输出
感受一个例子:
T.T^__^ -> T.T^__^T.T -> T.T^__^T.TT.T^__^
我们可以发现标红的点都是一个得来的,就可以得到它在前一个字符串的位置:
pos=lower_bound(a,a+89,n)-a;
n-=a[x-1];
^__^
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
long long n;
string c="T.T^__^";
long long a[105];
a[0]=4;a[1]=3;
for(int i=2;i<=88;++i){
a[i]=a[i-1]+a[i-2];//printf("%lld\n",a[i]);
}
while(~scanf("%lld",&n)){
while(n>7){
int x=lower_bound(a,a+89,n)-a;
n-=a[x-1];
} //一定相同前缀故而可以直接减!
printf("%c\n",c[n-1]);
}
return 0;
}