Sicily 1327 Pinary

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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章