http://acm.hdu.edu.cn/showproblem.php?pid=1171
这题是1085的通用版本,这个模板可同用于任意面值,任意数量的硬币和
#include<stdio.h> int c1[250010], c2[250010]; int v[55]; int m[55]; int main() { int n,i,j,k,sum,len; while(scanf("%d", &n),n>0) { memset(v, 0, sizeof(v)); memset(m, 0, sizeof(m)); memset(c1, 0, sizeof(c1)); memset(c2, 0, sizeof(c2)); for(sum=0,i=1; i<=n; ++i) { scanf("%d %d", &v[i], &m[i]); sum += v[i]*m[i]; } for(i=0; i<=v[1]*m[1]; i+=v[1]) c1[i] = 1; len = v[1]*m[1]; for(i=2; i<=n; ++i) { for(j=0; j<=len; ++j) for(k=0; k<=v[i]*m[i]; k+=v [i]) { c2[k+j] += c1[j]; } len += v[i]*m[i]; for(j=0; j<=len; ++j) { c1[j] = c2[j]; c2[j] = 0; } } for(i= sum/2; i>=0; --i) if(c1[i] != 0) { printf("%d %d ", sum-i, i); break; } } return 0; }