QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#187640 | #3849. Cactus cutting | open your brain (Zhi Zhang, Yanru Guan, Jianfeng Zhu) | AC ✓ | 3116ms | 20496kb | C++17 | 4.6kb | 2023-09-24 19:31:50 | 2023-09-24 19:31:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 100005, mod = 1e6 + 3;
int n, m;
int dfn[N], low[N], tot;
int st[N], top;
int fac[N], ifac[N], val[N];
pair<int, int> g[N];
vector<int> to[N];
vector<vector<int>> son[N];
int power(int a, int n) {
int tp = 1;
while (n) {
if (n & 1) {
tp = 1ll * tp * a % mod;
}
a = 1ll * a * a % mod, n >>= 1;
}
return tp;
}
void dfs1(int i, int fa) {
low[i] = dfn[i] = ++tot;
st[++top] = i;
for (int j : to[i]) {
if (j == fa) {
continue;
}
if (dfn[j]) {
low[i] = min(low[i], dfn[j]);
} else {
dfs1(j, i);
low[i] = min(low[i], low[j]);
if (low[j] < dfn[i]) {
continue;
}
vector<int> tp;
int x;
do {
x = st[top--];
tp.push_back(x);
} while (x != j);
son[i].push_back(tp);
}
}
}
int calc(int a, int b) {
int ans = 0;
for (int i = b; i != -1; i--) {
ans = (mod - ans + 1ll * ifac[i] * ifac[b - i] % mod * val[a + 2 * (b - i)]) % mod;
}
return 1ll * ans * fac[b] % mod;
}
pair<int, int> dfs2(vector<int> c) {
static int gu[N], vv1[N];
int vv = 1;
for (int x : c) {
vector<pair<int, int>> tp;
int s1 = 0;
for (vector<int> a : son[x]) {
auto [v0, v1] = dfs2(a);
if (v1 == -1) {
vv = 1ll * vv * v0 % mod;
s1++;
} else {
if (v1) {
tp.emplace_back(v0, v1);
} else {
vv = 1ll * vv * v0 % mod;
}
}
}
int cnt = 0;
gu[0] = 1;
for (auto [x, y] : tp) {
cnt++;
gu[cnt] = 1ll * gu[cnt - 1] * y % mod;
for (int j = cnt - 1; j; j--) {
gu[j] = (1ll * gu[j] * x + 1ll * gu[j - 1] * y) % mod;
}
gu[0] = 1ll * gu[0] * x % mod;
}
int a = s1;
for (int j = 0; j <= cnt; j++) {
vv1[j] = calc(a, j);
}
if (a % 2 == 0) {
for (int j = 0; j <= cnt; j++) {
g[x].first = (g[x].first + 1ll * gu[j] * vv1[j]) % mod;
}
for (int j = 1; j <= cnt; j++) {
g[x].second = (g[x].second + 1ll * j * vv1[j - 1] % mod * gu[j] + 2ll * a * j % mod * vv1[j - 1] % mod * gu[j]) % mod;
}
if (a >= 2) {
for (int j = 0; j <= cnt; j++) {
g[x].second = (g[x].second + 1ll * a * (a - 1) / 2 % mod * calc(a - 2, j) % mod * gu[j]) % mod;
}
}
for (int j = 2; j <= cnt; j++) {
g[x].second = (g[x].second + 2ll * j * (j - 1) % mod * gu[j] % mod * calc(a + 2, j - 2)) % mod;
}
} else {
for (int j = 0; j <= cnt; j++) {
g[x].first = (g[x].first + 1ll * gu[j] * vv1[j]) % mod;
}
g[x].second = -1;
}
}
if (c.size() == 1) {
int x = c[0];
if (x == 1) {
if (g[x].second == -1) {
cout << 0 << endl;
} else {
cout << 1ll * g[x].first * vv % mod;
}
exit(0);
}
if (g[x].second == -1) {
g[x].first = 1ll * g[x].first * vv % mod;
return {g[x].first, 0};
}
g[x].first = 1ll * g[x].first * vv % mod;
return {g[x].first, -1};
}
int x = c[0];
int dd[2][2] = {{0, 0}, {0, 0}};
if (g[x].second == -1) {
dd[1][0] = dd[0][1] = 1ll * vv * g[x].first % mod;
} else {
dd[0][0] = 1ll * vv * g[x].first % mod;
dd[1][1] = (1ll * vv * g[x].first + 2ll * vv * g[x].second) % mod;
}
for (int j = 1; j != (int)c.size(); j++) {
int y = c[j];
for (bool o : {0, 1}) {
if (g[y].second == -1) {
dd[o][0] = 1ll * dd[o][0] * g[y].first % mod;
dd[o][1] = 1ll * dd[o][1] * g[y].first % mod;
} else {
int xx = dd[o][0], yy = dd[o][1];
dd[o][0] = 1ll * yy * g[y].first % mod;
dd[o][1] = (1ll * xx * g[y].first + 2ll * xx * g[y].second) % mod;
}
}
}
if (dd[0][0] || dd[1][1]) {
return {(dd[1][1] + dd[0][0]) % mod, dd[0][0]};
} else {
return {(dd[0][1] + dd[1][0]) % mod, -1};
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
fac[0] = 1;
for (int i = 1; i <= m; i++) {
fac[i] = 1ll * fac[i - 1] * i % mod;
}
ifac[m] = power(fac[m], mod - 2);
for (int i = m - 1; i != -1; i--) {
ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;
}
val[0] = 1;
for (int i = 2; i <= m; i += 2) {
val[i] = 1ll * (i - 1) * val[i - 2] % mod;
}
for (int i = 1; i <= m; i += 2) {
val[i] = 1ll * i * val[i - 1] % mod;
}
while (m--) {
int x, y;
cin >> x >> y;
to[x].push_back(y), to[y].push_back(x);
}
dfs1(1, 0);
dfs2(vector<int>{1});
}
详细
Test #1:
score: 100
Accepted
time: 21ms
memory: 17224kb
input:
75000 99998 1 2 1 3 1 6 1 13 1 14 1 17 1 19 1 54 1 62 1 64 1 25003 1 50002 1 25004 1 50003 1 25005 1 50004 1 25006 1 50005 1 25007 1 50006 1 25008 1 50007 1 25009 1 50008 1 25010 1 50009 1 25011 1 50010 1 25012 1 50011 1 25013 1 50012 1 25014 1 50013 1 25015 1 50014 1 25016 1 50015 1 25017 1 50016 1...
output:
224026
result:
ok single line: '224026'
Test #2:
score: 0
Accepted
time: 25ms
memory: 16648kb
input:
75000 99998 1 2 1 3 1 10 1 18 1 63 1 514 1 3552 1 4093 1 6245 1 22678 1 25003 1 50002 1 25004 1 50003 1 25005 1 50004 1 25006 1 50005 1 25007 1 50006 1 25008 1 50007 1 25009 1 50008 1 25010 1 50009 1 25011 1 50010 1 25012 1 50011 1 25013 1 50012 1 25014 1 50013 1 25015 1 50014 1 25016 1 50015 1 2501...
output:
429308
result:
ok single line: '429308'
Test #3:
score: 0
Accepted
time: 24ms
memory: 17036kb
input:
75000 99998 1 2 1 3 1 58 1 66 1 77 1 210 1 770 1 844 1 25003 1 50002 1 25004 1 50003 1 25005 1 50004 1 25006 1 50005 1 25007 1 50006 1 25008 1 50007 1 25009 1 50008 1 25010 1 50009 1 25011 1 50010 1 25012 1 50011 1 25013 1 50012 1 25014 1 50013 1 25015 1 50014 1 25016 1 50015 1 25017 1 50016 1 25018...
output:
355105
result:
ok single line: '355105'
Test #4:
score: 0
Accepted
time: 21ms
memory: 16424kb
input:
75000 99998 1 2 1 6 1 89 1 137 1 169 1 2929 1 6649 1 24127 1 25003 1 50002 1 25004 1 50003 1 25005 1 50004 1 25006 1 50005 1 25007 1 50006 1 25008 1 50007 1 25009 1 50008 1 25010 1 50009 1 25011 1 50010 1 25012 1 50011 1 25013 1 50012 1 25014 1 50013 1 25015 1 50014 1 25016 1 50015 1 25017 1 50016 1...
output:
729262
result:
ok single line: '729262'
Test #5:
score: 0
Accepted
time: 24ms
memory: 17052kb
input:
75000 91998 1 2 1 7 1 8 1 11 1 77 1 107 1 118 1 332 1 804 1 14455 1 22866 1 34680 1 41003 1 58002 1 41004 1 58003 1 41005 1 58004 1 41006 1 58005 1 41007 1 58006 1 41008 1 58007 1 41009 1 58008 1 41010 1 58009 1 41011 1 58010 1 41012 1 58011 1 41013 1 58012 1 41014 1 58013 1 41015 1 58014 1 41016 1 ...
output:
916981
result:
ok single line: '916981'
Test #6:
score: 0
Accepted
time: 30ms
memory: 17268kb
input:
75000 91998 1 2 1 3 1 4 1 8 1 27 1 3972 1 4196 1 6344 1 8151 1 17056 1 41003 1 58002 1 41004 1 58003 1 41005 1 58004 1 41006 1 58005 1 41007 1 58006 1 41008 1 58007 1 41009 1 58008 1 41010 1 58009 1 41011 1 58010 1 41012 1 58011 1 41013 1 58012 1 41014 1 58013 1 41015 1 58014 1 41016 1 58015 1 41017...
output:
537508
result:
ok single line: '537508'
Test #7:
score: 0
Accepted
time: 24ms
memory: 16796kb
input:
75000 91998 1 2 1 4 1 7 1 16 1 60 1 72 1 77 1 87 1 448 1 3609 1 4997 1 10698 1 21177 1 41003 1 58002 1 41004 1 58003 1 41005 1 58004 1 41006 1 58005 1 41007 1 58006 1 41008 1 58007 1 41009 1 58008 1 41010 1 58009 1 41011 1 58010 1 41012 1 58011 1 41013 1 58012 1 41014 1 58013 1 41015 1 58014 1 41016...
output:
549167
result:
ok single line: '549167'
Test #8:
score: 0
Accepted
time: 27ms
memory: 17284kb
input:
75000 91998 1 2 1 3 1 19 1 55 1 133 1 229 1 263 1 277 1 352 1 729 1 41003 1 58002 1 41004 1 58003 1 41005 1 58004 1 41006 1 58005 1 41007 1 58006 1 41008 1 58007 1 41009 1 58008 1 41010 1 58009 1 41011 1 58010 1 41012 1 58011 1 41013 1 58012 1 41014 1 58013 1 41015 1 58014 1 41016 1 58015 1 41017 1 ...
output:
411498
result:
ok single line: '411498'
Test #9:
score: 0
Accepted
time: 32ms
memory: 16972kb
input:
75000 84355 1 60160 1 36004 1 2727 2 37452 3 48319 3 2480 4 58521 4 17803 4 43753 5 72576 5 29500 6 66502 6 8864 6 70643 7 52161 7 57465 8 41017 9 16472 9 32077 10 216 10 67333 10 6377 11 32592 11 15405 12 32930 12 72311 13 22378 13 19539 14 73204 14 47834 14 6326 15 65328 15 29270 16 62556 17 29754...
output:
0
result:
ok single line: '0'
Test #10:
score: 0
Accepted
time: 21ms
memory: 17056kb
input:
75000 84443 1 16256 1 65404 1 53817 2 15011 3 46150 3 58264 3 64631 4 63600 4 42517 5 35410 5 2918 6 43679 6 31347 7 313 7 26818 8 19803 8 28022 9 33081 9 58179 9 49534 10 28057 10 38811 10 65628 10 7342 11 8803 11 13929 12 41494 12 35263 13 32331 13 20071 13 45194 14 22131 14 51956 15 55884 15 2706...
output:
0
result:
ok single line: '0'
Test #11:
score: 0
Accepted
time: 30ms
memory: 16332kb
input:
75000 84361 1 14054 1 45822 1 5525 2 48240 2 55109 3 49958 3 43979 3 57569 3 13554 3 68084 4 38798 5 32827 5 3992 6 74366 6 62015 6 71485 7 8019 8 50760 8 19013 9 52224 10 38701 10 8292 11 10571 11 69807 11 68176 11 45014 11 38122 12 11627 12 63666 12 63447 13 72983 14 697 14 18585 14 11520 15 67317...
output:
0
result:
ok single line: '0'
Test #12:
score: 0
Accepted
time: 32ms
memory: 16704kb
input:
75000 84446 1 12163 1 10711 2 49923 2 44701 2 8609 3 53520 3 43344 3 38631 4 51228 4 73023 4 29607 4 47035 5 59192 5 7614 5 6391 6 32925 6 7726 7 25514 7 9391 8 44630 9 68916 9 7576 9 67445 10 28334 10 27979 11 64824 12 40803 13 47302 13 21264 14 49825 14 54823 14 61784 15 2695 15 22559 16 50048 16 ...
output:
708959
result:
ok single line: '708959'
Test #13:
score: 0
Accepted
time: 30ms
memory: 17068kb
input:
80000 89652 1 66308 1 10222 2 26590 2 71306 3 66796 3 29384 4 70837 5 40447 5 47609 6 34764 7 11408 7 14944 8 314 8 74773 8 41874 8 71852 9 17383 9 67743 9 5347 10 19430 10 56609 11 417 12 76333 12 60115 13 18696 14 57507 14 74267 15 65479 15 38262 15 43748 15 33902 16 68798 16 14903 17 25416 17 514...
output:
554931
result:
ok single line: '554931'
Test #14:
score: 0
Accepted
time: 26ms
memory: 17184kb
input:
80000 89479 1 20098 1 29446 2 53025 3 77498 3 42267 3 48225 4 59800 5 49989 5 75588 6 46532 6 3921 6 65370 6 43573 7 24293 7 72322 7 41443 7 50945 8 79801 8 6185 8 14588 9 63479 9 43283 10 4729 10 72803 10 75177 11 74978 11 28556 11 8400 11 35850 12 2045 13 57480 13 25090 13 17356 14 75090 15 48767 ...
output:
0
result:
ok single line: '0'
Test #15:
score: 0
Accepted
time: 37ms
memory: 17508kb
input:
80000 89541 1 37420 1 21167 2 27013 3 16643 3 7351 3 75810 4 72423 5 33283 5 24152 6 67473 6 35419 7 51075 8 64510 8 56113 9 25479 9 49232 10 79970 10 5793 11 72852 11 18034 11 53317 11 71175 12 74230 12 23826 13 30588 13 4880 13 13481 13 59718 13 48623 14 37346 15 1053 16 50470 17 78276 17 55335 17...
output:
0
result:
ok single line: '0'
Test #16:
score: 0
Accepted
time: 36ms
memory: 17188kb
input:
80000 89486 1 27427 1 14790 2 41804 2 6603 2 10074 2 26642 2 44375 3 11606 4 10048 4 48838 5 23219 6 53875 7 32447 7 10347 8 12387 8 19203 9 18548 9 64409 10 79601 11 48861 11 34818 12 50037 12 8561 12 46194 13 3983 14 5421 15 24834 15 43143 16 75566 17 26566 17 64114 17 70281 18 18296 18 43833 19 3...
output:
490881
result:
ok single line: '490881'
Test #17:
score: 0
Accepted
time: 40ms
memory: 19384kb
input:
90000 97174 1 7614 1 43646 2 71272 2 67043 3 53652 3 75364 4 77652 4 33533 5 2762 5 69727 6 33066 6 74704 6 32981 7 7827 8 32603 9 46684 9 23465 10 730 10 6472 10 8374 10 47655 11 49036 12 51500 12 2614 13 4012 13 15522 13 43567 14 52946 15 34403 15 83392 15 52725 16 44333 16 22639 16 748 17 18394 1...
output:
588018
result:
ok single line: '588018'
Test #18:
score: 0
Accepted
time: 38ms
memory: 18988kb
input:
90000 97212 1 33588 1 43881 2 32458 2 48845 3 10607 3 14583 3 61327 4 81356 5 85065 5 18512 5 9169 6 89640 6 70595 7 53905 7 32741 8 40142 9 35301 10 62004 11 30952 11 33180 12 61023 13 88711 13 36424 13 52374 14 23059 15 9374 16 68813 16 49264 16 8226 17 24553 17 69481 17 86966 18 1358 18 80756 18 ...
output:
729514
result:
ok single line: '729514'
Test #19:
score: 0
Accepted
time: 37ms
memory: 18796kb
input:
90000 97119 1 27709 1 75782 2 49423 2 43710 2 38280 3 79902 3 16177 4 17534 4 4971 4 52981 4 85259 5 61410 6 47670 6 24515 7 32817 7 7674 8 19146 9 2135 9 33461 9 77724 9 79066 9 66561 10 49909 11 65637 12 17342 12 24782 13 61518 13 47315 13 9454 13 6223 14 53351 14 21336 14 62013 15 34765 15 75067 ...
output:
0
result:
ok single line: '0'
Test #20:
score: 0
Accepted
time: 38ms
memory: 19160kb
input:
90000 97163 1 31427 1 70685 2 36010 2 48358 3 59856 3 18107 3 51742 4 14392 5 83470 5 51954 6 26664 6 62806 6 48105 6 61407 7 12462 8 5611 9 70485 10 17935 11 18902 11 22543 12 1168 12 43259 12 34919 12 70473 13 11396 13 8129 14 24370 14 81633 15 62316 16 31708 16 5261 16 73888 17 35435 17 47626 18 ...
output:
0
result:
ok single line: '0'
Test #21:
score: 0
Accepted
time: 27ms
memory: 20496kb
input:
99999 99998 97945 69840 97945 99591 97945 23621 97945 96306 97945 49596 97945 45916 97945 90799 97945 95403 97945 93990 97945 98394 97945 41714 97945 61236 97945 1701 97945 37124 97945 16020 97945 19127 97945 59494 97945 64104 97945 63062 97945 37016 97945 97047 97945 50811 97945 35552 97945 23083 9...
output:
539460
result:
ok single line: '539460'
Test #22:
score: 0
Accepted
time: 19ms
memory: 19944kb
input:
99995 99996 1 99991 2 99991 3 99991 4 99991 5 99991 6 99991 7 99991 8 99991 9 99991 10 99991 11 99991 12 99991 13 99991 14 99991 15 99991 16 99991 17 99991 18 99991 19 99991 20 99991 21 99991 22 99991 23 99991 24 99991 25 99991 26 99991 27 99991 28 99991 29 99991 30 99991 31 99991 32 99991 33 99991 ...
output:
627786
result:
ok single line: '627786'
Test #23:
score: 0
Accepted
time: 3116ms
memory: 17176kb
input:
75001 100000 1 3 1 2 3 2 3 4 1 6 1 5 6 5 6 7 1 9 1 8 9 8 9 10 1 12 1 11 12 11 12 13 1 15 1 14 15 14 15 16 1 18 1 17 18 17 18 19 1 21 1 20 21 20 21 22 1 24 1 23 24 23 24 25 1 27 1 26 27 26 27 28 1 30 1 29 30 29 30 31 1 33 1 32 33 32 33 34 1 36 1 35 36 35 36 37 1 39 1 38 39 38 39 40 1 42 1 41 42 41 42...
output:
132779
result:
ok single line: '132779'