UVa948 - Fibonaccimal Base

Problem C - Fibonaccimal Base

The well known Fibonacci sequence is obtained by starting with 0 and 1 and then adding the two last numbers to get the next one. For example the third number in the sequence is 1 (1=1+0), the forth is 2 (2=1+1), the fifth is 3 (3=2+1) and so on.
 

i0123456789
Fib(i)0112358132134
Figure 1 - The first numbers in the Fibonacci sequence

The sequence appears on many things in our life, in nature, and has a great significance. Among other things, do you know that all positive integer numbers can be represented as a sum of numbers in the Fibonacci sequence? More than that, all positive integers can be represented as a sum of a set of Fibonacci numbers, that is, numbers from the sequence, without repetition. For example: 13 can be the sum of the sets{13}{5,8} or {2,3,8} and 17 is represented by {1,3,13} or {1,3,5,8}. Since all numbers have this property (do you want to try to prove this for yourself?) this set could be a nice way to use as a "base" to represent the number. But, as we have seen, some numbers have more than one set whose sum is the number. How can we solve that? Simple! If we add the constraint that the sets cannot have two consecutive Fibonacci numbers, than we have a unique representation for each number! This restriction is because the sum of any two consecutive Fibonacci numbers is just the following Fibonacci number.

Now that we know all this we can prepare a nice way to represent any positive integer. We will use a binary sequence (just zeros and ones) to do that. For example, 17 = 1 + 3 + 13 (remember that no two consecutive Fibonacci numbers can be used). Let's write a zero for each Fibonacci number that is not used and one for each one that is used, starting at the right. Then, 17 = 100101. See figure 2 for a detailed explanation. In this representation we should not have zeros at the left, this is, we should only write starting with the first one. In order for you to understand better, note that in this scheme, not using two consecutive Fibonacci numbers means that the binary sequence will not have two consecutive ones. When we use this representation for a number we say that we are using the Fibonaccimal base, and we write it like 17 = 100101 (fib)
 

17 =100101
13+3+1 =1385321
Figure 2 - Explaining the representation of 17 in Fibonaccimal base

The Problem

Given a set of numbers in decimal base, your task is to write them in the Fibonaccimal base.

Input

The first line of input contains a single number N, representing the quantity of numbers that follow ( 1 ≤ N ≤ 500).

Than follow exactly N lines, each one containing a single positive integer smaller than 100 000 000. These numbers can come in any order.

Output

You should output a single line for each of the N integers in the input, with the format "DEC_BASE = FIB_BASE (fib)"DEC_BASE is the original number in decimal base and FIB_BASE is its representation in Fibonaccimal base. See the sample output for an example.

Example Input

10
1
2
3
4
5
6
7
8
9
10

Example Output

1 = 1 (fib)
2 = 10 (fib)
3 = 100 (fib)
4 = 101 (fib)
5 = 1000 (fib)
6 = 1001 (fib)
7 = 1010 (fib)
8 = 10000 (fib)
9 = 10001 (fib)
10 = 10010 (fib)
 
 
贪心
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int n;
vector<int> fibo;
void init(){
    fibo.push_back(1);
    fibo.push_back(1);
    for(int i = 2; ; i++){
        long long t = (long long) fibo[i-1]+fibo[i-2];
        if(t>1e8) break;
        fibo.push_back(t);
    }
}
int main(){
    init();
    int ncase;
    cin >> ncase;
    while(ncase--){
        cin >> n;
        int k = n;
        vector<int> digit;
        int sta = fibo.size()-1;
        while(n<fibo[sta]) sta--;
        for(int i = sta; i >= 0; ){
            if(n >=fibo[sta]){
                n -= fibo[sta];
                digit.push_back(1);
                sta--;
            }
            while(n<fibo[sta]&&sta>0) {sta--;digit.push_back(0);}
            if(sta<=0) break;
        }
        cout<<k<<" "<<"="<<" ";
        for(int i = 0; i < digit.size(); i++)
            cout<<digit[i];
        cout<<" (fib)"<<endl;


    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值