F - Fi Binary Number
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Description
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
4
10
20
30
40
Sample Output
Case 1: 10010
Case 2: 101010
Case 3: 1010001
Case 4: 10001001
题意:
Fi-binary number 是只含0和1且没有前导零的数,并且2个1不能连续出现,问第n个Fi-binary number 是什么
题解:其实如果列出前几个数的话可以发现这其实就是个斐波那契数列,设数的长度为len,那么该长度下Fi-binary number 的个数就是f[len],
2个1不能连续出现那是因为这样的话就可以合成下一个斐波那契数了,f[n]=f[n-1]+f[n-2]
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100;
const int tot = 1e9;
int a[maxn];
int main(){
int n=2,T,x;
a[1]=a[2]=1;
while(a[n]<=tot){
n++;
a[n]=a[n-2]+a[n-1];
}
// freopen("in.txt","r",stdin);
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
printf("Case %d: ",cas);
scanf("%d",&x);
int pos=n,flag=1;
while(pos>1){
if(flag){
if(a[pos]<=x){
x-=a[pos];
flag=0;
printf("1");
}
}
else{
if(a[pos]<=x) {
x-=a[pos];
printf("1");
}
else printf("0");
}
pos--;
}
printf("\n");
}
return 0;
}