题解链接
https://www.lucien.ink/archives/228/
题目链接
https://arc060.contest.atcoder.jp/tasks/arc060_a
题目
Problem Statement
Tak has cards. On the -th card is written an integer . He is selecting one or more cards from these cards, so that the average of the integers written on the selected cards is exactly . In how many ways can he make his selection?
Constraints
are integers.
Partial Score
200 points will be awarded for passing the test set satisfying .
Input
The input is given from Standard Input in the following format:
Output
Print the number of ways to select cards such that the average of the written integers is exactly .
题意
给你N
张卡片,每个卡片有一个权值,然你选任意多张卡片(不能为
),使得这些卡片权值的平均值严格等于A
,问你有多少种选法。
思路
因为比较小,所以三维递推的复杂度是允许的。dp[i][j][k]
代表前i
个元素里严格选j
个的和为k
的方案数,很容易得到递推公式:
然后
的long long
开不下,又发现每一层只会用到上一层的状态,用滚动数组优化一下第一维就可以。
实现
#include <bits/stdc++.h>
const int maxn = 57;
long long f[3][maxn][maxn * maxn], ans;
int n, num[maxn], a;
int main() {
scanf("%d%d", &n, &a);
for (int i = 1; i <= n; i++) scanf("%d", num + i);
f[0][0][0] = f[1][0][0] = 1;
for (int i = 1, cur = 1; i <= n; i++, cur ^= 1)
for (int k = 1; k <= i; k++)
for (int val = 0; val <= 2500; val++) {
f[cur][k][val] = f[cur ^ 1][k][val];
if (val >= num[i]) f[cur][k][val] += f[cur ^ 1][k - 1][val - num[i]];
}
for (int i = 1, cur = n & 1; i <= n; i++) ans += f[cur][i][a * i];
printf("%lld\n", ans);
return 0;
}