QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#548444 | #6397. Master of Both III | pandapythoner# | RE | 3ms | 3836kb | C++23 | 1.6kb | 2024-09-05 18:35:46 | 2024-09-05 18:35:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define len(a) int((a).size())
#define all(a) begin(a), end(a)
#define rep(i, n) for (int i = 0; i < (n); i++)
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
const int N = (1 << n);
vector<int> cost(n);
for (auto& x : cost) {
cin >> x;
}
vector<pair<int, int>> ord(N);
for (int i = 0; i < N; i++) {
ord[i] = { __builtin_popcount(i), i };
}
sort(all(ord));
constexpr ll INF = 1e18;
vector<ll> dp(N, INF);
dp[0] = 0;
dp[1 << 0] = 0;
for (auto [_, mask] : ord) {
for (int x = 1; x < n; x++) {
int shift = (mask << x);
int rem = (shift >> n);
int nmask = (mask | rem | (shift & (N - 1)));
dp[nmask] = min(dp[nmask], dp[mask] + cost[x]);
}
}
for (int mask = N - 1; mask >= 0; mask--) {
for (int i = 0; i < n; i++) {
dp[mask] = min(dp[mask], dp[mask | (1 << i)]);
}
}
constexpr int MOD = 998244353;
ll ans = 0;
for (int mask = 1; mask < N; mask++) {
int need = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (mask >> i & 1) {
if (i == 0) {
need ^= 1;
} else {
need ^= (1 << (n - i));
}
sum += (1 << i);
}
}
ans += 1ll * sum * dp[need] % MOD;
if (ans >= MOD) {
ans -= MOD;
}
}
cout << ans % MOD << '\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3836kb
input:
3 2 1 2
output:
45
result:
ok 1 number(s): "45"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3552kb
input:
4 1919810 999999998 999999997 114114514
output:
152175989
result:
ok 1 number(s): "152175989"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
3 842160586 705327547 868243944
output:
78597628
result:
ok 1 number(s): "78597628"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3616kb
input:
5 198327434 147392532 760837755 771109105 676721155
output:
751568230
result:
ok 1 number(s): "751568230"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3632kb
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: 0ms
memory: 3620kb
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: 3ms
memory: 3648kb
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: -100
Runtime Error
input:
20 627365465 726842612 947302944 649244156 293375951 318148820 237155023 981487641 688151803 844901013 430309799 733877736 520864483 720366437 28746495 143052089 306590290 18586578 662663479 375430013