zoukankan      html  css  js  c++  java
  • [BZOJ2287/POJ Challenge]消失之物

    Description
    ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN。 由于她的疏忽, 第 i 个物品丢失了。 “要使用剩下的 N - 1 物品装满容积为 x 的背包,有几种方法呢?” -- 这是经典的问题了。她把答案记为 Count(i, x) ,想要得到所有1 <= i <= N, 1 <= x <= M的 Count(i, x) 表格。

    Input
    第1行:两个整数 N (1 ≤ N ≤ 2 × 103) 和 M (1 ≤ M ≤ 2 × 103),物品的数量和最大的容积。
    第2行: N 个整数 W1, W2, ..., WN, 物品的体积。

    Output
    一个 N × M 的矩阵, Count(i, x)的末位数字。

    Sample Input
    3 2
    1 1 2

    Sample Output
    11
    11
    21

    HINT
    如果物品3丢失的话,只有一种方法装满容量是2的背包,即选择物品1和物品2。


    考虑01背包,假定我们在背包的时候,第i个物品是最后考虑的,那么我们如何得到上一阶段的状态?直接减回去即可……对10取模……

    /*program from Wolfycz*/
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int frd(){
    	int x=0,f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline int read(){
    	int x=0,f=1; char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)	putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int N=2e3;
    int f[N+10],v[N+10],g[N+10];
    int main(){
    	int n=read(),m=read();
    	f[0]=1;
    	for (int i=1,x;i<=n;i++){
    		x=v[i]=read();
    		for (int j=m;j>=x;j--)	f[j]=(f[j]+f[j-x])%10;
    	}
    	for (int i=1;i<=n;i++){
    		memcpy(g,f,sizeof(f));
    		for (int j=v[i];j<=m;j++)	g[j]=(g[j]-g[j-v[i]]+10)%10;
    		for (int j=1;j<=m;j++)	printf("%d",g[j]);
    		putchar('
    ');
    	}
    	return 0;
    }
    
  • 相关阅读:
    Spoj-DWARFLOG Manipulate Dwarfs
    Spoj-DRUIDEOI Fata7y Ya Warda!
    LightOJ1106 Gone Fishing
    LightOJ1125 Divisible Group Sums
    hdu5396 Expression
    cf715B Complete The Graph
    cf601A The Two Routes
    cf602B Approximating a Constant Range
    cf602A Two Bases
    认证方式
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/10020856.html
Copyright © 2011-2022 走看看