zoukankan      html  css  js  c++  java
  • POJ 1742 Coins

    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.

    题目大意:一个多重背包
    解题报告:恶心的卡常题
    几个优化:
    1.对于容量×价值>=m的我们可以直接当成完全背包来做,否则直接分组
    2.memset要删掉,在统计答案时就清空

    #include <algorithm>
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #define RG register
    #define il inline
    #define iter iterator
    #define Max(a,b) ((a)>(b)?(a):(b))
    #define Min(a,b) ((a)<(b)?(a):(b))
    using namespace std;
    typedef long long ll;
    const int N=3005,M=1e5+5;
    bool f[M];int n,m,val[N],tot=0,pv[105],pw[105];
    void work()
    {
    	ll tmp;
    	int k=1;tot=0;f[0]=1;
    	for(int i=1;i<=n;i++)scanf("%d",&pv[i]);
    	for(int i=1;i<=n;i++)scanf("%d",&pw[i]);
    	for(int i=1;i<=n;i++){
    		tmp=(ll)pv[i]*pw[i];
    		if(tmp>=m){
    			for(int j=pv[i];j<=m;j++)f[j]|=f[j-pv[i]];
    			continue;
    		}
    		k=1;
    		while(pw[i]>=k){
    			val[++tot]=pv[i]*k;
    		  pw[i]-=k;k<<=1;
    		}
    		if(pw[i])val[++tot]=pv[i]*pw[i];
    	}
    	for(int i=1;i<=tot;i++){
    		for(int j=m;j>=val[i];j--){
    			if(f[j])continue;
    			f[j]|=f[j-val[i]];
    		}
    	}
    	int ans=0;
    	for(int i=1;i<=m;i++)ans+=f[i],f[i]=0;
    	printf("%d
    ",ans);
    }
    
    int main()
    {
    	while(~scanf("%d%d",&n,&m) && n+m)work();
    	return 0;
    }
    
    
  • 相关阅读:
    MVC基础
    图片水印和图片验证码
    Jquery弹窗
    AJAX基础
    Jquery--动画
    Jquery--动画
    JQuery
    LinkQ 组合查询与分页
    LinQ的简单使用
    JavaScript复习
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7499599.html
Copyright © 2011-2022 走看看