QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#135501 | #6394. Turn on the Light | UrgantTeam# | RE | 0ms | 0kb | C++23 | 1.9kb | 2023-08-05 16:35:37 | 2023-08-05 16:35:40 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long double ld;
typedef long long ll;
const int MOD = 998244353;
const ll INF = (ll) 1e18;
int cost[25], subsum[(1 << 22) + 7];
ll dp[(1 << 22) + 7];
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> cost[i - 1];
subsum[0] = 1;
for (int mask = 1; mask < (1 << n); mask++)
{
int pos = -1;
for (int bit = 0; bit < n; bit++)
if ((mask >> bit) & 1) {pos = bit; break;}
int mask1 = subsum[mask ^ (1 << pos)];
int mask2 = 0;
for (int bit = 0; bit < n; bit++)
{
if ((mask1 >> bit) & 1)
{
int need = (bit + pos) % n;
mask2 += (1 << need);
}
}
subsum[mask] = (mask1 | mask2);
}
for (int mask = 0; mask < (1 << n); mask++) dp[mask] = INF;
for (int mask = 0; mask < (1 << n); mask++)
{
ll sum = 0;
for (int bit = 0; bit < n; bit++)
if ((mask >> bit) & 1) sum += cost[bit];
dp[subsum[mask]] = min(dp[subsum[mask]], sum);
}
for (int mask = (1 << n) - 1; mask >= 0; mask--)
{
for (int bit = 0; bit < n; bit++)
if (((mask >> bit) & 1) == 0) dp[mask] = min(dp[mask], dp[mask ^ (1 << bit)]);
}
for (int i = 0; i < (1 << n); i++) cout << dp[i] << ' ';
cout << '\n';
int ans = 0;
for (int mask = 1; mask < (1 << n); mask++)
{
int revmask = 0;
for (int bit = 0; bit < n; bit++)
if (bit == 0) revmask += (mask & 1);
else revmask += (((mask >> bit) & 1) << (n - bit));
cout << mask << ' ' << revmask << '\n';
int s = dp[revmask] % MOD;
int t = 0;
for (int bit = 0; bit < n; bit++)
if ((mask >> bit) & 1) t = (t + (1 << bit)) % MOD;
ans = (ans + (ll) s * t) % MOD;
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls