poj 3252 Round Numbers(排列组合)

15 篇文章 0 订阅
2 篇文章 0 订阅

【题目大意】:让你找两个数之间有多少个round number。round number的定义是:把一个数化成2进制,如果0的个数>=1的个数。那么该数为round number。

【解题思路】:

先把输入的数化成2进制,然后得到长度len,那么很容易求得长度比其小的round number的数目。然后再对长度len来说,找每一位比它小的round number的数目。

不妨设count(n,k)为2进制n位时需要有k个0时的round number数。b数组记录该数的2进制的每一位。那么有递归公式:

 count(n,k)=count(n-1,k-1)(if b[n-1]==0).

 count(n,k)=c(n-2,k-1)+count(n-1,k).(ifb[n-1]==1,)


【代码】:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <string>
#include <cctype>
#include <map>
#include <iomanip>
                   
using namespace std;
                   
#define eps 1e-8
#define pi acos(-1.0)
#define inf 1<<30
#define linf 1LL<<60
#define pb push_back
#define lc(x) (x << 1)
#define rc(x) (x << 1 | 1)
#define lowbit(x) (x & (-x))
#define ll long long

int c[50][50];
int cc[50];
int b[50];

int to_binary(int t){
    int i=1;
    while(t){
        b[i++]=t%2;
        t/=2;
    }
    return i-1;
}

int count(int n,int k){
    if(k>=n||k<0)  return 0;
    if((n==1&&k==0)||(n==2&&k==1))  return 1;
    if(b[n-1]==0)  return count(n-1,k-1);
    else  return c[n-2][k-1]+count(n-1,k);
}

int solve(int n){
    int ans=0;
    int len=to_binary(n);
    for (int i=(len+1)/2; i<len; i++)
        ans+=count(len,i);
    for (int i=1; i<len; i++)
        ans+=cc[i];
    return ans;
}

void init(){
    c[1][0]=c[1][1]=1;
    for (int i=2; i<=32; i++){
        c[i][0]=1;
        for (int j=1; j<=i; j++)
            c[i][j]=c[i-1][j]+c[i-1][j-1];
    }
    cc[1]=0;
    for (int i=2; i<=32; i++){
        for (int j=(i+1)/2; j<i; j++)
            cc[i]+=c[i-1][j];
    }
    return ;
}

int main(){
    init();
    int st,en;
    while (~scanf("%d%d",&st,&en)){
        printf("%d\n",solve(en)-solve(st-1));
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值