HDU2079选课时间(母函数)
母函数的简单应用http://acm.hdu.edu.cn/showproblem.php?pid=2079
介绍见另一篇随笔HDU1028Ignatius and the Princess III(母函数)
1 #include<stdio.h> 2 3 4 5 int main() 6 { 7 int T; 8 while(~scanf("%d", &T))while(T--) 9 { 10 int c1[41]={0},c2[41]={0}; 11 int num,val; 12 int i,n,k; 13 scanf("%d%d", &n, &k); 14 for(i = 1; i<=k;i++) 15 { 16 scanf("%d%d",&val, &num); 17 if(i ==1) 18 { 19 for(int j = 0; j<=num && j*val<=n ;j++) 20 { 21 c1[j*val] = 1; 22 c2[j*val] = 0; 23 } 24 } 25 else 26 { 27 for(int j = 0;j <= n;j ++ ) 28 { 29 for(int t = 0;t*val +j <=n && t<=num; t++) 30 { 31 c2[j+t*val] += c1[j]; 32 } 33 } 34 for(j =0;j<=n;j++) 35 { 36 c1[j]=c2[j]; 37 c2[j]=0; 38 } 39 } 40 } 41 printf("%d\n", c1[n]); 42 } 43 return 0; 44 }