Light oj 1105 - Fi Binary Number(计数)

本文介绍了一种特殊的二进制数——Fi-Binary数,并提供了一个算法来计算第n个Fi-Binary数。Fi-Binary数的特点是不含前导0且不包含连续的1。文章通过示例解释了输入输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1105 - Fi Binary Number
Time Limit: 2 second(s)Memory Limit: 32 MB

A Fi-binary number is a number that contains only 0 and 1. It does not contain any leading 0. And also it does not contain 2 consecutive 1. The first few such number are 1, 10, 100, 101, 1000, 1001, 1010, 10000, 10001, 10010, 10100, 10101 and so on. You are given n. You have to calculate the nth Fi-Binary number.

Input

Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer n (1 ≤ n ≤ 109).

Output

For each case, print the case number and the nth Fi-Binary number

Sample Input

Output for Sample Input

4

10

20

30

40

Case 1: 10010

Case 2: 101010

Case 3: 1010001

Case 4: 10001001

 


PROBLEM SETTER: ABDULLAH AL MAHMUD
SPECIAL THANKS: JANE ALAM JAN (SOLUTION, DATASET)


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define bug printf("hihi\n")

#define eps 1e-12

typedef long long ll;
using namespace std;

#define mod 1000000007
#define N 100

ll f[2][N];

void inint()
{
    int i,j;
    int one,zero;
    zero=1;
    one=0;
    f[0][0]=f[1][0]=0;
    for(i=1;i<N;i++)
       {
           f[0][i]=f[0][i-1]+one+zero;
           int te=one;
           one=zero;
           zero=zero+te;
       }
    zero=0;
    one=1;
    for(i=1;i<N;i++)
    {
        f[1][i]=f[1][i-1]+one+zero;
        int te=one;
        one=zero;
        zero=zero+te;
    }
}

void dfs(int st,int n)
{

    if(n==0) return ;

    if(st==1)
    {
        printf("1");
        dfs(0,n-1);
        return ;
    }

    printf("0");
    n--;
    if(n==0) return ;

    int i;
    for(i=1;i<N;i++)
        if(f[0][i]+f[1][i]>=n) break;

    if(n<=f[0][i]+f[1][i-1])
    {
        dfs(0,n-f[1][i-1]);
        return ;
    }
    dfs(1,n-f[0][i]);
}

int main()
{
    int i,j,t,ca=0,n;
    inint();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("Case %d: ",++ca);
        dfs(1,n);
        printf("\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值