HDU 5106 Bits Problem (數位DP),hdu5106

來源:互聯網
上載者:User

HDU 5106 Bits Problem (數位DP),hdu5106

題目地址:HDU 5106

這個題要定義個dp結構體,dp[i][j].sum表示當前第i位還剩j個1的時候的和,dp[i][j].tot表示當前第i位還剩j個1的時候的符合要求的個數。不記錄個數的話,當前位上的1無法跟著低位的出現而累加。

代碼如下:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace std;#define LL __int64#define pi acos(-1.0)const int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;char c[1100];LL d[1100];int len;struct node{        LL sum, tot;}dp[1100][1100];node dfs(int cnt, int one, int maxd, int zero){        node tmp, ans;        if(cnt==-1){                tmp.tot=(!one&&maxd);                tmp.sum=0;                return tmp;        }        if(maxd&&zero&&dp[cnt][one].sum!=-1) return dp[cnt][one];        int i, r=maxd?1:c[len-cnt-1]-'0';        ans.sum=0;ans.tot=0;        for(i=0;i<=r;i++){                if(one-i>=0){                        tmp=dfs(cnt-1,one-i,maxd||i<r,zero||i);                        ans.sum+=(tmp.tot*d[cnt]*i+tmp.sum)%mod;                        ans.tot=(ans.tot+tmp.tot)%mod;                }        }        if(maxd&&zero){                dp[cnt][one].sum=ans.sum;                dp[cnt][one].tot=ans.tot;        }        return ans;}void init(){        int i, j;        for(i=0;i<=1000;i++){                for(j=0;j<=1000;j++){                        dp[i][j].sum=-1;                        dp[i][j].tot=0;                }        }        d[0]=1;        for(i=1;i<=1000;i++){                d[i]=d[i-1]*2%mod;        }}int main(){        int n, i, j;        init();        while(scanf("%d",&n)!=EOF){                scanf("%s",c);                len=strlen(c);                node ans=dfs(len-1,n,0,0);                printf("%I64d\n",ans.sum%mod);        }        return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.