What's the problem
查看原帖
What's the problem
270791
WanderingTrader楼主2020/7/1 20:07

kkcoding 1075,没有题解或者讨论版,所以发在这里

题目描述

今天是K的生日,K的朋友们送给他很多硬币。这些硬币有N种不同的面额a[i],并且每种面额的硬币有一定的数量c[i]。因为大家知道K是程序大神,所以朋友们给K出了一道难题,选取任意硬币,最多能组成几种总价值,且总价值不超过m。

输入格式:

多个测试点。每个测试点,第一行包含两个整数N(1<=N<=100),M(M<=100000) ,第二行前N个数表示N种硬币的面额A[i](1<=A[i]<=100000),后N个数表示N种硬币的数量C[i](1<=C[i]<=100000)。以0 0 结尾。

输出格式:

每个测试点输出最多能组成几种总价值,且总价值不超过m。

蒟蒻的代码:

#include <bits/stdc++.h>
using namespace std;
#define N 105
#define M 100005
#define ll long long
int a[N],c[N],n,m;
bool dp[M]={1};
inline int read()
{
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
	return x*f;
}
void bb01(int v)
{
	for(int i = m;i >= v;-- i) dp[i] |= dp[i - v];
}
int main()
{
	n=read(),m=read();
	while(n||m)
	{
		ll ans = 0;
		fill(dp+1,dp + m + 1,0);
		for(int i = 1;i <= n;++ i)
			a[i] = read();
		for(int i = 1;i <= n;++ i)
			c[i] = read();
		for(int i = 1;i <= n;++ i)
		{
			for(int j = 1;j <= c[i];c[i] -= j,j <<= 1)
				bb01(a[i]);
			if(c[i]) bb01(a[i]);
		}
		for(int i = 1;i <= m; ++ i) ans += dp[i];
		printf("%lld\n",ans);
		n=read(),m=read();
	}
	return 0;
}

一片WA还有几个TLE(正常),所以有什么问题,怎么改?望大佬出手相助

2020/7/1 20:07
加载中...