hdu(2110)(?母函数问题)
#include <iostream> #include <cstdio> #include <string.h> using namespace std; int dp[15000],t,sum; /*void bagg01(int value,int cost) { for (int i=t;i>=cost;--i) dp[i]=(dp[i]+dp[i-cost])%10000; } void baggall(int value,int cost) { for (int i=cost;i<=t;++i) dp[i]=(dp[i]+dp[i-cost])%10000; } void fun(int value,int num) { if (value*num>=t) {baggall(value,value);return;} int k=1; while(k<=num) { bagg01(k*value,k*value); num-=k; k=k*2; } bagg01(num*value,num*value); }*/ void fun(int value,int num) { for (int j=sum;j>=0;--j) for (int k=1;k<=num&&j>=k*value;++k) dp[j]=(dp[j]+dp[j-k*value])%10000;现在还不知道这个母函数与多重背包有什么区别 } int main() { int n,i,p[1500],m[1500]; while(scanf("%d",&n),n) { sum=0; for (i=1;i<=n;++i) {scanf("%d%d",&p[i],&m[i]);sum=sum+p[i]*m[i];} t=sum/3; if (sum!=3*t) { printf ("sorry\n"); continue; } memset(dp,0,sizeof(dp)); dp[0]=1; for (i=1;i<=n;++i) fun(p[i],m[i]);//代表价值和数量 if (dp[t]!=0) printf ("%d\n",dp[t]); else printf ("sorry\n"); } return 0; }