UVA 题目11089 Fi-binary Number(找规律)

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 n-th
Fi-Binary number.
Input
The first line of the input contains one integer T the number of test cases. Each test case contains one
integer n.
Output
For each test case output one line containing the n-th Fi-Binary number.
Constraints
• 1 ≤ N ≤ 109
Sample Input
4
10
20
30
40
Sample Output
10010
101010
1010001

10001001

题目大意:是Fi-binary Number的定义时由0和1构成,且没有连续的两个一,从第一个往后一次是 1, 10, 100, 101, 1000, 1001,
1010, 10000, 10001, 10010, 10100, 10101 。。。。。,现在给你一个n问第n项是什么

窝找的规律麻烦了一点。。。看别人找的挺短的,,莫名uva过了,zzulioj过不了。。

我的规律是;长度相等的个数成斐波那契数,长度1的有1个,长度2的有1,长度3的有两个,长度4的有3个。。

例如第20项为101010,先求出n属于第几个长度,答案是6,101010=100000+1010,,1010又等于1000+10,10是2的幂次方了(第某长度第一项。。)就结束了。。就是这个从长的往短的找。。

ac代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#define LL long long
#define INF 0x3f3f3f3f3f3f
using namespace std;
LL fac[55],sum[55];
void init()
{
    int i=1;
    fac[1]=1;
    sum[1]=1;
    sum[2]=2;
    fac[2]=1;
    for(i=3;i<=45;i++)
    {
        fac[i]=fac[i-1]+fac[i-2];
        sum[i]=sum[i-1]+fac[i];
    }
}
int bseach(LL val)
{
    int l=1,r=45;
    int ans=45;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(sum[mid]>=val)
        {
            ans=mid;
            r=mid-1;
        }
        else
            l=mid+1;
    }
    return ans;
}
LL bit[60];
LL Pow(LL a,int n)
{
    if(n==0)
        return 1;
    LL ans=1;
    int i;
    for(i=1;i<=n;i++)
        ans*=a;
    return ans;
}
int main()
{
    init();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        LL n;
        int i;
        scanf("%lld",&n);
        int c=bseach(n);
        //printf("%d\n",c);
        LL ans=0;
        ans+=Pow(2,c-1);
        while(n-sum[c-1]!=1)
        {
            n=n-sum[c-1]-1;
            //printf("%lld\n",n);
            c=bseach(n);
            ans+=Pow(2,c-1);
        }
        int k=0;
        while(ans)
        {
            LL temp=ans%2;
            bit[k++]=temp;
            ans/=2;
        }
        for(i=k-1;i>=0;i--)
            printf("%lld",bit[i]);
        printf("\n");
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值