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
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");
}
}