上海网络赛 J Stone game (dp)

CSL loves stone games. He has nn stones; each has a weight a_iai​. CSL wants to get some stones. The rule is that the pile he gets should have a higher or equal total weight than the rest; however if he removes any stone in the pile he gets, the total weight of the pile he gets will be no higher than the rest. It's so easy for CSL, because CSL is a talented stone-gamer, who can win almost every stone game! So he wants to know the number of possible plans. The answer may be large, so you should tell CSL the answer modulo 10^9 + 7109+7.

Formerly, you are given a labelled multiset S=\{a_1,a_2,\ldots,a_n\}S={a1​,a2​,…,an​}, find the number of subsets of SS: S'=\{a_{i_1}, a_{i_2}, \ldots, a_{i_k} \}S′={ai1​​,ai2​​,…,aik​​}, such that

\left(Sum(S') \ge Sum(S-S') \right) \land \left(\forall t \in S', Sum(S') - t \le Sum(S-S') \right) .(Sum(S′)≥Sum(S−S′))∧(∀t∈S′,Sum(S′)−t≤Sum(S−S′)).

InputFile

The first line an integer TT (1 \leq T \leq 10)1≤T≤10), which is the number of cases.

For each test case, the first line is an integer nn (1 \leq n \leq 3001≤n≤300), which means the number of stones. The second line are nn space-separated integers a_1,a_2,\ldots,a_na1​,a2​,…,an​ (1 \leq a_i \leq 5001≤ai​≤500).

OutputFile

For each case, a line of only one integer tt --- the number of possible plans. If the answer is too large, please output the answer modulo 10^9 + 7109+7.

样例输入复制

2
3
1 2 2
3
1 2 4

样例输出复制

2
1

样例解释

In example 1, CSL can choose the stone 1 and 2 or stone 1 and 3. 

 In example 2, CSL can choose the stone 3.

          sort以后从大到小枚举集合S'中重量最小的那块石头,然后用大于他的那些石头看看能凑出符合条件的能有多少种。

          所谓的条件:需要凑出重量大于等于

		int minn = (sum + 1) / 2 - da[i];

                               小于等于

                 int maxn = (sum + da[i]) / 2 - da[i];

         的就好了。这样凑出的s'就能符合题目中的条件。

#include <bits/stdc++.h>
using namespace std;
int da[305];
long long dp[150003];
const int mod = 1e9 + 7;
inline bool sc(int &num)
{
	char in; bool IsN = false;
	in = getchar();
	if (in == EOF) return false;
	while (in != '-' && (in<'0' || in>'9')) in = getchar();
	if (in == '-') { IsN = true; num = 0; }
	else num = in - '0';
	while (in = getchar(), in >= '0'&&in <= '9') {
		num *= 10, num += in - '0';
	}
	if (IsN) num = -num;
	return true;
}
int main() {
	int te, cas = 1;
	sc(te);
	while (te--) {
		int n; sc(n);
		int sum = 0;
		for (int i = 1; i <= n; i++) {
			sc(da[i]);
			sum += da[i];
		}
		sort(da + 1, da + 1 + n);
		for (int i = 0; i < sum; i++)
			dp[i] = 0;
		long long ans = 0;
		dp[0] = 1;
		for (int i = n; i >= 1; i--) {
			int minn = (sum + 1) / 2 - da[i];
			int maxn = (sum + da[i]) / 2 - da[i];
			for (int j = minn; j <= maxn; j++) {
				if (j < 0)continue;
				ans += dp[j];
				ans %= mod;
			}
			for (int j = sum; j >= da[i]; j--) {
				dp[j] += dp[j - da[i]];
				dp[j] %= mod;
			}
		}
		ans %= mod;
		printf("%lld\n", ans);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/chenshibo17/article/details/100868294