HDU2110+母函数

 1 /*

 2 母函数(生成函数)

 3 题意:

 4 有n种资产,每种资产num份,每份有val的价值

 5 问取出总价值的1/3有多少种方案

 6 */

 7 #include<stdio.h>

 8 #include<string.h>

 9 #include<stdlib.h>

10 #include<algorithm>

11 #include<iostream>

12 #include<queue>

13 #include<stack>

14 #include<math.h>

15 #include<map>

16 using namespace std;

17 const int maxn = 10005;

18 int c1[ maxn ],c2[ maxn ];

19 int val[ 105 ],num[ 105 ];

20 int main(){

21     int n;

22     while( scanf("%d",&n)==1,n ){

23         int sum = 0;

24         for( int i=0;i<n;i++ ){

25             scanf("%d%d",&val[i],&num[i]);

26             sum += val[i]*num[i];

27         }

28         if( sum%3!=0 ){

29             printf("sorry\n");

30         }

31         else{

32             int aim = sum/3;

33             memset( c1,0,sizeof( c1 ) );

34             memset( c2,0,sizeof( c2 ) );

35             for( int i=0;i<=val[0]*num[0];i+=val[0] ){

36                 c1[ i ] = 1;

37             }//

38             for( int i=1;i<n;i++ ){

39                 for( int j=0;j<=aim;j++ ){

40                     for( int k=0;(k+j<=aim)&&(k<=val[i]*num[i]);k+=val[i] ){

41                         c2[ k+j ] = c1[ j ]+c2[ k+j ];

42                         c2[ k+j ] %= 10000;

43                     }

44                 }//

45                 for( int j=0;j<=aim;j++ ){

46                     c1[j] = c2[j];

47                     c2[j] = 0;

48                 }//滚动数组

49             }

50             if( c1[aim]!=0 ) printf("%d\n",c1[aim]);

51             else printf("sorry\n");

52         }

53     }

54     return 0;

55 }

56     
View Code

 

你可能感兴趣的:(HDU)