uva1350 Pinary(递推)

题意:求第k大的无前导0和连续1的二进制数
解法:递推+二分查找
1.得到st[i]:第i位是1的前i位数列个数,sum[i]:前i为数列个数和
st[i]=st[i-2]+st[i-3]+…+st[0]
该递推式等价于st[i]=st[i-1]+st[i-2]
2.二分查找sum序列。并减去由于l位为1,l-1位不能为1的中间略去的数(sum[l-1]+1)

# include<iostream>
# include<cstdio>
# include<algorithm>
# include<queue>
# include<stack>
# include<math.h>
# include<cstring>
# define INF 0x3f3f3f3f
# define ll long long
using namespace std;

const int maxn = 1000+10;
const int maxm = 90000000+10;

int t,n;
int tmp;
int st[maxn],sum[maxn];//st[i]:第i位是1的前i位数列个数;
int ans[maxn];
int maxx;

int main(){
    st[1]=st[2]=1;
    sum[1]=1,sum[2]=2;
    for(int i=3;i<maxn;i++){
        st[i]=st[i-1]+st[i-2];
        sum[i]=st[i]+sum[i-1];
        if(sum[i]>maxm) {tmp=i; break; }
    }
    //freopen("a.txt","r",stdin);
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        maxx=0;
        memset(ans,0,sizeof(ans));
        while(n){
            int l=lower_bound(sum+1,sum+tmp+1,n)-sum;
            maxx=max(maxx,l);
            ans[l]=1;
            n-=(sum[l-1]+1);
        }
        for(int i=maxx;i>=1;i--){
            printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值