列表

详情


NC51167. Coins

描述

People in Silverland use coins.They have coins of value A_1,A_2,A_3...A_n Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which readsn,m,A_1,A_2,A_3...A_nandC_1,C_2,C_3...C_ncorresponding to the number of Tony's coins of valueA_1,A_2,A_3...A_n then calculate how many prices(form 1 to m) Tony can pay use these 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 A_1,A_2,A_3...A_n,C_1,C_2,C_3...C_n. 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);
	}
} 

上一题