zoukankan      html  css  js  c++  java
  • Stone game

    •  31.91%
    •  3000ms
    •  262144K
     

    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') ight) land left(forall t in S', Sum(S') - t le Sum(S-S') ight) .(Sum(S)Sum(SS))(tS,Sum(S)tSum(SS)).

    InputFile

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

    For each test case, the first line is an integer nn(1 leq n leq 3001n300), 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 5001ai500).

    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.

    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const int mod = 1000000007;
    const int maxn = 305;
    
    int T, n;
    int c[maxn], dp[308][150008];
    
    
    int main() {
    //#ifndef ONLINE_JUDGE
    //    freopen("pre.txt", "r", stdin);
    //    freopen("1.txt","w",stdout);
    //#endif
        scanf("%d", &T);
        while (T--) {
            scanf("%d", &n);
            ll tot = 0, res = 0;
            for (register int i = 1; i <= n; ++i) {
                scanf("%d", &c[i]);
                tot += c[i];
            }
            sort(c + 1, c + 1 + n);
            for (register int i = 0; i <= tot; ++i)dp[n + 1][i] = 0;
            dp[n + 1][0] = 1;
            for (register int i = n; i >= 1; --i) {
                for (register int j = 0; j <= tot; ++j) {
                    dp[i][j] = dp[i + 1][j];
                    if (j >= c[i]) {
                        dp[i][j] += dp[i + 1][j - c[i]];
                        dp[i][j] %= mod;
                        if (j >= tot - j && c[i] >= j - (tot - j)) {
                            res += dp[i + 1][j - c[i]];
                            res %= mod;
                        }
                    }
                }
            }
            printf("%lld
    ", res);
        }
        return 0;
    }
  • 相关阅读:
    java JDBC DAO ORM Domain
    《硅谷钢铁侠-- 埃隆·马斯克的冒险人生》
    在启动MYSQL时出现问题:“ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061)”
    使用IntelliJ IDEA开发java web
    [django]用日期来查询datetime类型字段
    2020/5/31
    图解排序算法(三)之堆排序
    图解排序算法(二)之希尔排序
    图解排序算法(一)之3种简单排序(选择,冒泡,直接插入)
    Oracle约束(Constraint)详解
  • 原文地址:https://www.cnblogs.com/czy-power/p/11524888.html
Copyright © 2011-2022 走看看