QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#290534 | #4605. 生成树计数 | MoRanSky | 100 ✓ | 162ms | 33104kb | C++23 | 7.2kb | 2023-12-25 05:38:54 | 2023-12-25 05:38:55 |
Judging History
answer
// Skyqwq
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
typedef vector<int> Poly;
#define pb push_back
const int N = 1e5 + 5, P = 998244353, G = 3;
int A[N], rev[N], mod, inv[N], fact[N], infact[N];
int lim = 1, len = 0, W[20][N];
int inline power(int a, int b, int Mod = P) {
int res = 1;
while (b) {
if (b & 1) res = (LL)res * a % Mod;
a = (LL)a * a % Mod;
b >>= 1;
}
return res;
}
int Gi = power(G, P - 2, P), inv2 = power(2, P - 2, P);
void inline NTT(int c[], int lim, int o) {
for (int i = 0; i < lim; i++)
if (i < rev[i]) swap(c[i], c[rev[i]]);
for (int k = 1, t = 0; k < lim; k <<= 1, t++) {
for (int i = 0; i < lim; i += (k << 1)) {
for (int j = 0; j < k; j++) {
int u = c[i + j], v = (LL)c[i + k + j] * W[t][j] % P;
c[i + j] = u + v >= P ? u + v - P : u + v;
c[i + j + k] = u - v < 0 ? u - v + P : u - v;
}
}
}
if (o == -1) {
reverse(c + 1, c + lim);
int inv = power(lim, P - 2, P);
for (int i = 0; i < lim; i++)
c[i] = (LL)c[i] * inv % P;
}
}
void inline setN(int n) {
lim = 1, len = 0;
while (lim < n) lim <<= 1, len++;
for (int i = 0; i < lim; i++)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
}
Poly inline NTT(Poly a, int o) {
int n = a.size();
for (int i = 0; i < n; i++) A[i] = a[i];
NTT(A, lim, o);
a.clear();
for (int i = 0; i < lim; i++) a.push_back(A[i]), A[i] = 0;
return a;
}
Poly operator + (const Poly a, const Poly b) {
Poly c(max(a.size(), b.size()));
for (int i = 0; i < c.size(); i++) {
if (i < a.size()) {
c[i] += a[i]; if (c[i] >= P) c[i] -= P;
}
if (i < b.size()) {
c[i] += b[i]; if (c[i] >= P) c[i] -= P;
}
}
return c;
}
Poly operator - (const Poly a, const Poly b) {
Poly c(max(a.size(), b.size()));
for (int i = 0; i < c.size(); i++) {
if (i < a.size()) {
c[i] += a[i]; if (c[i] >= P) c[i] -= P;
}
if (i < b.size()) {
c[i] -= b[i]; if (c[i] < 0) c[i] += P;
}
}
return c;
}
Poly inline mul (Poly a, Poly b, int newn = -1) {
if (newn == -1) newn = a.size() + b.size() - 1;
setN(a.size() + b.size() - 1);
Poly c = NTT(a, 1), d = NTT(b, 1);
for (int i = 0; i < lim; i++) c[i] = (LL)c[i] * d[i] % P;
d = NTT(c, -1); d.resize(newn);
return d;
}
Poly inline reverse(Poly a) {
int n = a.size() - 1;
for (int i = 0; i < n - i; i++) swap(a[i], a[n - i]);
return a;
}
Poly inline dx(Poly a) {
int n = a.size();
Poly b; b.resize(n - 1);
for (int i = 0; i < b.size(); i++) b[i] = a[i + 1] * (i + 1ll) % P;
return b;
}
Poly inline F(Poly a) {
int n = a.size();
Poly b; b.resize(n + 1);
for (int i = 1; i < b.size(); i++) b[i] = (LL)a[i - 1] * inv[i] % P;
return b;
}
Poly polyInv(Poly a) {
int n = a.size();
if (n == 1) { Poly b; b.push_back(power(a[0], P - 2, P)); return b;}
Poly b = a; b.resize((n + 1) >> 1);
b = polyInv(b);
setN(2 * n);
a = NTT(a, 1), b = NTT(b, 1);
for (int i = 0; i < lim; i++)
b[i] = (LL)b[i] * (2ll - (LL)a[i] * b[i] % P + P) % P;
b = NTT(b, -1);
b.resize(n);
return b;
}
// 注意必须保证 n >= m
void inline div (Poly a, Poly b, Poly &Q, Poly &R) {
int n = a.size() - 1, m = b.size() - 1;
Poly ar = reverse(a), br = reverse(b);
ar.resize(n - m + 1), br.resize(n - m + 1);
Q = reverse(mul(ar, polyInv(br), n - m + 1));
R = a - mul(b, Q); R.resize(m);
}
Poly t(1, 1);
Poly inline ln(Poly a) {
Poly b = F(mul(dx(a), polyInv(a)));
b.resize(a.size());
return b;
}
Poly exp(Poly a) {
int n = a.size();
if (n == 1) return t;
Poly b = a; b.resize((n + 1) >> 1);
b = exp(b); b.resize(n);
Poly c = a - ln(b);
(c[0] += 1) %= P;
b = mul(b, c, a.size());
return b;
}
void cdq(int l, int r) {
if (r - l <= 0) return;
int mid = (l + r) >> 1, len = r - l;
cdq(l, mid);
// Do sth
cdq(mid + 1, r);
}
void inline preInv(int n) {
inv[1] = 1;
for (int i = 2; i <= n; i++)
inv[i] = ((LL)P - P / i) * inv[P % i] % P;
}
bool ok = false;
Poly inline power(Poly a, int b) {
int Mul = 1, Cy = 0, n = a.size();
if (b == 0) {
Poly c(n, 0);
c[0] = power(a[0], b, P);
return c;
}
if (a[0] == 0) {
for (Cy = 1; Cy < n; Cy++) if (a[Cy]) break;
if (ok || (LL)Cy * b >= n) {
Poly c(n, 0);
return c;
}
for (int i = 0; i + Cy < n; i++) a[i] = a[i + Cy];
for (int i = n - Cy; i < n; i++) a[i] = 0;
Cy *= b;
}
if (a[0] != 1) {
int in = power(a[0], P - 2, P); Mul = power(a[0], b, P);
for (int i = 0; i < n; i++) a[i] = (LL)a[i] * in % P;
}
a = ln(a);
for (int i = 0; i < n; i++) a[i] = (LL)a[i] * b % P;
a = exp(a);
if (Mul != 1) for (int i = 0; i < n; i++) a[i] = (LL)a[i] * Mul % P;
if (Cy) {
for (int i = n - 1; i >= Cy; i--) a[i] = a[i - Cy];
for (int i = 0; i < Cy; i++) a[i] = 0;
}
return a;
}
void inline factPrework(int n) {
fact[0] = infact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = (LL)fact[i - 1] * i % P;
infact[n] = power(fact[n], P - 2);
for (int i = n - 1; i; i--) infact[i] = infact[i + 1] * (i + 1ll) % P;
}
// 用到的最大的 n
void inline init(int n) {
preInv(n);
factPrework(n);
setN(2 * n);
for (int k = 1, t = 0; k < lim; k <<= 1, t++) {
int wn = power(G, (P - 1) / (k << 1));
W[t][0] = 1;
for (int j = 1; j < k; j++) W[t][j] = (LL)W[t][j - 1] * wn % P;
}
}
int n, m, a[N], w[N];
void inline add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
Poly X[N << 2], Y[N << 2];
#define ls (p << 1)
#define rs (p << 1 | 1)
void solve(int p, int l, int r) {
if (l == r) {
X[p].pb(1);
X[p].pb(P - a[r]);
Y[p].pb(1);
return;
}
int mid = (l + r) >> 1;
solve(ls, l, mid);
solve(rs, mid + 1, r);
X[p] = mul(X[ls], X[rs]);
Y[p] = mul(X[ls], Y[rs]) + mul(Y[ls], X[rs]);
}
void inline wk() {
solve(1, 1, n);
Poly T = mul(Y[1], polyInv(X[1]));
for (int i = 0; i <= n; i++) {
w[i] = T[i];
}
}
int main() {
init(3e4);
read(n), read(m);
for (int i = 1; i <= n; i++) read(a[i]);
if (n == 1) {
puts(!m ? "1" : "0");
return 0;
}
// calc wi = sum a[j]^i ()
wk();
// --
Poly A(n + 1, 0);
Poly B(n + 1, 0);
for (int i = 0; i <= n; i++) A[i] = 1ll * infact[i] * power(i + 1, m) % P;
for (int i = 0; i <= n; i++) B[i] = 1ll * infact[i] * power(i + 1, 2 * m) % P;
Poly C = ln(A);
for (int i = 0; i <= n; i++) C[i] = 1ll * C[i] * w[i] % P;
C = exp(C);
Poly D = mul(B, polyInv(A), n + 1);
for (int i = 0; i <= n; i++) D[i] = 1ll * D[i] * w[i] % P;
Poly G = mul(C, D, n + 1);
int ans = 1ll * G[n - 2] * fact[n - 2] % P;
for (int i = 1; i <= n; i++) ans = 1ll * ans * a[i] % P;
printf("%d\n", ans);
return 0;
}
详细
Test #1:
score: 5
Accepted
time: 0ms
memory: 23432kb
input:
105 14 138447198 654870701 150462538 749539275 29291082 757937945 39017800 516237051 770048433 208371121 183485972 565638440 892667426 429830095 20420107 149308492 634292609 342992316 892767631 233604646 172597402 779769499 264531516 831659743 713722094 913085287 579306192 484271595 775184458 338222...
output:
745603313
result:
ok 1 number(s): "745603313"
Test #2:
score: 5
Accepted
time: 4ms
memory: 23424kb
input:
189 23 641972211 979684612 152744946 783615120 978713222 168650336 857141516 616857395 540616523 113005973 711704024 687584327 301312674 275198920 51589456 80314015 157861752 762127499 92185899 106425187 268377739 537927189 848269317 38081352 129966693 24959563 806662836 815390176 283101118 70529095...
output:
31694519
result:
ok 1 number(s): "31694519"
Test #3:
score: 5
Accepted
time: 6ms
memory: 23440kb
input:
255 30 644732757 539051803 11521854 33672393 724898333 57201047 572157108 376478236 179179709 65318497 294731323 311061838 975241245 480268333 677060569 117849971 342083580 315785155 868382819 200079270 59824554 687361561 349882593 837076985 151361359 231945651 103914661 768822624 51062072 362357598...
output:
198283395
result:
ok 1 number(s): "198283395"
Test #4:
score: 5
Accepted
time: 4ms
memory: 23556kb
input:
479 21 425726002 487303905 37293431 95501469 427290832 900428476 754996703 736133996 43934815 494960949 987312413 635333947 129261282 468062813 784287979 243149526 861636447 278860015 226944388 909945345 495516775 65698638 337671210 862178697 622195526 997259676 284030292 21590370 502934410 81491610...
output:
332416726
result:
ok 1 number(s): "332416726"
Test #5:
score: 5
Accepted
time: 7ms
memory: 23688kb
input:
1001 23 43412769 328418634 172260660 515307451 265107560 252450730 354186179 30915291 670354972 243092380 821532610 887901814 265736742 515075155 107159529 951956978 476374470 778413044 522258384 147058289 564416380 105255288 103268840 275517718 878764216 619359944 558463566 852338938 439555478 4389...
output:
504242000
result:
ok 1 number(s): "504242000"
Test #6:
score: 5
Accepted
time: 11ms
memory: 23912kb
input:
1350 19 51792089 195765831 153834664 682500250 93550073 712089786 331935989 719713753 659375672 135741625 48747604 266234619 376000300 783975255 904432649 784067668 126677895 935652520 635232411 235725811 470906291 948563978 767164798 263773692 695228198 23765872 166571639 142065740 147681465 781765...
output:
21527657
result:
ok 1 number(s): "21527657"
Test #7:
score: 5
Accepted
time: 15ms
memory: 24032kb
input:
1999 14 977048642 436302631 51078567 715481869 937321294 40738049 39271403 736950737 829074708 197223160 776266475 680010881 145195765 861977709 405221131 794540650 842588046 156651101 467611893 323265287 355460693 137744170 725197657 13762224 727577079 996690158 270699851 57333509 403827104 4772745...
output:
119210730
result:
ok 1 number(s): "119210730"
Test #8:
score: 5
Accepted
time: 17ms
memory: 24200kb
input:
2509 28 693774123 174870138 336583497 720228549 699162721 573088543 491966100 633205770 781900217 284291396 889407725 103468453 967441012 45803434 289864228 603632648 121348048 825100009 833780846 415148480 454173856 391623014 357576987 258723971 820174171 245459016 184917741 69032619 139501944 5504...
output:
72861666
result:
ok 1 number(s): "72861666"
Test #9:
score: 5
Accepted
time: 34ms
memory: 24712kb
input:
5005 1 662319723 57173773 757645647 726996603 220670101 92998963 748404758 806177278 335795884 177330628 71230677 65521852 419502916 208926863 544809703 712331583 239364857 104977669 250992721 281232675 15113672 119283135 250544236 79314338 476188283 752159592 549820513 794094563 809015634 331358126...
output:
452373642
result:
ok 1 number(s): "452373642"
Test #10:
score: 5
Accepted
time: 72ms
memory: 26632kb
input:
10010 1 257336921 142539292 70533714 715092679 800022554 485944515 775769629 280877101 184644858 690111212 42846424 781413804 53942898 586475427 699985596 106017201 425551537 451899481 419432071 821680719 236400698 695020040 85130291 127141944 923119943 37707471 299559834 664382675 329282841 5533672...
output:
970412135
result:
ok 1 number(s): "970412135"
Test #11:
score: 5
Accepted
time: 38ms
memory: 24760kb
input:
5005 2 76151250 845540817 363885714 817247341 912391878 252921655 49257382 478924579 54420268 360118161 248430290 629132930 543292959 102242015 433093271 191508609 158347305 534693727 420196482 760310015 182444350 101548267 132308109 796578767 808363396 451057426 489003472 425626521 208186481 424059...
output:
374956031
result:
ok 1 number(s): "374956031"
Test #12:
score: 5
Accepted
time: 71ms
memory: 26820kb
input:
10015 2 310920628 839254433 220339414 515505380 582607535 748583798 875348092 158211247 370452787 716267850 294890331 247800791 29356266 417002500 82708702 223965410 174354176 941880171 785366764 631262 442792169 521920903 654306940 486539480 112881636 259298851 722799854 621079043 279305353 1027954...
output:
918785322
result:
ok 1 number(s): "918785322"
Test #13:
score: 5
Accepted
time: 29ms
memory: 24756kb
input:
5000 23 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 43855414 4385...
output:
253212914
result:
ok 1 number(s): "253212914"
Test #14:
score: 5
Accepted
time: 71ms
memory: 26828kb
input:
10000 27 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 203167337 2...
output:
895771012
result:
ok 1 number(s): "895771012"
Test #15:
score: 5
Accepted
time: 145ms
memory: 30840kb
input:
20000 19 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 74380774 743...
output:
91973126
result:
ok 1 number(s): "91973126"
Test #16:
score: 5
Accepted
time: 158ms
memory: 33104kb
input:
30000 29 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 292975416 2...
output:
903763257
result:
ok 1 number(s): "903763257"
Test #17:
score: 5
Accepted
time: 70ms
memory: 27808kb
input:
15000 15 432094160 826672471 66459847 496161113 970568934 570119738 319545126 698998332 132346021 4998 737952654 56041950 334984203 497869218 659008297 262495948 885560045 756305639 630873767 96889276 150483356 36151069 768118784 922961816 68580937 625900632 67294750 559220805 236631420 187431960 51...
output:
61109672
result:
ok 1 number(s): "61109672"
Test #18:
score: 5
Accepted
time: 150ms
memory: 30760kb
input:
20000 20 938923428 245728685 329575443 277635437 102904378 146372786 618943897 479294393 131191858 16295561 735444726 43609557 479766604 105216718 290827416 983641941 517126021 947178755 270386148 437534116 180103688 941061724 572552686 757491694 903454347 382785440 358780864 273564899 403004237 243...
output:
885273489
result:
ok 1 number(s): "885273489"
Test #19:
score: 5
Accepted
time: 162ms
memory: 32840kb
input:
28000 24 855300694 386133818 705146305 936773243 286991264 944467539 187184126 892681865 18123277 881026660 179932795 35128595 614747204 36334120 869838453 799505292 569255472 818530203 741767692 572775375 968515998 124926452 135739445 108940767 353549593 106384550 990557244 806049401 454171871 8988...
output:
350968823
result:
ok 1 number(s): "350968823"
Test #20:
score: 5
Accepted
time: 158ms
memory: 33056kb
input:
30000 30 9011967 960220911 49038306 30528077 693907891 571027429 630367765 77071332 555292056 687307517 23923128 375675120 236000142 327115127 76073627 783815174 93399649 337556192 996890954 941149537 776319700 593374944 252695496 167591842 274933366 272700955 827440792 548566078 619330066 66483862 ...
output:
908276353
result:
ok 1 number(s): "908276353"
Extra Test:
score: 0
Extra Test Passed