QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#378998 | #6397. Master of Both III | plutos | WA | 0ms | 3580kb | C++17 | 4.9kb | 2024-04-06 15:42:46 | 2024-04-06 15:42:46 |
Judging History
answer
#include<bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
using ll = long long;
template <class T> constexpr auto NL(T) -> T {return std::numeric_limits<T>::max();}
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
template<class T>
constexpr T power(T a, ll b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
template<ll P>
struct MInt {
ll x;
constexpr MInt() : x{} {}
constexpr MInt(ll x) : x{norm(x % P)} {}
constexpr ll norm(ll x) const {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
constexpr ll val() const {
return x;
}
explicit constexpr operator ll() const {
return x;
}
constexpr MInt operator-() const {
MInt res;
res.x = norm(P - x);
return res;
}
constexpr MInt inv() const {
assert(x != 0);
return power(*this, P - 2);
}
constexpr MInt &operator*=(MInt rhs) {
x = 1LL * x * rhs.x % P;
return *this;
}
constexpr MInt &operator+=(MInt rhs) {
x = norm(x + rhs.x);
return *this;
}
constexpr MInt &operator-=(MInt rhs) {
x = norm(x - rhs.x);
return *this;
}
constexpr MInt &operator/=(MInt rhs) {
return *this *= rhs.inv();
}
friend constexpr MInt operator*(MInt lhs, MInt rhs) {
MInt res = lhs;
res *= rhs;
return res;
}
friend constexpr MInt operator+(MInt lhs, MInt rhs) {
MInt res = lhs;
res += rhs;
return res;
}
friend constexpr MInt operator-(MInt lhs, MInt rhs) {
MInt res = lhs;
res -= rhs;
return res;
}
friend constexpr MInt operator/(MInt lhs, MInt rhs) {
MInt res = lhs;
res /= rhs;
return res;
}
friend constexpr std::istream &operator>>(std::istream &is, MInt &a) {
ll v;
is >> v;
a = MInt(v);
return is;
}
friend constexpr std::ostream &operator<<(std::ostream &os, const MInt &a) {
return os << a.val();
}
friend constexpr bool operator==(MInt lhs, MInt rhs) {
return lhs.val() == rhs.val();
}
friend constexpr bool operator!=(MInt lhs, MInt rhs) {
return lhs.val() != rhs.val();
}
};
template<ll V, ll P>
constexpr MInt<P> CInv = MInt<P>(V).inv();
constexpr ll P = 998244353;
// constexpr ll P = 1e9+7;
using Z = MInt<P>;
struct Comb {
ll n;
std::vector<Z> _fac;
std::vector<Z> _invfac;
std::vector<Z> _inv;
Comb() : n{0}, _fac{1}, _invfac{1}, _inv{0} {}
Comb(ll n) : Comb() {
init(n);
}
void init(ll m) {
if (m <= n) return;
_fac.resize(m + 1);
_invfac.resize(m + 1);
_inv.resize(m + 1);
for (ll i = n + 1; i <= m; i++) {
_fac[i] = _fac[i - 1] * i;
}
_invfac[m] = _fac[m].inv();
for (ll i = m; i > n; i--) {
_invfac[i - 1] = _invfac[i] * i;
_inv[i] = _invfac[i] * _fac[i - 1];
}
n = m;
}
Z fac(ll m) {
if (m > n) init(2 * m);
return _fac[m];
}
Z invfac(ll m) {
if (m > n) init(2 * m);
return _invfac[m];
}
Z inv(ll m) {
if (m > n) init(2 * m);
return _inv[m];
}
Z binom(ll n, ll m) {
if (n < m || m < 0) return 0;
return fac(n) * invfac(m) * invfac(n - m);
}
} comb;
ll costp[1 << 22];
Z two[30];
void solve(void) {
ll n;
cin >> n;
two[0] = 1;
for (ll i = 1; i <= 26; i++) two[i] = two[i - 1] * 2;
std::vector<ll> cost(n);
ll pp = (1 << n) - 1;
for (ll i = 0; i < n; i++)cin >> cost[i];
for (ll j = 2; j < (1 << n); j++) {
costp[j] = NL(1LL) / 2;
}
for (ll j = 0; j < (1 << n); j++) {
for (ll i = 0; i < n - 1; i++) {
ll now = (j << (i + 1));
now |= ((now >> (n)));
now |= 1;
now |= (1 << (i + 1));
now &= pp;
// get(now);
// cout<<"\n";
costp[now] = min(costp[now], costp[j] + cost[i + 1]);
}
}
Z ans = 0;
for (ll i = 1; i < (1 << (n)); i++) {
Z now = 0;
ll te = 0;
for (ll j = 0; j < n; j++) {
if (i >> j & 1) {
now += two[j];
if (j == 0)continue;
te |= (1LL << (n - j));
}
}
te |= 1;
ans = ans + now * costp[te];
}
cout << ans;
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
ll t = 1;
// cin>>t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
input:
3 2 1 2
output:
45
result:
ok 1 number(s): "45"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3540kb
input:
4 1919810 999999998 999999997 114114514
output:
872788327
result:
wrong answer 1st numbers differ - expected: '152175989', found: '872788327'