Light oj 1105 - Fi Binary Number(计数)

来源:互联网 发布:单片机程序流程图画法 编辑:程序博客网 时间:2024/05/18 18:45

1105 - Fi Binary Number
PDF (English)StatisticsForum
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-12typedef long long ll;using namespace std;#define mod 1000000007#define N 100ll 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;}


0 0
原创粉丝点击