B. 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 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
Sample Input | Output for Sample Input |
4 10 20 30 40 | Case 1: 10010 Case 2: 101010 Case 3: 1010001 Case 4: 10001001 |
题意:给你一个01串满足:没有前置0,任意两个1不能相邻,问你第n个这样的数是多少
题解:这是一种dp思想, 假设最高位是1,那么次高位必须是0,那么有 f[i]=f[i-1]+f[i-2];
得出来是一个fibonacci数列 打表暴力可求解
///1085422276 #include<bits/stdc++.h> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,127,sizeof(a)) #define TS printf("111111\n") #define FOR(i,a,b) for( int i=a;i<=b;i++) #define FORJ(i,a,b) for(int i=a;i>=b;i--) #define READ(a,b,c) scanf("%d%d%d",&a,&b,&c) #define inf 100000 inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //**************************************** #define maxn 1111 ll f[maxn],sum[maxn]; void init() { f[0]=1; f[1]=1;;sum[0]=0;sum[1]=1; for(int i=2;i<=50;i++) f[i]=f[i-1]+f[i-2],sum[i]=sum[i-1]+f[i-1]; } int main() { init(); int T=read(); int oo=1; while(T--) { ll n=read();printf ("Case %d: ",oo++); bool flag=0; for(int i=50;i>=0;i--) { if(n>sum[i]) { n-=sum[i]+1; cout<<1; flag=1; } else if(flag){ cout<<0; } }cout<<endl; } return 0; }
///1085422276 #include<bits/stdc++.h> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,127,sizeof(a)) #define TS printf("111111\n") #define FOR(i,a,b) for( int i=a;i<=b;i++) #define FORJ(i,a,b) for(int i=a;i>=b;i--) #define READ(a,b,c) scanf("%d%d%d",&a,&b,&c) #define inf 1000000001 inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //**************************************** #define maxn 50 ll dp[maxn][2]; ll num[maxn]; int dfs(int i,int p) { if(dp[i][p])return dp[i][p]; if(i<1)return 1; int ret=0; if(p) ret=dfs(i-1,0); else ret=dfs(i-1,0)+dfs(i-1,1); dp[i][p]=ret; return ret; } void get(int n) { bool flag=0; for(int i=45;i>1;i--) { if(n>=num[i]) { n-=num[i]; cout<<1; flag=1; } else if(flag)cout<<0; } } int main() { mem(dp); for(int i=1;i<=45;i++)num[i]=dfs(i-1,1); cout<<num[2]<<endl; // cout<<dp[1][0]<<endl; int T=read(); int oo=1; while(T--) { int n=read(); printf("Case %d: ",oo++); get(n);cout<<endl; } return 0; }