Stone game
题目描述:
CSL loves stone games. He has nnn stones; each has a weight aia_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 109+710^9 + 7109+7.Formerly, you are given a labelled multiset S={a1,a2,…,an}S={a_1,a_2,\ldots,a_n}S={a1,a2,…,an}, find the number of subsets of SSS: S′={ai1,ai2,…,aik}S’={a_{i_1}, a_{i_2}, \ldots, a_{i_k} }S′={ai1,ai2,…,aik}, such that (Sum(S′)≥Sum(S−S′))∧(∀t∈S′,Sum(S′)−t≤Sum(S−S′)).\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′)).
输入:
The first line an integer TTT (1≤T≤10)1 \leq T \leq 10)1≤T≤10), which is the number of cases.For each test case, the first line is an integer nnn (1≤n≤3001 \leq n \leq 3001≤n≤300), which means the number of stones. The second line are nnn space-separated integers a1,a2,…,ana_1,a_2,\ldots,a_na1,a2,…,an (1≤ai≤5001 \leq a_i \leq 5001≤ai≤500).
输出:
For each case, a line of only one integer ttt — the number of possible plans. If the answer is too large, please output the answer modulo 109+710^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.
code:
一直在想如何保存,想了许久,现在来补一下题目
我们考虑这道题,意思是,一堆石头,一个人要从中取出一堆石头,使取出来的石头比剩下来的石头的质量要重,并且从中已经取出来的石头堆里取出任意一颗石头,这样的一堆石头比原来剩下的石头的质量要轻,那么只要最轻的那颗石头满足,那么就都满足了
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=355;
const long long mod=1e9+7;
long long dp[maxn*505];
int a[maxn];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int ttt;
scanf("%d",&ttt);
while(ttt--){
int n;
scanf("%d",&n);
memset(dp,0,sizeof(dp));
int sum=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
sum+=a[i];
}
long long ans=0;
dp[0]=1;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
for(int j=sum;j>=a[i];j--){
dp[j]+=dp[j-a[i]];
dp[j]=dp[j]%mod;
if((j>=sum-j)&&(j-a[i]<=sum-j)){
ans+=dp[j-a[i]];
ans=ans%mod;
}
}
}
printf("%lld\n",ans);
}
return 0;
}