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
思路:因为这个字符串的长度是个斐波那契数列(7,10,17,27,......),后面的字符都来源于前面的字符,所以从后面开始求解
例如,求解第23个位置的字符,先找到23的位置,17<23<27,所以23是处在由10和17组成的字符串中,23-17=6,第23个字符等于长为10的字符串的第6个位置,也就是长为7的字符串的第6个位置
#include<cstdio>
#include<string.h>
#include<cmath>
using namespace std;
long long f[1000];
char s1[8] = "T.T^__^";
char s2[11] = "T.T^__^T.T";
int main()
{
memset(f, 0, sizeof(f));
f[1] = 7; f[2] = 10;
int Large = 0;
for (int i = 3; i <= 1000; i++)
{
long long sum = f[i - 1] + f[i - 2];
if (sum < 0)
{
break;
}
else
f[i] = sum;
}
long long N = 0;
while(scanf("%lld", &N) != EOF)
{
int i = 1;
while (N > 7)
{
int i = 1;
while (N > f[i])
i++;
N = N - f[i - 1];
}
printf("%c\n", s1[N - 1]);
}
return 0;
}