标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10632 Accepted Submission(s): 4230
1 #include <stdio.h> 2 #define max(a,b) a>b?a:b 3 int main() 4 { 5 int n; 6 int a[101],b[101]; 7 int f[100001]; 8 int i,j,m,p,g; 9 int sum=0; 10 freopen("in.txt","r",stdin); 11 int q; 12 while(scanf("%d%d",&n,&m)!=EOF) 13 { 14 sum=0; 15 for(i=0;i<=m;i++) f[i]=0; 16 if(m==0&&n==0) 17 return 0; 18 for(i=1;i<=n;i++) 19 scanf("%d",&a[i]); 20 for(i=1;i<=n;i++) 21 scanf("%d",&b[i]); 22 for(i=1;i<=n;i++) 23 { 24 p=0; 25 g=1; 26 while(b[i]>g) 27 { 28 q=a[i]*g; 29 for(j=m;j>=q;j--) 30 { 31 f[j]=max(f[j],f[j-a[i]*g]+a[i]*g); 32 } 33 b[i]-=g; 34 g=g*2; 35 } 36 q=a[i]*b[i]; 37 for(j=m;j>=q;--j) 38 f[j]=max(f[j],f[j-a[i]*b[i]]+a[i]*b[i]); 39 } 40 for(i=1;i<=m;i++) 41 { 42 if(f[i]==i) 43 sum++; 44 } 45 printf("%d\n",sum); 46 } 47 }
母函数超时代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 int val[100+10],num[100+10]; 7 int c1[100000+1],c2[100000+1]; 8 int m,n; 9 int solve() 10 { 11 int ans=0,u; 12 int i,j,k,p=num[1]*val[1]; 13 int t=min(val[1]*num[1],m); 14 for(i=0;i<=t;i+=val[1]) 15 c1[i]=1; 16 for(i=0;i<=t;i++) 17 c2[i]=0; 18 for(i=2;i<=n;i++) 19 { 20 for(j=0;j<=p;j++) 21 { 22 u=-1; 23 for(k=0;j+k<=m;k+=val[i]) 24 { 25 u++; 26 if(u>num[i]) 27 break; 28 c2[j+k]+=c1[j]; 29 } 30 } 31 p=j+k; 32 for(j=0;j<=p;j++) 33 { 34 c1[j]=c2[j]; 35 c2[j]=0; 36 } 37 } 38 for(i=1;i<=m;i++) 39 { 40 if(c1[i]!=0) 41 ans++; 42 } 43 return ans; 44 } 45 int main() 46 { 47 int i,j; 48 freopen("in.txt","r",stdin); 49 while(scanf("%d%d",&n,&m)) 50 { 51 if(m==0&&n==0) 52 return 0; 53 for(i=1;i<=n;i++) 54 scanf("%d",&val[i]); 55 for(i=1;i<=n;i++) 56 scanf("%d",&num[i]); 57 cout<<solve()<<endl; 58 } 59 return 0; 60 }
标签:
原文地址:http://www.cnblogs.com/a1225234/p/5056597.html