[ARC060]高橋君とカード / Tak and Cards(dp,背包)

题目链接:http://arc060.contest.atcoder.jp/tasks/arc060_a

题意:给n个数字,求这n个数字平均数为a的组合数。

思路:第一次打atcoder,这个题目分部分解和完全解两种。部分解要求n<=16,完全解n<=50。

部分解的做法很简单,枚举所有子集,看看是否满足条件即可。

完全解要用背包来做,设dp(i,j,k)为在第i个数字前选j个,和为k的所有组合数目。由于我们知道平均数的计算方法为Σxi/X,设a=Σxi/X,则a*X=Σxi。我们最后只需要统计所有的dp(n,X,a*X)就行了。

转移的话首先计数要把之前的结果转移到当前结果下,dp(i,j,k)+=dp(i-1,j,k)。接下来就是背包了,dp(i,j,k)+=dp(i-1,j-1,k-x(i-1)) | {k >= x(i-1)}

 1 /*
 2 ━━━━━┒ギリギリ♂ eye!
 3 ┓┏┓┏┓┃キリキリ♂ mind!
 4 ┛┗┛┗┛┃\○/
 5 ┓┏┓┏┓┃ /
 6 ┛┗┛┗┛┃ノ)
 7 ┓┏┓┏┓┃
 8 ┛┗┛┗┛┃
 9 ┓┏┓┏┓┃
10 ┛┗┛┗┛┃
11 ┓┏┓┏┓┃
12 ┛┗┛┗┛┃
13 ┓┏┓┏┓┃
14 ┃┃┃┃┃┃
15 ┻┻┻┻┻┻
16 */
17 #include <algorithm>
18 #include <iostream>
19 #include <iomanip>
20 #include <cstring>
21 #include <climits>
22 #include <complex>
23 #include <cassert>
24 #include <cstdio>
25 #include <bitset>
26 #include <vector>
27 #include <deque>
28 #include <queue>
29 #include <stack>
30 #include <ctime>
31 #include <set>
32 #include <map>
33 #include <cmath>
34 //#include <unordered_map>
35 using namespace std;
36 #define fr first
37 #define sc second
38 #define cl clear
39 #define BUG puts("here!!!")
40 #define W(a) while(a--)
41 #define pb(a) push_back(a)
42 #define Rint(a) scanf("%d", &a)
43 #define Rll(a) scanf("%I64d", &a)
44 #define Rs(a) scanf("%s", a)
45 #define Cin(a) cin >> a
46 #define FRead() freopen("in", "r", stdin)
47 #define FWrite() freopen("out", "w", stdout)
48 #define Rep(i, len) for(int i = 0; i < (len); i++)
49 #define For(i, a, len) for(int i = (a); i < (len); i++)
50 #define Cls(a) memset((a), 0, sizeof(a))
51 #define Clr(a, x) memset((a), (x), sizeof(a))
52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
53 #define lrt rt << 1
54 #define rrt rt << 1 | 1
55 #define pi 3.14159265359
56 #define RT return
57 #define lowbit(x) x & (-x)
58 #define onenum(x) __builtin_popcount(x)
59 typedef long long LL;
60 typedef long double LD;
61 typedef unsigned long long ULL;
62 typedef pair<int, int> pii;
63 typedef pair<string, int> psi;
64 typedef pair<LL, LL> pll;
65 typedef map<string, int> msi;
66 typedef vector<int> vi;
67 typedef vector<LL> vl;
68 typedef vector<vl> vvl;
69 typedef vector<bool> vb;
70 
71 const int maxn = 55;
72 const int maxm = 2550;
73 LL ret;
74 LL n, a;
75 LL x[maxn];
76 LL dp[maxn][maxn][maxm];
77 
78 int main() {
79     // FRead();
80     while(cin >> n >> a) {
81         Cls(dp); dp[0][0][0] = 1; ret = 0;
82         Rep(i, n) cin >> x[i];
83         For(i, 1, n+1) {
84             Rep(j, i+1) {
85                 Rep(k, 2501) {
86                     dp[i][j][k] += dp[i-1][j][k];
87                     if(j >= 1 && k >= x[i-1]) {
88                         dp[i][j][k] += dp[i-1][j-1][k-x[i-1]];
89                     }
90                 }
91             }
92         }
93         For(i, 1, n+1) ret += dp[n][i][a*i];
94         cout << ret << endl;
95     }
96     RT 0;
97 }
原文地址:https://www.cnblogs.com/kirai/p/5817584.html