POJ1742--Coins

2782 단어 동적 기획배낭.
Description
People in Silverland use coins.They have coins of value A1,A2,A3...An 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 reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output
8
4
전설의 남자 여덟 문제 중 하나...
다중 가방 문제, 나는 2진 최적화를 0-1 가방으로 전환, 시간 초과.
다음은 시간 초과 코드입니다.
#include 
#include 
#include 
using namespace std;
int dp[100008];
int xinstv[1008],xinstw[1008];
int A[108];int W[108];
bool vis[100008];
inline int max(int a,int b)
{
	return a>b?a:b;
}
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)==2&&(n||m))
	{
		memset(vis,0,sizeof(vis));
		memset(dp,0,sizeof(dp));
		int t=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&A[i]);//A[i]   
		}
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&W[i]);//    
		}
		for(int i=1;i<=n;i++)
		{
			int temp=1;
			while(W[i]/temp)
			{
				xinstv[t]=temp*A[i];
				xinstw[t++]=temp*A[i];
				W[i]-=temp;
				temp=temp*2;
			}
			xinstv[t]=W[i]*A[i];
			xinstw[t++]=W[i]*A[i];
		}
		int sum=0;
		vis[0]=1;
		for(int i=0;i=xinstw[i];j--)
			{
				if(!vis[j]&&vis[j-xinstv[i]])
				{
					sum++;
					vis[j]=1;
				}
			}
		}
		printf("%d
",sum); } return 0; }
   AC  。。
#include
int use[100005],c[105];
char p[100005];
int main()
{
    int n,m,i,j,s,res;
    p[0]=1;
    while(scanf("%d%d",&n,&m),n||m)
    {
        res=0;
        for(i=1;i<=m;i++) p[i]=0;
        for(i=0;i

 
  


 

좋은 웹페이지 즐겨찾기