Submission #5069151


Source Code Expand

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define ALL(x) begin(x), end(x)
using namespace std;

template <int32_t MOD>
struct mint {
    int32_t value;
    mint() = default;
    mint(int32_t value_) : value(value_) {}
    inline mint<MOD> operator + (mint<MOD> other) const { int32_t c = this->value + other.value; return mint<MOD>(c >= MOD ? c - MOD : c); }
    inline mint<MOD> operator - (mint<MOD> other) const { int32_t c = this->value - other.value; return mint<MOD>(c <    0 ? c + MOD : c); }
    inline mint<MOD> operator * (mint<MOD> other) const { int32_t c = (int64_t)this->value * other.value % MOD; return mint<MOD>(c < 0 ? c + MOD : c); }
    inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; }
    inline mint<MOD> & operator -= (mint<MOD> other) { this->value -= other.value; if (this->value <    0) this->value += MOD; return *this; }
    inline mint<MOD> & operator *= (mint<MOD> other) { this->value = (int64_t)this->value * other.value % MOD; if (this->value < 0) this->value += MOD; return *this; }
    mint<MOD> pow(uint64_t k) const {
        mint<MOD> x = *this, y = 1;
        for (; k; k >>= 1) {
            if (k & 1) y *= x;
            x *= x;
        }
        return y;
    }
};


constexpr int MOD = 998244353;
mint<MOD> solve(int n, vector<int> const & a) {
    int sum_a = accumulate(ALL(a), 0);

    vector<mint<MOD> > dp(sum_a + 1);
    dp[0] += 1;
    REP (i, n) {
        REP_R (j, sum_a + 1) if (dp[j].value) {
            dp[j + a[i]] += dp[j];  // c
            dp[j] += dp[j];  // a, b
        }
    }

    vector<mint<MOD> > dp1(sum_a + 1);
    dp1[0] += 1;
    REP (i, n) {
        REP_R (j, sum_a + 1 - a[i]) {
            dp1[j + a[i]] += dp1[j];
        }
    }

    if (sum_a % 2 == 0) {
        return mint<MOD>(3).pow(n) - accumulate(dp.begin() + (sum_a / 2), dp.end(), mint<MOD>()) * 3 + dp1[sum_a / 2] * 3;
    } else {
        return mint<MOD>(3).pow(n) - accumulate(dp.begin() + (sum_a / 2 + 1), dp.end(), mint<MOD>()) * 3;
    }
}

int main() {
    int n; cin >> n;
    vector<int> a(n);
    REP (i, n) {
        cin >> a[i];
    }
    cout << solve(n, a).value << endl;
    return 0;
}

Submission Info

Submission Time
Task D - Three Colors
User kimiyuki
Language C++14 (GCC 5.4.1)
Score 600
Code Size 2336 Byte
Status AC
Exec Time 209 ms
Memory 896 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 27
Set Name Test Cases
Sample s1.txt, s2.txt, s3.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, s1.txt, s2.txt, s3.txt
Case Name Status Exec Time Memory
01.txt AC 110 ms 512 KiB
02.txt AC 115 ms 640 KiB
03.txt AC 119 ms 640 KiB
04.txt AC 115 ms 640 KiB
05.txt AC 6 ms 256 KiB
06.txt AC 5 ms 256 KiB
07.txt AC 5 ms 256 KiB
08.txt AC 5 ms 256 KiB
09.txt AC 2 ms 256 KiB
10.txt AC 2 ms 256 KiB
11.txt AC 205 ms 896 KiB
12.txt AC 209 ms 896 KiB
13.txt AC 2 ms 256 KiB
14.txt AC 52 ms 896 KiB
15.txt AC 52 ms 896 KiB
16.txt AC 53 ms 896 KiB
17.txt AC 2 ms 256 KiB
18.txt AC 2 ms 256 KiB
19.txt AC 2 ms 256 KiB
20.txt AC 2 ms 256 KiB
21.txt AC 1 ms 256 KiB
22.txt AC 1 ms 256 KiB
23.txt AC 1 ms 256 KiB
24.txt AC 1 ms 256 KiB
s1.txt AC 1 ms 256 KiB
s2.txt AC 1 ms 256 KiB
s3.txt AC 1 ms 256 KiB