NOJ 2033 一頁書的書 (組合數+dp),noj+dp

來源:互聯網
上載者:User

NOJ 2033 一頁書的書 (組合數+dp),noj+dp

一頁書的書

時間限制(普通/Java) : 1000 MS/ 3000 MS          運行記憶體限制 : 65536 KByte

總提交 : 55            測試通過 : 12 

題目描述

一頁書前輩作為一位得道高僧,在他無悔的生涯中創作了許多經典,被世人稱作百世經綸。這一天有m個粉絲來膜拜書大,書大很開心,決定送他們每人一本經典。已知一頁書一共創作了n部作品,每部作品分別有a1、a2…an份藏本,那麼書大一共可以有多少種送書的選擇呢?(由於計算結果可能很大,請把結果對1000000007模數)



輸入

第一行是一個正整數T表示有T組資料

每組資料第一行是兩個正整數n,m(n,m<=20)

第二行是n個正整數ai(ai<=20)

輸出

一個正整數k表示方法的總數對1000000007(10^9+7)模數的結果

範例輸入

2

2 3
2 2

2 3
3 3

範例輸出

6

8


題目連結:http://acm.njupt.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=2033

題目分析:dp[i][j]表示前i個人給j種書的方案數,則dp[i][j] = Σ(0, min(a[j], i)) c[i][k] * dp[i - k][j - 1] 前i個人給j種書的方案等於,從i個人中選k個數來給第j本的方案數乘前i - k個人給j-1種書的方案數,初始時dp[i][1] = 1表示前i個人給1本書的方案,顯然為1,注意i要小於某冊書數量的最大值,否則不存在


#include <cstdio>#include <cstring>#include <algorithm>#define ll long longusing namespace std;int const MOD = 1e9 + 7;int const MAX = 22;int a[MAX], dp[MAX][MAX];ll c[MAX][MAX];int cmp(int a, int b){    return a > b;}void pre(){    memset(c, 0, sizeof(c));    for(int i = 0; i < 22; i++)        c[i][0] = 1;    for(int i = 1; i < 22; i++)        for(int j = 1; j <= 22; j++)            c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % MOD;}int main(){    pre();    int T;    scanf("%d", &T);    while(T--)    {        int n, m;        scanf("%d %d", &n, &m);        for(int i = 1; i <= n; i++)            scanf("%d", &a[i]);        sort(a + 1, a + n + 1, cmp);        memset(dp, 0, sizeof(dp));        for(int i = 0; i <= m && i <= a[1]; i++)            dp[i][1] = 1;        for(int i = 0; i <= m; i++)            for(int j = 1; j <= n; j++)                for(int k = 0; k <= min(a[j], i); k++)                    dp[i][j] = ((dp[i][j] % MOD) + ((c[i][k] * dp[i - k][j - 1]) % MOD) % MOD);        printf("%d\n", dp[m][n] % MOD);    }}


聯繫我們

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