QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#71827 | #2579. 开关 | He_Ren | 40 | 2ms | 3920kb | C++23 | 3.3kb | 2023-01-12 01:27:17 | 2023-01-12 01:31:37 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXN = 1e2 + 5;
const int MAXM = (1 << 8) + 5;
const int mod = 998244353;
inline void add_mod(int &a, int b) {
a += b;
if (a >= mod)
a -= mod;
}
inline ll pw(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
#define bbit(i) (1<<(i))
#define bdig(x,i) (((x)>>(i))&1)
inline void fwt(int a[], int n) {
for (int k = 1; k < n; k <<= 1)
for (int i = 0; i < n; i += k << 1)
for (int j = i; j < i + k; ++j) {
int tmp = a[j + k];
add_mod(a[j + k] = a[j], mod - tmp);
add_mod(a[j], tmp);
}
}
int s[MAXN], p[MAXN];
int main(void) {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d", &s[i]);
for (int i = 1; i <= n; ++i)
scanf("%d", &p[i]);
{
int sump = accumulate(p + 1, p + n + 1, 0);
sump = pw(sump, mod - 2);
for (int i = 1; i <= n; ++i)
p[i] = (ll)p[i] * sump % mod;
}
static int fp[MAXM];
for (int i = 1; i <= n; ++i)
fp[bbit(i - 1)] = p[i];
fwt(fp, 1 << n);
// for(int i=0; i<(1<<n); ++i)
// printf("fp: %d\n",fp[i]);
static int fbeg[MAXM];
int begmask = 0;
for (int i = 1; i <= n; ++i)
if (s[i])
begmask |= bbit(i - 1);
fbeg[begmask] = 1;
fwt(fbeg, 1 << n);
// for(int i=0; i<(1<<n); ++i)
// printf("fbeg: %d\n",fbeg[i]);
int m = (1 << n) + 1;
static int a[MAXM][MAXM];
auto chk = [&](int b[], int i) {
if (!a[i][i] || !b[i])
return;
ll t = mod - b[i];
for (int j = i; j <= m; ++j)
b[j] = (b[j] + t * a[i][j]) % mod;
};
auto insert = [&](int b[]) {
// printf("insert: ");
// for(int i=0; i<=m; ++i) printf("%d ",b[i]);
// printf("\n");
for (int i = 0; i < m; ++i)
if (b[i]) {
if (a[i][i]) {
chk(b, i);
continue;
}
ll t = pw(b[i], mod - 2);
for (int j = i; j <= m; ++j)
b[j] = b[j] * t % mod;
memcpy(a[i], b, (m + 1) << 2);
for (int j = i + 1; j < m; ++j)
chk(a[i], j);
for (int j = 0; j < i; ++j)
chk(a[j], i);
return;
}
// printf("fail\n");
};
static int b[MAXM];
b[1 << n] = (mod - (1 << n) % mod) % mod;
for (int mask = 0; mask < (1 << n); ++mask)
b[mask] = 1;
insert(b);
for (int mask = 0; mask < (1 << n); ++mask) {
memset(b, 0, sizeof(b));
b[mask] = (1 - fp[mask] + mod) % mod;
b[1 << n] = fp[mask];
b[m] = fbeg[mask];
insert(b);
}
// for(int i=0; i<m; ++i, printf("\n"))
// for(int j=0; j<=m; ++j)
// printf("%d ",a[i][j]);
// printf("\n");
int ans = (a[0][m] - a[1 << n][m] + mod) % mod;
printf("%d", ans);
return 0;
}
详细
Test #1:
score: 10
Accepted
time: 2ms
memory: 3812kb
input:
2 1 1 23723 26273
output:
877668810
result:
ok single line: '877668810'
Test #2:
score: 10
Accepted
time: 2ms
memory: 3572kb
input:
2 1 0 18216 31771
output:
489970511
result:
ok single line: '489970511'
Test #3:
score: 10
Accepted
time: 1ms
memory: 3920kb
input:
8 1 1 0 1 1 0 1 0 1270 10205 3901 3809 8931 2530 11580 7773
output:
193550909
result:
ok single line: '193550909'
Test #4:
score: 10
Accepted
time: 2ms
memory: 3904kb
input:
8 0 1 0 0 0 1 0 0 5929 4817 1939 5783 7039 5771 9785 8934
output:
662840715
result:
ok single line: '662840715'
Test #5:
score: 0
Runtime Error
input:
100 0 1 0 1 1 1 0 1 0 1 1 1 1 1 1 0 1 0 1 1 0 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1 0 0 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 0 1 1 0 1 1 0 1 0 0 1 1 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 1 1 0 0 0 1 1 0 0 24 28 18 19 22 36 35 24 7 19 26 2 31 20 33 11 20 37 11 36 9 28 16 20 46 28 5 20 40 27 13 15 28 7...
output:
result:
Test #9:
score: 0
Runtime Error
input:
100 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 0 1 1 0 1 0 1 0 1 0 1 1 1 1 0 0 0 1 0 1 1 0 0 1 0 0 1 0 1 1 0 1 0 0 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 1 1 1 1 1 0 0 1 1 0 0 0 1 1 1 1 0 0 0 0 1 1 0 1 0 1 0 21 11 16 21 30 35 10 3 15 33 39 7 16 31 24 27 4 15 38 16 6 7 18 11 8 31 8 17 12 20 27 9 44 9 31 ...
output:
result:
Test #10:
score: 0
Runtime Error
input:
100 1 0 0 1 0 1 0 0 1 1 1 0 0 1 0 0 0 1 1 1 1 0 1 1 1 1 0 1 0 0 1 0 1 1 1 0 0 0 1 1 1 0 1 1 1 1 0 1 0 1 0 1 1 1 0 1 1 1 1 1 0 1 1 0 1 0 1 0 0 0 0 1 1 1 0 0 1 0 1 1 1 1 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1 1 1 1 840 587 741 540 310 84 284 345 368 251 545 676 754 274 266 97 858 486 108 727 373 283 722 539 29...