CSL loves stone games. He has n stones; each has a weight a i a_i ai . 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 1 0 9 10^9 109+ 7.Formerly, you are given a labelled multiset
S = { a 1 , a 2 , … , a n } S=\{a_1,a_2,\ldots,a_n\} S={a1,a2,…,an}, find the number of subsets of S: S ′ = { a i 1 , a i 2 , … , a i k } S'=\{a_{i_1}, a_{i_2}, \ldots, a_{i_k} \} S′={ai1,ai2,…,aik} such that
S u m ( S ′ ) ≥ S u m ( S − S ′ ) ) ∧ ( ∀ t ∈ S ′ , S u m ( S ′ ) − t ≤ S u m ( S − S ′ ) ) . Sum(S ′ )≥Sum(S−S ′ ))∧(∀t∈S ′ ,Sum(S ′ )−t≤Sum(S−S ′ )). Sum(S′)≥Sum(S−S′))∧(∀t∈S′,Sum(S′)−t≤Sum(S−S′)).
InputFile
The first line an integer T (1≤T≤10), which is the number of cases.
For each test case, the first line is an integer nn (1≤n≤300), which means the number of stones. The second line are nn space-separated integers a 1 ,a 2 ,…,an (1≤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 1 0 9 + 7 10^9+7 109+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.
总结下题意,你有一堆石头,你需要选出其中的若干个石头,使得选出石头的重量大于未选出的石头的重量①,并且去掉选出石头中的任意的一个,新的石头堆重量小于未选出的石头的重量②。求有多少种方案满足题意。
因为是求总方案数,所以考虑需要使用DP来做。考虑条件①,它将总重量分成两部分,总重量sum,选中的重量sum1,余下的重量sum-sum1,条件① => sum1>=sum-sum1。
dp[i]表示重量为i的石头堆的方案数。
考虑条件②,因为是任选,所以我们考虑影响重量最小的变量(因为去掉最小的满足,去掉大的一定满足),所以可以先将石头从大到小排序,以当前重量x[i]为最小值的石头堆即x[1]+x[2]…+x[i]。这样通过确定一个石头堆中的最小值,枚举石头堆总质量,就可以获得答案。
dp[j]=dp[j]+dp[j-x[i]](常规dp获得不考虑条件②的所有方案数)
if(j>=sum-sum1&&j-x[i]<=sum-sum1)
ans=(ans+dp[j-x[i]]) (加入判断获得答案)
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int mod=1e9+7;
int dp[160000],x[4000];
bool cmp(int a,int b){
return a>b;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int sum=0,ans=0;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
scanf("%d",&x[i]);
sum+=x[i];
}
dp[0]=1;
sort(x+1,x+1+n,cmp);
for(int i=1;i<=n;i++){
for(int j=sum;j>=x[i];j--){
int yu=sum-j;
dp[j]=(dp[j]+dp[j-x[i]])%mod;
if(j>=yu&&j-x[i]<=yu){
ans=(ans+dp[j-x[i]])%mod;
}
}
}
printf("%d\n",ans%mod);
}
return 0;
}