hdu 2079 選課時間(題目已修改,注意讀題) 多重背包,hdu2079

來源:互聯網
上載者:User

hdu 2079 選課時間(題目已修改,注意讀題) 多重背包,hdu2079
選課時間(題目已修改,注意讀題)Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3162    Accepted Submission(s): 2472


Problem Description又到了選課的時間了,xhd看著選課表發獃,為了想讓下一學期好過點,他想知道學n個學分共有多少組合。你來幫幫他吧。(xhd認為一樣學分的課沒區別)
 
Input輸入資料的第一行是一個資料T,表示有T組資料。
每組資料的第一行是兩個整數n(1 <= n <= 40),k(1 <= k <= 8)。
接著有k行,每行有兩個整數a(1 <= a <= 8),b(1 <= b <= 10),表示學分為a的課有b門。
 
Output對於每組輸入資料,輸出一個整數,表示學n個學分的組合數。
 
Sample Input

22 21 22 140 81 12 23 24 25 86 97 68 8
 
Sample Output
2445
 

我上一個AC的代碼:

#include <stdio.h>#include <string.h>#define MAX 55int dp[MAX] ;int main(){int t ;scanf("%d",&t);while(t--){int n , k ,a,b;scanf("%d%d",&n,&k) ;memset(dp,0,sizeof(int)*(n+10)) ;dp[0] = 1 ;for(int i = 0 ; i < k ; ++i){scanf("%d%d",&a,&b);for(int j = n ; j >= a ; --j){for(int x = 1 ; x <= b ; ++x){if(j>=x*a)dp[j] += dp[j-x*a] ;}}}printf("%d\n",dp[n]) ;}return 0 ;}

上面的代碼完全是把多重背包轉化為01背包了。


我下面上一份沒有AC的代碼,,但是我很鬱悶,它為什麼不能AC!!!求大神指點,先前是用二進位最佳化的,,錯的離譜的不能在離譜,後來轉化為用01背包和完全背包時,還是wrong。真的很蛋疼,跪求大神

update:我隱隱約約已經知道為什麼了。。


#include <stdio.h>#include <string.h>#define MAX 55int dp[MAX] ;void zeroOnePack(int value , int total){for(int i = total ; i >= value ; --i){dp[i] = dp[i] + dp[i-value] ;}}void completePack(int value , int total){for(int i = value ; i <= total ; ++i){dp[i] = dp[i] + dp[i-value] ;}}void multiplePack(int value , int count , int total){if(count*value > total){completePack(value , total) ;}else{int k = 1 ;while(k<=count){zeroOnePack(k*value , total);++k ;}}}int main(){int t ;scanf("%d",&t);while(t--){int n , k ,a[45],b[45];scanf("%d%d",&n,&k) ;for(int i = 0 ; i < k ; ++i){scanf("%d%d",&a[i],&b[i]);}memset(dp,0,sizeof(int)*(n+10)) ;dp[0] = 1 ;for(int i = 0 ; i < k ; ++i){multiplePack(a[i],b[i],n) ;}printf("%d\n",dp[n]) ;}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.