NC51167. Coins
描述
输入描述
The input contains several test cases. The first line of each test case contains two integers ,m.The second line contains 2n integers, denoting . The last test case is followed by two zeros.
输出描述
For each test case output the answer on a single line.
示例1
输入:
3 10 1 2 4 2 1 1 2 5 1 4 2 1 0 0
输出:
8 4
C++14(g++5.4) 解法, 执行用时: 331ms, 内存消耗: 1228K, 提交时间: 2019-08-07 10:30:56
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[100010],d[100010],a[110],c[110],n,m,x,y,ans; int main() { while(cin>>n>>m&&n!=0) { for(int i=1;i<=m;i++) f[i]=0; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++) cin>>c[i]; f[0]=1; for(int i=1;i<=n;i++) { x=a[i],y=c[i]; for(int j=0;j<=m;j++) d[j]=0; for(int j=0;j<=m-x;j++) if(f[j]==1&&d[j]<y&&f[j+x]==0) f[j+x]=1,d[j+x]=d[j]+1; } ans=0; for(int i=1;i<=m;i++) if(f[i]==1) ans++; cout<<ans<<endl; } }
C++11(clang++ 3.9) 解法, 执行用时: 30ms, 内存消耗: 472K, 提交时间: 2019-10-07 17:18:13
#include<bits/stdc++.h> using namespace std; int a[110],b[110]; int main() { int n,m; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) return 0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) scanf("%d",&b[i]); bitset<100010>f;f[0]=1; for(int i=1;i<=n;i++) { int z=1; while(b[i]-z>=0) { b[i]-=z; f|=(f<<(a[i]*z)); z<<=1; } f|=(f<<(a[i]*b[i])); } int ans=0; for(int j=1;j<=m;j++) if(f[j]) ans++; printf("%d\n",ans); } }