QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#178298#6397. Master of Both IIIucup-team1600#WA 187ms19368kbC++205.9kb2023-09-13 20:49:582023-09-13 20:49:58

Judging History

你现在查看的是最新测评结果

  • [2023-09-13 20:49:58]
  • 评测
  • 测评结果:WA
  • 用时:187ms
  • 内存:19368kb
  • [2023-09-13 20:49:58]
  • 提交

answer

//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")
#ifdef LOCAL
#include <iostream>
#include <cmath>
#include <algorithm>
#include <stdio.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <cstdlib>
#include <vector>
#include <bitset>
#include <map>
#include <queue>
#include <ctime>
#include <stack>
#include <set>
#include <list>
#include <random>
#include <deque>
#include <functional>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <complex>
#include <numeric>
#include <cassert>
#include <array>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <thread>
#else
#include <bits/stdc++.h>
#endif

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;

template<typename T>
inline bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template<typename T>
inline bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef LOCAL
#define D for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define D while (false)
#define LOG(...)
#endif // LOCAL

const int max_n = -1, inf = 1000111222;

const int MOD = 998244353;
struct mint {
    int val;

    mint () : val(0) {}
    mint (int x) {
        if (-MOD <= x && x < MOD) {
            val = x;
        }
        else {
            val = x % MOD;
        }
        if (val < 0) val += MOD;
    }

    mint (ll x) {
        if (-MOD <= x && x < MOD) {
            val = x;
        }
        else {
            val = x % MOD;
        }
        if (val < 0) val += MOD;
    }

    mint (const mint& x) : val(x.val) {}

    constexpr mint& operator = (const mint& x) {
        val = x.val;
        return *this;
    }

    inline mint& operator += (const mint &x) {
        val += x.val;
        if (val >= MOD) {
            val -= MOD;
        }
        return *this;
    }

    inline mint& operator -= (const mint &x) {
        val -= x.val;
        if (val < 0) {
            val += MOD;
        }
        return *this;
    }

    inline mint operator - () const {
        mint tmp(*this);
        if (tmp.val) tmp.val = MOD - tmp.val;
        return tmp;
    }

    inline mint operator + (const mint &x) const {
        return mint(*this) += x;
    }

    inline mint operator - (const mint &x) const {
        return mint(*this) -= x;
    }

    inline mint& operator *= (const mint &x) {
        val = ((ll)val * x.val) % MOD;
        return *this;
    }

    inline mint operator * (const mint &x) const {
        return mint(*this) *= x;
    }

    inline mint binpow (int n) const {
        mint res = 1, tmp = *this;
        for (; n; n >>= 1) {
            if (n & 1) {
                res *= tmp;
            }
            tmp *= tmp;
        }
        return res;
    }

    inline mint inverse () const {
        return binpow(MOD - 2);
    }

    inline mint& operator /= (const mint &x) {
        return *this *= x.inverse();
    }

    inline mint operator / (const mint &x) {
        return mint(*this) *= x.inverse();
    }


    friend ostream& operator << (ostream &os, const mint &x) {
        os << x.val;
        return os;
    }

    friend istream& operator >> (istream &is, mint &x) {
        is >> x.val;
        return is;
    }

    inline bool operator == (const mint &x) const {
        return val == x.val;
    }

    inline bool operator != (const mint &x) const {
        return val != x.val;
    }

    inline bool operator < (const mint &x) const {
        return val < x.val;
    }

    inline bool operator > (const mint &x) const {
        return val > x.val;
    }

    friend string to_string (const mint &x) {
        return to_string(x.val);
    }

};

vector <mint> f = {1}, fi = {1};

inline mint fact (int n) {
    f.reserve(n + 1);
    while (len(f) <= n) {
        f.emplace_back(f.back() * len(f));
    }
    return f[n];
}

inline mint inv_fact (int n) { /// think
    if (len(fi) <= n) {
        fi.resize(n + 1, 0);
        mint val = mint(1) / fact(n);
        for (int i = n; fi[i] == 0; i--) {
            fi[i] = val;
            val *= i;
        }
    }
    return fi[n];
}

inline mint A (int n, int k) {
    if (k < 0 || k > n) return 0;
    return fact(n) * inv_fact(n - k);
}

inline mint C (int n, int k) {
    if (k < 0 || k > n) return 0;
    return A(n, k) * inv_fact(k);
}

const ll linf = inf * 1ll * inf;


int main() {
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;
    vector <int> a(n);
    for (auto &i : a) {
        cin >> i;
    }
    reverse(a.begin() + 1, a.end());
    vector <ll> dp(1 << n, linf);
    dp[1] = 0;
    for (int i = 1; i < (1 << n); i++) {
        for (int j = 1; j < n; j++) {
            int to = (i << j) | i;
            to |= to >> n;
            to &= (1 << n) - 1;
            umin(dp[to], dp[i] + a[j]);
        }
    }
    for (int mask = (1 << n) - 1; mask >= 0; mask--) {
        for (int i = 0; i < n; i++) {
            if (!(mask & (1 << i))) {
                umin(dp[mask], dp[mask | (1 << i)]);
            }
        }
    }
    mint ans = 0;
    for (int i = 1; i < (1 << n); i++) {
        ans += mint(dp[i]) * i;
    }
    cout << ans << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3468kb

input:

3
2 1 2

output:

45

result:

ok 1 number(s): "45"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3500kb

input:

4
1919810 999999998 999999997 114114514

output:

152175989

result:

ok 1 number(s): "152175989"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3504kb

input:

3
842160586 705327547 868243944

output:

78597628

result:

ok 1 number(s): "78597628"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3436kb

input:

5
198327434 147392532 760837755 771109105 676721155

output:

751568230

result:

ok 1 number(s): "751568230"

Test #5:

score: 0
Accepted
time: 1ms
memory: 3440kb

input:

10
831766351 33417723 223739726 80131988 348810263 415437931 119999060 799356097 512022962 23197703

output:

308170104

result:

ok 1 number(s): "308170104"

Test #6:

score: 0
Accepted
time: 1ms
memory: 3540kb

input:

12
892965903 35291219 261557729 131390943 457251874 944586973 724767219 190756777 658923976 587806068 793999675 378390067

output:

964920873

result:

ok 1 number(s): "964920873"

Test #7:

score: 0
Accepted
time: 1ms
memory: 3456kb

input:

14
249132751 477356204 594343028 32906794 270726189 883801423 329535378 877124753 100792287 152414432 142520554 196476850 924736849 383197276

output:

796031217

result:

ok 1 number(s): "796031217"

Test #8:

score: 0
Accepted
time: 86ms
memory: 11300kb

input:

20
627365465 726842612 947302944 649244156 293375951 318148820 237155023 981487641 688151803 844901013 430309799 733877736 520864483 720366437 28746495 143052089 306590290 18586578 662663479 375430013

output:

179404754

result:

ok 1 number(s): "179404754"

Test #9:

score: -100
Wrong Answer
time: 187ms
memory: 19368kb

input:

21
805448889 595358753 391340394 525130530 272725205 157594893 261894302 29704333 909085958 127205196 104570238 495437480 458664573 599968678 690908307 220500006 735062204 172834136 241126905 814694254 294923292

output:

377942368

result:

wrong answer 1st numbers differ - expected: '260115885', found: '377942368'