QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#370971 | #6397. Master of Both III | ricofx# | WA | 1ms | 3692kb | C++17 | 2.4kb | 2024-03-29 20:20:25 | 2024-03-29 20:20:25 |
Judging History
answer
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define fi first
#define se second
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
//std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 998244353;
const int N = 4e6 + 5;
const ll inf = 1e15;
void add(int &x, int y) {
((x += y) >= mod) && (x -= mod);
}
int n;
ll w[25], dis[25][25], dp[(1 << 22) + 5];
ll d[25];
int vis[25];
void prim(int st){
for (int i = 0; i < n; i++) d[i] = inf, vis[i] = 0;
d[0] = 0;
for(int i = 0; i <= n; i++) if ((st >> i) & 1) {
int x = -1;
for(int j = 0; j < n; ++j) if(((st >> j) & 1) && !vis[j] && (x == -1 || d[j] < d[x])) x = j;
if (x == -1) break;
vis[x] = 1;
for(int j = 0; j < n; ++j) if(((st >> j) & 1) && !vis[j]) d[j] = min(d[j], dis[j][x]);
}
ll sum = 0;
for (int i = 0; i < n; i++) {
if ((st >> i) & 1) sum += d[i];
//if (st == 7 && ((st >> i) & 1)) cout << st << ' ' << i << ' ' << d[i] << '\n';
}
dp[st] = sum;
}
void MAIN() {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) dis[i][j] = inf;
}
for (int i = 0; i < n; i++) cin >> w[i];
for (int i = 0; i < n; i++) {
dis[i][i] = 0;
for (int j = 0; j < n; j++) dis[i][(i + j) % n] = min(dis[i][(i + j) % n], w[j]);
}
// for (int k = 0; k < n; k++) {
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
// }
// }
// cerr << dis[1][2] << '\n';
int mask = ((1 << n) - 1);
for (int i = 0; i <= mask; i++) dp[i] = inf;
for (int i = 0; i <= mask; i++) prim(i | 1);
for (int s = mask; s >= 0; s--) {
for (int k = 0; k < n; k++) if (!((s >> k) & 1)) {
//cerr << s << ' ' << (s | (1 << k)) << '\n';
dp[s] = min(dp[s], dp[s | (1 << k)]);
}
//cout << s << ' ' << dp[s] << '\n';
}
ll ans = 0;
for (int i = 1; i <= mask; i++) ans = (ans + dp[i] * (ll)i % mod) % mod;
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int T = 1;
//cin >> T;
while (T--) MAIN();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3584kb
input:
3 2 1 2
output:
45
result:
ok 1 number(s): "45"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3620kb
input:
4 1919810 999999998 999999997 114114514
output:
152175989
result:
ok 1 number(s): "152175989"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
3 842160586 705327547 868243944
output:
78597628
result:
ok 1 number(s): "78597628"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3692kb
input:
5 198327434 147392532 760837755 771109105 676721155
output:
751568230
result:
ok 1 number(s): "751568230"
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 3628kb
input:
10 831766351 33417723 223739726 80131988 348810263 415437931 119999060 799356097 512022962 23197703
output:
458771697
result:
wrong answer 1st numbers differ - expected: '308170104', found: '458771697'