hdu2152
从给出的N种水果中选出限制范围最大M种
然后母函数
#include <iostream> #include<algorithm> using namespace std; int N, M; //int A, B; struct fruit { int A,B; } X[101]; int a[105], b[105]; int c1[105], c2[105]; bool cmp(fruit a,fruit b) { return (a.B-a.A)>(b.B-b.A); } int main() { while(cin >> N >> M) { for(int i=1; i<=N; ++i) cin >> X[i].A >> X[i].B; memset(c2, 0, sizeof(c2)); memset(c1, 0, sizeof(c1)); sort(X+1,X+N+1,cmp); for(int i=X[1].A;i <=X[1].B; ++i) c1[i] = 1; for(int i=2; i<=M; ++i) { for(int j=0; j<=M; ++j) for(int k=X[i].A; (k+j<=M) && (k<=X[i].B); ++k) c2[k+j] += c1[j]; for(int j=0; j<=M; ++j) { c1[j] = c2[j]; c2[j] = 0; } } cout << c1[M] << endl; } }当然这是错的……
ac代码
#include<iostream> using namespace std; int A[101],B[101],c1[101],c2[101]; int main() { int i,j,m,num,N,M,sum; while(scanf("%d%d",&N,&M)!=EOF) { for(i=1;i<=N;i++) {scanf("%d%d",&A[i],&B[i]);} memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2)); for(i=A[1];i<=B[1];i++) c1[i]=1;//c1[0]=1; for(i=2;i<=N;i++) { for(j=0;j<=M;j++) { if(c1[j]) for(num=A[i];(num+j<=M)&&(num<=B[i]);num++) c2[num+j]+=c1[j]; } for(j=0;j<=M;j++) { c1[j]=c2[j]; c2[j]=0; } } printf("%d\n",c1[M]); } return 0; }