hdu4906 Our happy ending,狀態壓縮DP

來源:互聯網
上載者:User

標籤:style   blog   strong   io   for   2014   amp   size   



題意:
給一個n個數的數列,從中取一些數構成新數列,
如果新數列中有一些數的和是k,那麼這就是一個好數列,問這樣的數列的個數。
n,k<=20, a[i] <=L


狀態:dp[][state] 中state的二進位每一位表示和為(1~k),1表示可以取到,0表示取不到。


狀態轉移方程:dp[i][state] = sum(dp[i-1][state‘]);   state = 1<<(p-1) || state‘  ||  ((state‘<<p) &SIZE) ; 1<=p<=min(k,L); //SIZE=(1<<k)-1;


#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const LL mod = 1e9 + 7;LL dp[1<<22];int main(){    int t, n, k, L;    scanf("%d",&t);    while(t--)    {        scanf("%d%d%d", &n, &k, &L);        int MIN = min(L, k);        int SIZE = (1<<k)-1;        memset(dp, 0, sizeof dp );        dp[0] = 1;        for(int i=0; i<n; ++i)        {            for(int j=SIZE; j>=0; --j) if(dp[j]>0)            {                LL tmp = dp[j];                for(int p=1; p<=MIN; ++p)                {                    int next=(1<<(p-1))|j|((j<<p)&SIZE);                    //加上p本身這個數 + 原來可取到的數 + 原來可取到的數加上p之後的數,即組成了所有加上p之後的情況                    dp[next] =dp[next] + tmp;                    if(dp[next]>mod) dp[next] -= mod;                }                if(k<L)//第i個數選大於k的數                {                        dp[j] = dp[j] +  (LL)tmp*(L-k);                        if(dp[j]>mod) dp[j] -= mod;                }            }        }        LL ans = 0;        for(int i=0; i<=SIZE; ++i) if(i&(1<<(k-1)))        {            ans = ans + dp[i];            if(ans>mod) ans -= mod;        }        printf("%I64d\n", ans);    }    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.