HDU 5389 DP

來源:互聯網
上載者:User

標籤:

給出n個人的id,有兩個門,每個門有一個標號a個b,現在我們要將n個人分成兩組,進入兩個門中,使得兩部分人的標號的和(迭代的求,直至變成一位元)分別等於a和b,問有多少種分法,也可全進入其中的一扇門

一個數的數字根只和它mod~9mod 9之後的值有關,只要類似背包就能完成人員分配的計算。

注意處理全從a出或者全從b出的情況

#include "stdio.h"#include "string.h"const int mod=258280327;int x[100010],dp[100010][20];int main(){    int t,a,k,b,n,key;    scanf("%d",&t);    while (t--)    {        scanf("%d%d%d",&n,&a,&b);        key=0;        for(int i=1;i<=n;i++)        {            scanf("%d",&x[i]);            key=(key+x[i])%9;        }        memset(dp,0,sizeof(dp));        dp[0][0]=1;        //dp[1][x[1]]=1;        for(int i=1;i<=n;i++)        {            for(int j=0;j<=9;j++)            {                k=(x[i]+j)%9;                dp[i][j]+=dp[i-1][j];                dp[i][k]+=dp[i-1][j];                dp[i][j]%=mod;                dp[i][k]%=mod;            }        }        int ans=0;        k=(a+b)%9;        a%=9;        b%=9;        if(k==key)        {            ans+=dp[n][a];            if(a==key)                ans--;            if (a==0) ans--;        }        a%=9;        b%=9;        if(a==key)            ans++;        if(b==key)            ans++;        printf("%d\n",ans);    }    return 0;}



著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

HDU 5389 DP

聯繫我們

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