hdu5106(組合數學)

題意:給一個二進制數R,長度不超過1000,問比R小並且1的個數爲n的二進制數的和是多少。


解法:其實只需要枚舉某個二進制數和R第一個1不同的位置就行了。因爲如果R某個位置的1變成了0,那麼以後01就隨便取變成了組合數的問題。求和就預處理下1000內2的冪和前綴和了。我做時候複雜化,又求了小於等於R且1個數是n的二進制了。其實不用這一步了。


代碼:

/******************************************************
* @author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=1010;
const LL INF=1000000007 ;
LL C[Max][Max];
LL po[Max];
LL sum[Max];
char s[Max];
int n;
void init()
{
    for(int i=0; i<Max; i++)
        for(int j=0; j<=i; j++)
            C[i][j]= j==0?1:(C[i-1][j-1]+C[i-1][j])%INF;
    po[0]=1;
    sum[0]=1;
    for(int i=1; i<Max; i++)
    {
        po[i]=po[i-1]*2%INF;
        sum[i]=(sum[i-1]+po[i])%INF;
    }
}
LL getans(bool f)
{
    int have=0;
    LL ans=0;
    int len=strlen(s);
    LL be=0;
    for(int i=0; i<len; i++)
    {
        int sheng=len-1-i;
        int ge=n-have-1;
        if(s[i]=='1')
        {
            ans=(ans+(C[sheng-1][ge]*sum[sheng-1]%INF)%INF)%INF;
            //cout<<"be: "<<be<<" "<<C[sheng+1][ge+1]<<endl;
            //cout<<ans<<" ";
            ans=(ans+be*C[sheng][ge+1]%INF)%INF;
            //cout<<ans<<endl;
            if(f)ans=(ans+po[len-i-1])%INF;
            be=(be+po[len-i-1])%INF;

            //cout<<ans<<endl;
            have++;
        }
    }
    return ans%INF;
}
LL solve()
{
    int len=strlen(s);
    int m=0;
    for(int i=0; i<len; i++)
        if(s[i]=='1')
            m++;
    if(m<n)
    {
        int now=0;
        int need=n-m+1;
        int i;
        for(i=len-1; i>=0; i--)
        {
            if(s[i]=='0')
                now++;
            if(now==need)
                break;
        }
        for(; i>=0; i--)
        {
            if(s[i]=='1')
            {
                s[i]='0';
                i++;
                for(; i<len; i++)
                {
                    if(s[i]=='0')
                    {
                        need--;
                        s[i]='1';
                    }
                    if(need==0)
                        break;
                }
                int j=i+1;
                i++;
                for(;i<len;i++)
                {
                    if(s[i]=='1')
                    {
                        s[i]='0';
                        s[j++]='1';
                    }
                }
                break;
            }
        }
    }
    else if(n<m)
    {
        int need=m-n;
        for(int i=len-1; i; i--)
        {
            if(s[i]=='1')
                s[i]='0',need--;
            if(need==0)
                break;
        }
    }
    int sad=0;
    for(int i=0; i<len; i++)if(s[i]=='1')sad++;
    if(sad!=n)
        return 0;
    //puts(s);
    return getans(m!=n);
}

int main()
{
    init();
    while(scanf("%d%s",&n,&s)==2)
    {
        if(n>strlen(s)||n==0)
            cout<<"0"<<endl;
        else
            cout<<solve()<<endl;
    }
    return 0;
}

發佈了217 篇原創文章 · 獲贊 10 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章