P4141 消失之物

P4141 消失之物

\(f(i, j)\) 表示用前 \(i\) 个物品恰满体积为 \(j\) 的背包方案数。

\(g(i, j)\) 表示所有 \(n\) 个物品里,不使用第 \(i\) 个物品恰满体积为 \(j\) 的背包方案数(也即答案数组)。

容斥:\(g(i, j)\) 可以表示为所有 \(n\) 个物品恰满体积 \(j\) 背包的方案数 \(-\) 必须使用第 \(i\) 个物品恰满体积为 \(j\) 的背包方案数。

前者为 \(f(n, j)\)。后者思考一下,必须使用第 \(i\) 个物品恰满体积为 \(j\) 的方案,与不使用第 \(i\) 个物品恰满体积 \(j - v_i\) 的方案一一对应,所以其方案数就是 \(g(i, j - v_i)\)。所以 \(g(i, j) = f(n, j) - g(i, j - v_i)\)

保证转移 \(g(i, j)\)\(j\) 顺序枚举即可。

/*
 * @Author: crab-in-the-northeast 
 * @Date: 2023-06-27 20:51:50 
 * @Last Modified by: crab-in-the-northeast
 * @Last Modified time: 2023-06-27 21:29:30
 */
#include <bits/stdc++.h>
inline int read() {
	int x = 0;
	bool f = true;
	char ch = getchar();
	for (; !isdigit(ch); ch = getchar())
		if (ch == '-')
			f = false;
	for (; isdigit(ch); ch = getchar())
		x = (x << 1) + (x << 3) + ch - '0';
	return f ? x : (~(x - 1));
}

const int N = 2005, M = 2005;
int f[N][M], g[N][M];
const int mod = 10;
int a[N];

signed main() {
	int n = read(), m = read();
	for (int i = 1; i <= n; ++i)
		a[i] = read();

	f[0][0] = 1;
	for (int i = 1; i <= n; ++i) {
		for (int j = 0; j <= m; ++j) {
			f[i][j] = f[i - 1][j];
			if (j >= a[i])
				(f[i][j] += f[i - 1][j - a[i]]) %= 10;
		}
	} 

	for (int i = 1; i <= n; ++i, puts("")) {
		for (int j = 0; j <= m; ++j) {
			g[i][j] = f[n][j]; 
			if (j >= a[i])
				(g[i][j] += 10 - g[i][j - a[i]]) %= 10;
			if (j)
				putchar(g[i][j] ^ '0');
		}
	}

	return 0;
}

posted @ 2023-06-28 08:23  dbxxx  阅读(95)  评论(0编辑  收藏  举报