HDU4734——F(x)(數位DP)

來源:互聯網
上載者:User

標籤:style   color   os   strong   io   for   

dp[i][j]表示i位元權值不超過j的數的個數

注意點

dp[i][j]的值不用每次都初始化,因為它的值不受輸入的影響,如果前面算過了就直接拿來用,沒算過就拿來算並記錄下來


<strong>#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<vector>#include<queue>#include<deque>#include<stack>#include<map>#include<set>#include<cmath>#include<cstdlib>#include<cctype>#define inf 0x3f3f3f3f#define maxn 10000typedef long long LL;using namespace std;int m,n,pos,f[11],fa;int dp[11][5000];int num[11];int dfs(int pos,int s,int e){    if(s<0) return 0;    if(pos==-1) return s>=0;    if(!e&&dp[pos][s]!=-1) return dp[pos][s];    int end=e?num[pos]:9;    int sum=0;    for(int i=0;i<=end;i++){        sum+=dfs(pos-1,s-i*f[pos],e&&i==end);    }    return e?sum:dp[pos][s]=sum;}int main(){    for(int i=0;i<=10;i++){        f[i]=1<<i;    }    int t,iCase=1;    cin>>t;    memset(dp,-1,sizeof dp);    while(t--){        scanf("%d%d",&m,&n);        int x=1,fa=0;        while(m){            fa+=m%10*x;            x<<=1;            m/=10;        }        pos=0;        while(n){            num[pos++]=n%10;            n/=10;        }        printf("Case #%d: %d\n",iCase++,dfs(pos-1,fa,1));    }return 0;}</strong>



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.