Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
``Pinary" number is a positive number using only two digits ``0" and ``1" with usual rule that it must not begin with a 0, and the additional rule that two successive digits must not be both ``1". This means that the factor ``11" is forbidden in the string. So the allowed Pinary writings are 1, 10, 100, 101, 1000, 1001,..., 100010101010100010001 . For example, ``100101000" is a Pinary number, but neither ``0010101" nor ``10110001" are Pinary numbers.
Each Pinary number represents a positive integer in the order they appear (using length order, lexicographic order), that is, 1 is mapped to 1, 10 is mapped to 2. And 100, 101 and 1000 are mapped to 3, 4 and 5, respectively. You are to write a program to generate Pinary number representations for integers given.
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing a postive integer 2 < K < 90, 000, 000 .
Output
Your program is to write to standard output. Print exactly one line for each test case. For each test case, print the Pinary number representation for input integer. The following shows sample input and output for three test cases.
Sample Input
3 7 2000 22
Sample Output
1010 1001000001001000 1000001
Solution
题意是给出一个序列,求序列的第n项。这个序列是由0和1组成的,并且其中不包括11,也就是连续两个的1。
首先就是找找看规律:
元素长度 以0结尾 以1结尾 sum
1 0 1 1
2 1 0 1
3 1 1 2
4 2 1 3
5 3 2 5
6 5 3 8
i sum[i-1] sum[i-2] sum[i]
可以从上面看出,每一个长度的元素数是呈现斐波那契数列的关系的,然后把每一个元素可以看作是一个数,这个数是每逢一个元素长度就进位的,也就是一个特殊的进制数。
由此可以写出以下代码
#include <cstdio>
int sum[45];//数据上限
int main()
{
int i, t, d;
sum[0] = sum[1] = 1;
for (i = 2; i < 45; ++i) sum[i] = sum[i-1] + sum[i-2];
scanf("%d", &t);
while (t --)
{
scanf("%d", &d);
bool out = false;
i = 45;
while (--i)
{
if (d >= sum[i])
{
out = true;
d -= sum[i];
printf("1");
}
else if (out) printf("0");
}
printf("\n");
}
return 0;
}