QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#67493 | #187. 数树 | alpha1022 | 100 ✓ | 278ms | 17560kb | C++23 | 8.3kb | 2022-12-10 16:31:17 | 2022-12-10 16:31:19 |
Judging History
answer
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#define add(a,b) (a + b >= mod ? a + b - mod : a + b)
#define dec(a,b) (a < b ? a - b + mod : a - b)
using namespace std;
const int N = 1e5;
const int mod = 998244353;
inline int fpow(int a,int b)
{
int ret = 1;
for(;b;b >>= 1)
(b & 1) && (ret = (long long)ret * a % mod),a = (long long)a * a % mod;
return ret;
}
int n,y,op;
namespace Poly
{
const int N = 1 << 18;
const int G = 3;
int lg2[N + 5];
int rev[N + 5],fac[N + 5],ifac[N + 5],inv[N + 5];
int rt[N + 5],irt[N + 5];
inline void init()
{
for(register int i = 2;i <= N;++i)
lg2[i] = lg2[i >> 1] + 1;
int w = fpow(G,(mod - 1) / N);
rt[N >> 1] = 1;
for(register int i = (N >> 1) + 1;i <= N;++i)
rt[i] = (long long)rt[i - 1] * w % mod;
for(register int i = (N >> 1) - 1;i;--i)
rt[i] = rt[i << 1];
fac[0] = 1;
for(register int i = 1;i <= N;++i)
fac[i] = (long long)fac[i - 1] * i % mod;
ifac[N] = fpow(fac[N],mod - 2);
for(register int i = N;i;--i)
ifac[i - 1] = (long long)ifac[i] * i % mod;
for(register int i = 1;i <= N;++i)
inv[i] = (long long)ifac[i] * fac[i - 1] % mod;
}
struct poly
{
vector<int> a;
inline poly(int x = 0)
{
x && (a.push_back(x),1);
}
inline poly(const vector<int> &o)
{
a = o,shrink();
}
inline void shrink()
{
for(;!a.empty() && !a.back();a.pop_back());
}
inline int size() const
{
return a.size();
}
inline void resize(int x)
{
a.resize(x);
}
inline int operator[](int x) const
{
if(x < 0 || x >= size())
return 0;
return a[x];
}
inline int &operator[](int x)
{
return a[x];
}
inline void clear()
{
vector<int>().swap(a);
}
inline void ntt(int type = 1)
{
int n = size();
type == -1 && (reverse(a.begin() + 1,a.end()),1);
int lg = lg2[n] - 1;
for(register int i = 0;i < n;++i)
rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << lg),
i < rev[i] && (swap(a[i],a[rev[i]]),1);
for(register int w = 2,m = 1;w <= n;w <<= 1,m <<= 1)
for(register int i = 0;i < n;i += w)
for(register int j = 0;j < m;++j)
{
int t = (long long)rt[m | j] * a[i | j | m] % mod;
a[i | j | m] = dec(a[i | j],t),a[i | j] = add(a[i | j],t);
}
if(type == -1)
for(register int i = 0;i < n;++i)
a[i] = (long long)a[i] * inv[n] % mod;
}
friend inline poly operator+(const poly &a,const poly &b)
{
vector<int> ret(max(a.size(),b.size()));
for(register int i = 0;i < ret.size();++i)
ret[i] = add(a[i],b[i]);
return poly(ret);
}
friend inline poly operator-(const poly &a,const poly &b)
{
vector<int> ret(max(a.size(),b.size()));
for(register int i = 0;i < ret.size();++i)
ret[i] = dec(a[i],b[i]);
return poly(ret);
}
friend inline poly operator*(poly a,poly b)
{
if(a.a.empty() || b.a.empty())
return poly();
int lim = 1,tot = a.size() + b.size() - 1;
for(;lim < tot;lim <<= 1);
a.resize(lim),b.resize(lim);
a.ntt(),b.ntt();
for(register int i = 0;i < lim;++i)
a[i] = (long long)a[i] * b[i] % mod;
a.ntt(-1),a.shrink();
return a;
}
poly &operator+=(const poly &o)
{
resize(max(size(),o.size()));
for(register int i = 0;i < o.size();++i)
a[i] = add(a[i],o[i]);
return *this;
}
poly &operator-=(const poly &o)
{
resize(max(size(),o.size()));
for(register int i = 0;i < o.size();++i)
a[i] = dec(a[i],o[i]);
return *this;
}
poly &operator*=(poly o)
{
return (*this) = (*this) * o;
}
poly deriv() const
{
if(a.empty())
return poly();
vector<int> ret(size() - 1);
for(register int i = 0;i < size() - 1;++i)
ret[i] = (long long)(i + 1) * a[i + 1] % mod;
return poly(ret);
}
poly integ() const
{
if(a.empty())
return poly();
vector<int> ret(size() + 1);
for(register int i = 0;i < size();++i)
ret[i + 1] = (long long)a[i] * inv[i + 1] % mod;
return poly(ret);
}
inline poly modxn(int n) const
{
n = min(n,size());
return poly(vector<int>(a.begin(),a.begin() + n));
}
inline poly inver(int m) const
{
poly ret(fpow(a[0],mod - 2));
for(register int k = 1;k < m;)
k <<= 1,ret = (ret * (2 - modxn(k) * ret)).modxn(k);
return ret.modxn(m);
}
inline poly log(int m) const
{
return (deriv() * inver(m)).integ().modxn(m);
}
inline poly exp(int m) const
{
poly ret(1);
for(register int k = 1;k < m;)
k <<= 1,ret = (ret * (1 - ret.log(k) + modxn(k))).modxn(k);
return ret.modxn(m);
}
};
}
using Poly::init;
using Poly::poly;
namespace Task0
{
unordered_map<int,unordered_set<int>> vis;
int ans,k;
void main()
{
k = n;
int u,v;
for(register int i = 2;i <= n;++i)
scanf("%d%d",&u,&v),u > v && (swap(u,v),1),vis[u].insert(v);
for(register int i = 2;i <= n;++i)
scanf("%d%d",&u,&v),u > v && (swap(u,v),1),k -= vis.count(u) && vis[u].count(v);
ans = fpow(y,k);
printf("%d\n",ans);
}
}
namespace Task1
{
int to[(N << 1) + 5],pre[(N << 1) + 5],first[N + 5];
inline void add_edge(int u,int v)
{
static int tot = 0;
to[++tot] = v,pre[tot] = first[u],first[u] = tot;
}
int fa[N + 5];
int c,ans,f[N + 5][2];
void dfs(int p)
{
f[p][0] = 1,f[p][1] = c;
for(register int i = first[p];i;i = pre[i])
if(to[i] ^ fa[p])
fa[to[i]] = p,
dfs(to[i]),
f[p][1] = ((long long)f[p][0] * f[to[i]][1] + (long long)f[p][1] * f[to[i]][0] + (long long)f[p][1] * f[to[i]][1]) % mod,
f[p][0] = ((long long)f[p][0] * f[to[i]][0] + (long long)f[p][0] * f[to[i]][1]) % mod;
}
void main()
{
if(y == 1)
{
printf("%d\n",fpow(n,n - 2));
return ;
}
int u,v;
for(register int i = 2;i <= n;++i)
scanf("%d%d",&u,&v),add_edge(u,v),add_edge(v,u);
c = (long long)n * y % mod * fpow(dec(1,y),mod - 2) % mod;
dfs(1);
ans = (long long)fpow(dec(1,y),n) * fpow(n,mod - 3) % mod * f[1][1] % mod;
printf("%d\n",ans);
}
}
namespace Task2
{
poly f;
int c,ans;
void main()
{
if(y == 1)
{
printf("%d\n",fpow(n,2 * n - 4));
return ;
}
init();
c = (long long)n * n % mod * y % mod * fpow(dec(1,y),mod - 2) % mod;
f.resize(n + 1);
for(register int i = 1;i <= n;++i)
f[i] = (long long)c * fpow(i,i) % mod * Poly::ifac[i] % mod;
f = f.exp(n + 1),ans = (long long)fpow(dec(1,y),n) * Poly::fac[n] % mod * fpow(n,mod - 5) % mod * f[n] % mod;
printf("%d\n",ans);
}
}
int main()
{
scanf("%d%d%d",&n,&y,&op);
if(!op)
Task0::main();
else if(op == 1)
Task1::main();
else
Task2::main();
}
详细
Test #1:
score: 2
Accepted
time: 3ms
memory: 3104kb
input:
10 97733807 0 4 9 1 8 9 7 10 8 5 3 2 3 10 3 3 7 6 9 4 9 1 8 3 7 2 3 6 9 10 3 8 9 9 7 5 3
output:
283139561
result:
ok 1 number(s): "283139561"
Test #2:
score: 2
Accepted
time: 75ms
memory: 17560kb
input:
97113 572544111 0 85116 86885 83696 33858 60990 57602 72077 74653 13516 84018 44398 2477 52043 7509 45435 91302 31654 55391 433 49328 81584 8482 43450 15971 67121 78887 11125 71991 93047 91000 76111 20923 78916 8417 37434 93336 78721 60237 76100 90321 59270 63662 1135 4454 65415 41283 27352 26669 48...
output:
856336316
result:
ok 1 number(s): "856336316"
Test #3:
score: 2
Accepted
time: 70ms
memory: 16876kb
input:
92250 902613619 0 31095 16639 84385 53915 44159 81962 80893 25917 27237 84588 7606 51233 73323 55771 30127 91804 9958 62997 81440 26645 68882 1681 76746 53580 37355 54307 44191 30974 11092 4437 89270 77446 47572 43778 75558 124 67496 45257 56466 23335 46804 56128 20244 83693 40658 81391 66154 32490 ...
output:
679057466
result:
ok 1 number(s): "679057466"
Test #4:
score: 1
Accepted
time: 2ms
memory: 5152kb
input:
3 423636777 1 1 3 1 2
output:
699516852
result:
ok 1 number(s): "699516852"
Test #5:
score: 1
Accepted
time: 2ms
memory: 3012kb
input:
5 769438012 1 4 5 3 2 2 5 1 4
output:
492436388
result:
ok 1 number(s): "492436388"
Test #6:
score: 6
Accepted
time: 0ms
memory: 3236kb
input:
467 758878847 1 252 241 458 440 458 248 114 213 350 338 6 316 270 387 157 17 229 368 14 304 458 111 452 382 269 11 458 307 268 174 144 240 286 129 365 328 458 148 458 130 458 104 458 445 224 222 334 306 82 232 167 8 465 322 168 292 48 36 85 437 458 313 160 132 386 239 7 430 69 432 247 310 458 84 285...
output:
720629517
result:
ok 1 number(s): "720629517"
Test #7:
score: 6
Accepted
time: 0ms
memory: 5080kb
input:
470 94671921 1 200 434 447 191 24 162 28 444 28 381 388 18 4 159 367 25 231 103 399 287 388 196 24 355 24 64 259 232 24 94 197 220 385 299 43 447 74 15 463 51 79 46 24 170 385 115 24 203 342 45 367 358 367 32 21 330 70 398 24 76 24 130 18 252 88 361 437 273 254 325 18 466 320 192 353 269 321 113 156...
output:
153298512
result:
ok 1 number(s): "153298512"
Test #8:
score: 6
Accepted
time: 0ms
memory: 3220kb
input:
4524 955788116 1 442 892 1440 2686 1213 399 3527 623 201 3251 4141 691 1627 2975 1440 499 1440 1135 2496 3790 3569 290 1440 2964 1440 631 1440 2531 4478 4041 1840 743 4124 3981 1440 756 679 3852 1440 2464 1440 1950 1132 622 456 647 1460 1759 4448 376 1440 3624 1236 3547 2535 1927 918 3662 1313 4332 ...
output:
476332561
result:
ok 1 number(s): "476332561"
Test #9:
score: 6
Accepted
time: 3ms
memory: 3208kb
input:
4934 13052936 1 1955 3761 4683 4379 180 1094 180 1716 4374 1659 2685 1586 4371 834 180 3726 27 3326 2475 2245 180 1497 2911 1924 1128 2887 826 2223 180 1179 180 1950 2900 1865 2986 2865 3472 3041 180 2829 180 4003 180 4613 1615 378 1516 3858 1114 2652 1797 3700 4501 297 3507 696 2474 13 1128 2064 18...
output:
442772276
result:
ok 1 number(s): "442772276"
Test #10:
score: 1
Accepted
time: 2ms
memory: 3040kb
input:
99914 1 1 13328 51343 15432 27410 93486 128 2783 37841 86248 12246 49690 70818 99645 78817 26570 57398 40214 19357 70491 18639 35104 23081 13328 95116 13687 25021 1476 58522 23862 33558 94926 49465 34578 48870 39895 53627 15197 94004 10431 63272 10559 60646 35463 2235 71010 43939 36028 59213 13328 5...
output:
545560075
result:
ok 1 number(s): "545560075"
Test #11:
score: 5
Accepted
time: 27ms
memory: 7144kb
input:
91940 608190966 1 18216 81653 46674 52064 78462 75194 21409 7465 73047 39252 35194 76666 78462 6121 5139 60935 21747 33877 17448 5842 64278 15890 19096 57288 78462 43547 78462 14000 61106 35289 83401 53112 40004 13567 80838 50765 44158 10892 78462 28580 78462 63505 70421 85727 46643 79333 20094 4169...
output:
941195593
result:
ok 1 number(s): "941195593"
Test #12:
score: 5
Accepted
time: 22ms
memory: 7260kb
input:
97091 216589897 1 41742 81257 38307 35015 14513 57294 86629 28982 10856 53837 18559 44404 90838 60441 38307 33369 95756 87518 77225 75848 94809 3150 38307 70495 9958 77947 38307 48125 8947 57420 38307 69865 88843 88247 7398 49661 55331 26437 45933 58433 52520 32811 96547 72130 49413 16694 53210 9268...
output:
800232100
result:
ok 1 number(s): "800232100"
Test #13:
score: 5
Accepted
time: 23ms
memory: 6792kb
input:
94382 929228179 1 48029 8415 82505 88628 27585 68425 11764 29468 14256 15727 53750 70815 14256 2851 76929 49782 32136 78720 22557 36683 11764 89242 21195 34050 25640 84425 14256 70469 89264 35081 78910 602 14256 30438 39782 47778 63534 17760 11764 53981 11773 62043 30839 8283 93248 38771 28929 6634 ...
output:
26441632
result:
ok 1 number(s): "26441632"
Test #14:
score: 5
Accepted
time: 18ms
memory: 7316kb
input:
93859 135460247 1 89459 24319 79026 18767 89459 81036 45068 48358 70351 54764 27476 60333 34989 67544 89459 3573 83852 81185 5697 12851 89459 73501 46160 20228 4551 34751 82892 75617 67106 80741 64907 63349 26607 30363 65868 73839 6421 6743 89459 40144 22793 65615 61997 80566 54326 14858 83608 67673...
output:
682864520
result:
ok 1 number(s): "682864520"
Test #15:
score: 1
Accepted
time: 4ms
memory: 8172kb
input:
3 393266836 2
output:
815898187
result:
ok 1 number(s): "815898187"
Test #16:
score: 1
Accepted
time: 4ms
memory: 8320kb
input:
10 146099373 2
output:
450425451
result:
ok 1 number(s): "450425451"
Test #17:
score: 6
Accepted
time: 1ms
memory: 8200kb
input:
464 157895536 2
output:
747558050
result:
ok 1 number(s): "747558050"
Test #18:
score: 6
Accepted
time: 2ms
memory: 8120kb
input:
475 972594936 2
output:
462113485
result:
ok 1 number(s): "462113485"
Test #19:
score: 6
Accepted
time: 13ms
memory: 8368kb
input:
4951 919004736 2
output:
262554311
result:
ok 1 number(s): "262554311"
Test #20:
score: 6
Accepted
time: 16ms
memory: 8356kb
input:
4763 65325417 2
output:
576657211
result:
ok 1 number(s): "576657211"
Test #21:
score: 1
Accepted
time: 2ms
memory: 3040kb
input:
94718 1 2
output:
643826176
result:
ok 1 number(s): "643826176"
Test #22:
score: 5
Accepted
time: 256ms
memory: 14872kb
input:
93067 666274736 2
output:
799030046
result:
ok 1 number(s): "799030046"
Test #23:
score: 5
Accepted
time: 271ms
memory: 14812kb
input:
91190 421191620 2
output:
151745318
result:
ok 1 number(s): "151745318"
Test #24:
score: 5
Accepted
time: 256ms
memory: 14712kb
input:
95268 247968469 2
output:
926246865
result:
ok 1 number(s): "926246865"
Test #25:
score: 5
Accepted
time: 278ms
memory: 14804kb
input:
93804 946644100 2
output:
804202472
result:
ok 1 number(s): "804202472"
Extra Test:
score: 0
Extra Test Passed