QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#869462 | #247. 州区划分 | lycc | 100 ✓ | 6793ms | 560876kb | C++20 | 3.6kb | 2025-01-25 09:52:36 | 2025-01-25 09:52:37 |
Judging History
answer
// Author: lycc
//
// Problem: P4221 [WC2018] 州区划分
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P4221
// Memory Limit: 1000 MB
// Time Limit: 10000 ms
#include <bits/stdc++.h>
// #define int long long
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define pb emplace_back
#define For(i, x, y) for (int i = (x); i <= (y); i ++)
#define rep(i, x, y) for (int i = (x); i >= (y); i --)
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define sz(v) (int)((v).size())
#define ull unsigned long long
#define ls (p << 1)
#define rs (p << 1 | 1)
#define mp make_pair
#define i128 __int128
#define db long double
#define vi vector< int >
#define mem(v, x) memset(v, x, sizeof(v))
#define A3 array< int, 3 >
#define A4 array< int, 4 >
#define vpii vector< pair< int, int > >
using namespace std;
mt19937_64 rnd(time(0));
template< typename T > void cmin(T &x, T y) { return x = min(x, y), void(); }
template< typename T > void cmax(T &x, T y) { return x = max(x, y), void(); }
int ksm(int x, int y, int p) {
int v = 1; x %= p;
while (y) v = 1ll * v * ((y & 1) ? x : 1) % p, x = 1ll * x * x % p, y >>= 1;
return (v % p + p) % p;
}
void file() {
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
return;
}
bool MemoryST;
const int N = 21;
const int mod = 998244353;
const long long INF = 1e18;
const int base = 13131;
using ll = long long;
int n, m, _, a[N], siz[1 << N], u[1000], v[1000], fa[N], val[1 << N], ans[1 << N];
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
vi pos[N + 1];
int todo[N + 1][1 << N], Val[N + 1][1 << N], Ans[N + 1][1 << N], deg[N];
void Main() {
cin >> n >> m >> _;
For (i, 1, m) cin >> u[i] >> v[i], u[i] --, v[i] --;
For (i, 0, n - 1) cin >> a[i];
For (s, 1, (1 << n) - 1) {
int x = __lg(s & (-s));
siz[s] = siz[s ^ (1 << x)] + a[x];
For (i, 0, n - 1) deg[i] = 0, fa[i] = i;
For (i, 1, m) {
if (((s >> u[i]) & 1) && ((s >> v[i]) & 1)) {
deg[u[i]] ++; deg[v[i]] ++;
fa[find(u[i])] = find(v[i]);
}
}
bool flag = 0; int cnt = 0;
For (i, 0, n - 1) {
if ((s >> i) & 1) {
if (find(i) == i) cnt ++;
flag |= (deg[i] & 1);
}
}
if (flag || cnt >= 2) {
if (!_) val[s] = 1;
if (_ == 1) val[s] = siz[s];
if (_ == 2) val[s] = siz[s] * siz[s];
ans[s] = val[s];
}
pos[__builtin_popcount(s)].pb(s);
Val[__builtin_popcount(s)][s] = val[s];
}
For (S, 1, n) {
For (i, 0, n - 1) {
For (s, 0, (1 << n) - 1) {
if ((s >> i) & 1) {
(Val[S][s] += Val[S][s ^ (1 << i)]) %= mod;
}
}
}
}
For (S, 1, n) {
For (i, 0, n - 1) {
For (s, 0, (1 << n) - 1) {
if ((s >> i) & 1) {
(todo[S][s] += mod - todo[S][s ^ (1 << i)]) %= mod;
}
}
}
for (int s : pos[S]) {
(ans[s] += todo[S][s]) %= mod;
ans[s] = 1ll * ans[s] * ksm((!_ ? 1 : (_ == 1 ? siz[s] : siz[s] * siz[s])), mod - 2, mod) % mod;
Ans[S][s] = ans[s];
}
For (i, 0, n - 1) {
For (s, 0, (1 << n) - 1) {
if ((s >> i) & 1) {
(Ans[S][s] += Ans[S][s ^ (1 << i)]) %= mod;
}
}
}
For (T, 1, n - S) {
For (s, 0, (1 << n) - 1) {
(todo[S + T][s] += 1ll * Ans[S][s] * Val[T][s] % mod) %= mod;
}
}
}
cout << ans[(1 << n) - 1];
return;
}
bool MemoryED;
signed main() {
// file();
ios :: sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cerr << fixed << setprecision(6) << (&MemoryST - &MemoryED) / 1048576.0 << "MB\n";
int TESTCNT = 1;
// cin >> TESTCNT;
while (TESTCNT --) Main();
cerr << endl << 1e3 * clock() / CLOCKS_PER_SEC << "ms";
return 0;
}
详细
Subtask #1:
score: 26
Accepted
Test #1:
score: 26
Accepted
time: 1ms
memory: 26756kb
input:
5 4 2 3 1 4 1 5 1 4 3 94 45 77 43 3
output:
652834711
result:
ok 1 number(s): "652834711"
Test #2:
score: 26
Accepted
time: 0ms
memory: 47180kb
input:
10 34 2 1 2 5 1 6 1 1 8 1 9 1 10 3 2 2 6 7 2 2 8 9 2 10 2 3 4 3 5 3 6 3 8 9 3 10 3 4 5 6 4 8 4 9 4 10 4 5 8 9 5 10 5 7 6 6 8 6 10 8 7 9 7 10 7 8 9 10 9 89 50 53 95 71 52 83 54 54 12
output:
748246143
result:
ok 1 number(s): "748246143"
Test #3:
score: 26
Accepted
time: 48ms
memory: 70008kb
input:
15 81 0 1 4 6 1 1 7 1 8 1 9 10 1 1 13 14 1 1 15 2 3 4 2 5 2 6 2 2 7 2 8 2 11 12 2 13 2 14 2 2 15 4 3 5 3 3 7 3 8 3 9 10 3 11 3 3 13 14 3 15 3 4 5 6 4 4 7 4 8 9 4 11 4 4 12 13 4 14 4 15 4 6 5 8 5 5 9 5 10 5 11 5 12 6 7 8 6 9 6 10 6 11 6 6 12 6 14 8 7 7 9 7 10 7 11 7 12 13 7 7 14 7 15 8 10 8 11 8 12 1...
output:
318194083
result:
ok 1 number(s): "318194083"
Test #4:
score: 26
Accepted
time: 49ms
memory: 70008kb
input:
15 70 1 1 2 3 1 4 1 1 5 6 1 8 1 1 9 1 12 1 13 1 15 2 3 2 4 2 5 2 6 2 7 2 9 2 11 12 2 13 2 2 14 3 5 6 3 3 8 9 3 11 3 12 3 3 13 4 5 8 4 10 4 4 12 13 4 6 5 7 5 5 8 9 5 11 5 5 13 14 5 6 7 6 8 6 9 10 6 6 12 6 13 6 14 15 6 7 8 7 9 7 10 12 7 7 13 14 7 9 8 10 8 12 8 14 8 15 8 9 11 13 9 14 9 9 15 10 11 13 10...
output:
92331988
result:
ok 1 number(s): "92331988"
Test #5:
score: 26
Accepted
time: 44ms
memory: 129604kb
input:
15 67 2 1 2 1 3 5 1 1 6 1 8 11 1 12 1 1 13 15 1 2 5 2 6 8 2 2 10 11 2 2 12 13 2 2 14 4 3 9 3 3 10 11 3 15 3 5 4 7 4 8 4 10 4 11 4 4 12 13 4 14 4 5 6 5 8 9 5 10 5 12 5 13 5 14 5 6 7 6 8 6 9 6 10 6 11 6 14 7 8 10 7 11 7 12 7 7 15 9 8 8 10 13 8 8 14 10 9 9 11 9 12 13 9 14 9 15 9 13 10 15 10 12 11 14 11...
output:
237027263
result:
ok 1 number(s): "237027263"
Subtask #2:
score: 29
Accepted
Dependency #1:
100%
Accepted
Test #6:
score: 29
Accepted
time: 5389ms
memory: 559180kb
input:
21 146 0 1 6 7 1 1 8 1 9 10 1 1 11 1 14 1 15 1 16 18 1 1 19 1 21 2 3 4 2 5 2 2 7 2 8 2 9 2 12 13 2 2 15 2 17 2 18 2 19 2 20 2 21 4 3 5 3 3 6 3 7 8 3 3 10 12 3 3 13 3 15 3 16 3 18 20 3 3 21 4 5 4 6 4 8 9 4 12 4 13 4 17 4 18 4 20 4 4 21 5 6 5 7 5 8 5 9 12 5 13 5 5 14 5 15 5 17 18 5 5 19 20 5 5 21 6 8 ...
output:
739452507
result:
ok 1 number(s): "739452507"
Test #7:
score: 29
Accepted
time: 6793ms
memory: 559864kb
input:
21 209 0 15 17 18 21 8 20 7 20 11 17 11 13 12 18 2 13 19 20 4 8 6 13 3 20 5 10 4 20 1 5 7 13 5 7 4 10 4 9 6 7 4 18 8 17 1 11 3 13 11 19 2 15 3 8 14 19 5 16 2 10 13 21 4 21 7 11 5 20 3 7 6 19 9 17 13 16 5 12 1 6 3 15 6 16 2 5 4 5 8 16 1 3 1 16 1 14 2 9 7 16 10 16 2 18 13 15 7 10 11 12 10 20 6 10 1 20...
output:
835537739
result:
ok 1 number(s): "835537739"
Test #8:
score: 29
Accepted
time: 5324ms
memory: 559972kb
input:
21 146 0 2 1 1 4 5 1 1 7 1 8 1 9 13 1 14 1 15 1 16 1 1 17 18 1 1 19 2 3 2 4 5 2 2 6 7 2 2 8 9 2 2 10 2 11 2 12 13 2 15 2 2 18 19 2 2 20 21 2 5 3 3 6 7 3 3 10 13 3 14 3 15 3 3 16 3 17 3 20 21 3 4 7 4 9 10 4 4 11 12 4 4 13 4 16 4 17 18 4 4 20 21 4 5 7 8 5 5 9 13 5 5 14 15 5 5 16 5 17 5 18 5 20 21 5 7 ...
output:
873127243
result:
ok 1 number(s): "873127243"
Test #9:
score: 29
Accepted
time: 6778ms
memory: 559176kb
input:
21 205 0 4 10 8 15 8 16 6 21 1 5 9 15 1 7 12 18 2 19 5 10 7 18 8 10 11 20 3 11 18 20 4 20 15 17 15 19 3 5 2 18 19 20 8 19 1 3 8 13 7 21 10 17 16 17 5 13 15 21 5 6 9 20 1 16 2 13 16 20 10 19 10 11 1 11 2 21 13 20 2 12 14 19 2 3 3 9 4 16 4 14 9 13 10 18 4 11 16 21 4 9 7 14 3 19 14 18 7 20 3 10 15 16 5...
output:
38084232
result:
ok 1 number(s): "38084232"
Subtask #3:
score: 23
Accepted
Dependency #1:
100%
Accepted
Test #10:
score: 23
Accepted
time: 5514ms
memory: 560108kb
input:
21 159 1 2 1 3 1 5 1 7 1 1 9 11 1 12 1 13 1 17 1 18 1 1 19 20 1 2 3 2 4 5 2 7 2 2 8 2 10 12 2 13 2 2 14 15 2 2 18 19 2 20 2 21 2 3 4 5 3 6 3 7 3 3 8 9 3 3 10 3 11 3 12 3 13 14 3 3 15 16 3 18 3 3 19 3 21 5 4 6 4 4 7 8 4 9 4 4 11 4 14 4 15 16 4 17 4 19 4 21 4 5 6 5 7 5 9 10 5 11 5 12 5 5 14 15 5 5 17 ...
output:
211589144
result:
ok 1 number(s): "211589144"
Test #11:
score: 23
Accepted
time: 6779ms
memory: 560488kb
input:
21 208 1 12 15 5 13 15 18 2 11 12 19 3 11 11 15 9 11 5 14 11 13 7 12 1 5 3 5 8 13 14 18 9 21 2 4 1 12 3 7 9 10 12 20 5 15 10 20 15 19 16 18 18 20 13 16 3 9 16 20 6 13 11 16 15 17 1 20 4 6 11 21 1 3 1 15 8 21 13 19 7 21 17 19 4 5 10 11 11 17 3 20 7 10 12 18 7 9 5 16 11 12 3 12 14 19 8 20 12 21 7 13 5...
output:
56778317
result:
ok 1 number(s): "56778317"
Test #12:
score: 23
Accepted
time: 5386ms
memory: 559472kb
input:
21 137 1 3 1 1 4 5 1 7 1 8 1 1 11 1 12 13 1 16 1 1 17 1 19 20 1 21 1 2 3 2 4 2 5 7 2 8 2 9 2 10 2 2 11 12 2 2 13 17 2 18 2 2 19 3 4 3 6 3 13 14 3 15 3 17 3 3 18 19 3 21 3 4 5 6 4 7 4 4 8 9 4 10 4 4 11 4 12 4 14 15 4 16 4 4 19 4 20 6 5 7 5 8 5 9 5 10 5 5 11 12 5 13 5 5 15 5 17 5 18 5 19 21 5 7 6 6 10...
output:
490355723
result:
ok 1 number(s): "490355723"
Test #13:
score: 23
Accepted
time: 6484ms
memory: 559080kb
input:
21 205 1 16 18 6 14 10 14 1 2 12 14 4 8 12 16 1 4 2 15 14 16 4 11 15 17 4 7 3 16 11 17 15 21 13 20 2 21 6 19 13 14 20 21 17 19 9 19 10 20 7 9 11 15 7 13 8 14 4 15 8 13 5 14 3 11 8 9 10 21 3 10 14 18 5 16 5 20 17 18 3 17 1 8 5 10 13 15 3 9 7 21 14 20 1 20 8 10 1 13 5 17 13 16 15 18 4 10 18 21 1 5 18 ...
output:
748937625
result:
ok 1 number(s): "748937625"
Subtask #4:
score: 22
Accepted
Dependency #1:
100%
Accepted
Test #14:
score: 22
Accepted
time: 6656ms
memory: 559740kb
input:
21 207 2 2 6 9 20 2 11 11 19 6 9 7 20 12 19 12 18 15 18 1 4 5 8 20 21 17 18 5 9 12 14 4 14 15 17 18 20 3 18 11 13 4 17 12 15 2 19 10 20 11 17 5 7 3 4 14 21 9 21 13 17 9 13 13 20 3 17 2 17 2 8 17 20 1 17 9 19 2 12 4 9 14 15 14 18 6 15 16 20 3 8 12 17 5 10 9 10 8 15 4 16 2 21 5 13 9 15 1 21 2 10 8 11 ...
output:
317043733
result:
ok 1 number(s): "317043733"
Test #15:
score: 22
Accepted
time: 6734ms
memory: 560876kb
input:
21 205 2 6 14 7 19 2 3 6 7 5 18 1 10 3 19 16 18 4 15 10 16 13 21 9 11 12 19 13 19 1 18 15 16 7 11 2 11 9 19 4 13 14 21 4 17 6 8 11 18 12 14 15 19 11 14 2 16 4 11 10 19 10 13 10 21 5 6 2 13 4 21 4 5 9 10 7 14 1 15 4 16 8 11 5 19 5 21 13 16 1 6 4 20 16 17 5 11 13 14 5 9 3 11 11 20 6 15 8 15 6 11 5 10 ...
output:
364004893
result:
ok 1 number(s): "364004893"
Extra Test:
score: 0
Extra Test Passed