QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#554808#3000. 希望nfls_vjudge100 ✓1836ms751772kbC++149.0kb2024-09-09 16:05:172024-09-09 16:05:17

Judging History

你现在查看的是最新测评结果

  • [2024-09-09 16:05:17]
  • 评测
  • 测评结果:100
  • 用时:1836ms
  • 内存:751772kb
  • [2024-09-09 16:05:17]
  • 提交

answer

// Hydro submission #66dea71ba262ee455ee5b61b@1725869114973
// 那就是希望。
// 即便需要取模,也是光明。

// 牛逼题。
// 应该是目前国内官方赛场上出现过的最难的 DS 优化 dp 题。
// 没有之一。
// 容斥、长链剖分、可回退化数据结构、离线求逆元四倍役满!(雾)

#include <algorithm>
#include <stdio.h>
#include <vector>
typedef long long llt;
typedef unsigned uint;typedef unsigned long long ullt;
typedef bool bol;typedef char chr;typedef void voi;
typedef double dbl;
template<typename T>bol _max(T&a,T b){return(a<b)?a=b,true:false;}
template<typename T>bol _min(T&a,T b){return(b<a)?a=b,true:false;}
template<typename T>T lowbit(T n){return n&-n;}
template<typename T>T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T>T lcm(T a,T b){return(a!=0||b!=0)?a/gcd(a,b)*b:(T)0;}
template<typename T>T exgcd(T a,T b,T&x,T&y){if(b!=0){T ans=exgcd(b,a%b,y,x);y-=a/b*x;return ans;}else return y=0,x=1,a;}
template<typename T>T power(T base,T index,T mod)
{
    T ans=1%mod;
    while(index)
    {
        if(index&1)ans=ans*base%mod;
        base=base*base%mod,index>>=1;
    }
    return ans;
}
namespace ConstMod
{
    template<const ullt p>
    class mod_ullt
    {
        private:
            ullt v;
            inline ullt chg(ullt w){return(w<p)?w:w-p;}
            inline mod_ullt _chg(ullt w){mod_ullt ans;ans.v=(w<p)?w:w-p;return ans;}
        public:
            mod_ullt():v(0){}
            mod_ullt(ullt v):v(v%p){}
            bol empty(){return!v;}
            inline ullt val(){return v;}
            friend bol operator<(mod_ullt a,mod_ullt b){return a.v<b.v;}
            friend bol operator>(mod_ullt a,mod_ullt b){return a.v>b.v;}
            friend bol operator<=(mod_ullt a,mod_ullt b){return a.v<=b.v;}
            friend bol operator>=(mod_ullt a,mod_ullt b){return a.v>=b.v;}
            friend bol operator==(mod_ullt a,mod_ullt b){return a.v==b.v;}
            friend bol operator!=(mod_ullt a,mod_ullt b){return a.v!=b.v;}
            inline friend mod_ullt operator+(mod_ullt a,mod_ullt b){return a._chg(a.v+b.v);}
            inline friend mod_ullt operator-(mod_ullt a,mod_ullt b){return a._chg(a.v+a.chg(p-b.v));}
            inline friend mod_ullt operator*(mod_ullt a,mod_ullt b){return a.v*b.v;}
            friend mod_ullt operator/(mod_ullt a,mod_ullt b){return b._power(p-2)*a.v;}
            friend mod_ullt operator^(mod_ullt a,ullt b){return a._power(b);}
            inline mod_ullt operator-(){return _chg(p-v);}
            mod_ullt sqrt()
            {
                if(power(v,(p-1)>>1,p)!=1)return 0;
                mod_ullt b=1;do b++;while(b._power((p-1)>>1)==1);
                ullt t=p-1,s=0,k=1;while(!(t&1))s++,t>>=1;
                mod_ullt x=_power((t+1)>>1),e=_power(t);
                while(k<s)
                {
                    if(e._power(1llu<<(s-k-1))!=1)x*=b._power((1llu<<(k-1))*t);
                    e=_power(p-2)*x*x,k++;
                }
                return _min(x,-x),x;
            }
            mod_ullt inv(){return _power(p-2);}
            mod_ullt _power(ullt index){mod_ullt ans(1),w(v);while(index){if(index&1)ans*=w;w*=w,index>>=1;}return ans;}
            voi read(){v=0;chr c;do c=getchar();while(c>'9'||c<'0');do v=(c-'0'+v*10)%p,c=getchar();while(c>='0'&&c<='9');v%=p;}
            voi print()
            {
                static chr C[20];uint tp=0;
                ullt w=v;do C[tp++]=w%10+'0',w/=10;while(w);
                while(tp--)putchar(C[tp]);
            }
            voi println(){print(),putchar('\n');}
            mod_ullt operator++(int){mod_ullt ans=*this;return v=chg(v+1),ans;}
        public:
            inline ullt&operator()(){return v;}
            inline mod_ullt&operator+=(mod_ullt b){return*this=_chg(v+b.v);}
            inline mod_ullt&operator-=(mod_ullt b){return*this=_chg(v+chg(p-b.v));}
            inline mod_ullt&operator*=(mod_ullt b){return*this=v*b.v;}
            mod_ullt&operator^=(ullt b){return*this=_power(b);}
            mod_ullt&operator/=(mod_ullt b){return*this=b._power(p-2)*v;}
            mod_ullt&operator++(){return v=chg(v+1),*this;}
    };
}
const ullt Mod=998244353;
typedef ConstMod::mod_ullt<Mod>modint;
typedef std::vector<modint>modvec;
template<typename T> // 可回退化 DS
struct Roll{
    std::vector<T*>At;std::vector<T>W;uint tp;
    Roll():At(),W(),tp(0){}
    voi clear(){At.clear(),W.clear(),tp=0;}
    voi chg(T&p,T v){
        if(At.size()>tp)At.resize(tp),W.resize(tp);
        At.push_back(&p),W.push_back(p),p=v,tp++;
    }
    voi step_on(){if(tp<At.size())std::swap(*At[tp],W[tp]),tp++;}
    voi step_back(){if(tp)--tp,std::swap(*At[tp],W[tp]);}
    voi go_time(uint t){
        _min(t,(uint)At.size());
        while(tp<t)step_on();
        while(tp>t)step_back();
    }
};
modint _Base[20000005],*End=_Base+20000000;
modint*NewMemory(uint n){return End-=n;}
struct vec1{
    modint*P,mul,add,inv,C;uint from,siz;Roll<uint>Ru;Roll<modint>Rv;
    vec1():P(NULL),mul(1),add(),inv(1),C(),from(0),siz(0),Ru(),Rv(){}
    voi build(uint len){P=NewMemory(len);}
    voi chg(uint&a,uint b){Ru.chg(a,b);}
    voi chg(modint&a,modint b){Rv.chg(a,b);}
    modint val(uint p){return from+p>=siz?C:P[siz-p-1]*mul+add;}
    voi push(){chg(P[siz],-add*inv),chg(C,C+1),chg(add,add+1),chg(siz,siz+1);}
    voi merge(vec1&p){
        while(from>siz-p.siz)chg(from,from-1),chg(P[from],(C-add)*inv);
        modint a=p.val(1e9);
        if(a.empty()){
            for(uint i=0;i<p.siz;i++)chg(P[siz-i-1],(val(i)*p.val(i)-add)*inv);
            chg(C,0),chg(from,siz-p.siz);
        }
        else{
            for(uint i=0;i<p.siz;i++)chg(P[siz-i-1],val(i)*p.val(i));
            chg(mul,mul*a),chg(add,add*a),chg(C,C*a),chg(inv,inv/a);
            for(uint i=0;i<p.siz;i++)chg(P[siz-i-1],(P[siz-i-1]-add)*inv);
        }
    }
};
std::vector<uint>Way[1000005],Son[1000005];
uint Dep[1000005],MaxDep[1000005],Heavy[1000005],Fath[1000005],x;
voi dfs0(uint p,uint f){
    Heavy[p]=-1,MaxDep[p]=Dep[p],Fath[p]=f;
    for(auto s:Way[p])if(s!=f)Dep[s]=Dep[p]+1,dfs0(s,p),Son[p].push_back(s);
    std::sort(Son[p].begin(),Son[p].end(),[&](uint a,uint b){return MaxDep[a]>MaxDep[b];});
    if(Son[p].size())MaxDep[p]=MaxDep[Heavy[p]=Son[p][0]];
}
vec1 H[1000005];uint At[1000005];
modint GetVal[3][1000005];
uint Time[2][1000005];
voi dfs1(uint p,uint r){
    if(~Heavy[p])dfs1(Heavy[p],r),At[p]=At[Heavy[p]];else H[At[p]=p].build(Dep[p]-Dep[r]+2);
    vec1&h=H[At[p]];h.push();
    for(auto s:Son[p])if(s!=Heavy[p])
        dfs1(s,s),Time[0][s]=h.Ru.tp,Time[1][s]=h.Rv.tp,H[At[s]].push(),h.merge(H[At[s]]);
    GetVal[0][p]=h.val(x);if(x)GetVal[1][p]=h.val(x-1);
}
struct vec2{
    modint*P;modint add,mul,inv;uint siz;
    vec2():P(NULL),add(),mul(1),inv(1),siz(){}
    voi build(uint len){P=NewMemory(siz=len);}
    modint val(uint p){return P[siz-p-1]*mul+add;}
    voi pop(){siz--;}
};
vec2 G[1000005];uint At2[1000005];
voi dfs2(uint p){
    vec1&h=H[At[p]];vec2&g=G[At2[p]];
    if(!~Fath[p])g.build(MaxDep[p]+1),g.add=1;
    GetVal[2][p]=g.val(0),g.pop();
    if(!~Heavy[p])return;
    std::reverse(Son[p].begin(),Son[p].end());
    for(auto s:Son[p])if(s!=Heavy[p]){
        h.Ru.go_time(Time[0][s]),h.Rv.go_time(Time[1][s]);
        vec2&gs=G[At2[s]=s];
        gs.build(MaxDep[s]-Dep[s]+1),gs.add=1;
        for(uint i=0;i<gs.siz&&i<x;i++)
            gs.P[gs.siz-i-1]=g.val(i)*h.val(x-i-1);
        vec1&hs=H[At[s]];
        if(hs.siz>=std::min(g.siz,x)){
            for(uint i=0;i<g.siz&&i<x;i++)g.P[g.siz-i-1]=(hs.val(x-i-1)*g.val(i)-g.add)*g.inv;
        }else{
            if(hs.val(1e9).empty()){
                for(uint i=0;i<g.siz&&i<x;i++)g.P[g.siz-i-1]=(hs.val(x-i-1)*g.val(i)-g.add)*g.inv;
            }
            else{
                for(uint i=std::min(x,g.siz)-hs.siz;i<std::min(x,g.siz);i++)
                    g.P[g.siz-i-1]=hs.val(x-i-1)*g.val(i);
                g.mul*=hs.val(1e9),g.add*=hs.val(1e9),g.inv/=hs.val(1e9);
                for(uint i=std::min(x,g.siz)-hs.siz;i<std::min(x,g.siz);i++)
                    g.P[g.siz-i-1]=(g.P[g.siz-i-1]-g.add)*g.inv;
            }
        }
        dfs2(s);
    }
    if(g.siz>x)g.P[g.siz-x-1]=-g.add*g.inv;
    G[At2[Heavy[p]]=At2[p]].add++,dfs2(Heavy[p]);
}
int main()
{
#ifdef MYEE
    freopen("QAQ.in","r",stdin);
#endif
    uint n,k;scanf("%u%u%u",&n,&x,&k);
    for(uint i=1,u,v;i<n;i++)scanf("%u%u",&u,&v),Way[--u].push_back(--v),Way[v].push_back(u);
    dfs0(0,-1),dfs1(0,0),dfs2(0);
    // for(uint i=0;i<n;i++)GetVal[0][i].print(),putchar(" \n"[i==n-1]);
    // for(uint i=0;i<n;i++)GetVal[1][i].print(),putchar(" \n"[i==n-1]);
    // for(uint i=0;i<n;i++)GetVal[2][i].print(),putchar(" \n"[i==n-1]);
    modint ans;
    for(uint i=0;i<n;i++)ans+=(GetVal[0][i]*GetVal[2][i])^k;
    for(uint i=1;i<n;i++)ans-=(GetVal[1][i]*(GetVal[2][i]-1))^k;
    ans.println();
    return 0;
}

// 那就是希望。
// 即便需要取模,也是光明。

詳細信息


Pretests


Final Tests

Test #1:

score: 4
Accepted
time: 34ms
memory: 439936kb

input:

16 3 1
15 13
7 5
7 3
13 4
13 11
16 9
13 9
11 2
1 6
14 13
11 12
8 9
7 14
10 6
6 16

output:

1243

result:

ok single line: '1243'

Test #2:

score: 4
Accepted
time: 39ms
memory: 442096kb

input:

10 2 2
6 4
7 3
6 5
8 4
9 8
1 7
2 7
6 7
10 4

output:

9430

result:

ok single line: '9430'

Test #3:

score: 4
Accepted
time: 31ms
memory: 440064kb

input:

10 10 9
2 4
1 8
5 8
7 8
10 9
8 2
1 10
8 3
5 6

output:

287143445

result:

ok single line: '287143445'

Test #4:

score: 4
Accepted
time: 19ms
memory: 439664kb

input:

10 2 10
9 5
4 7
10 4
6 8
3 4
4 6
4 2
5 6
1 10

output:

736013382

result:

ok single line: '736013382'

Test #5:

score: 4
Accepted
time: 20ms
memory: 438892kb

input:

497 497 9
421 44
120 434
367 497
332 122
115 101
5 348
14 144
210 359
141 242
439 276
215 289
303 375
28 77
143 428
151 127
457 288
62 351
421 187
25 261
347 257
344 339
452 474
216 387
144 24
167 148
135 133
178 331
274 50
339 369
60 211
350 292
115 76
393 473
475 387
294 332
43 91
368 81
481 27
95...

output:

331917336

result:

ok single line: '331917336'

Test #6:

score: 4
Accepted
time: 23ms
memory: 439168kb

input:

1000 100 1
382 224
432 411
634 456
768 851
345 832
659 985
177 604
834 849
397 593
156 24
43 819
438 78
188 80
936 395
91 529
959 911
104 355
503 618
164 972
505 652
746 845
907 301
662 531
486 660
360 724
340 112
155 499
308 974
123 97
829 600
709 781
415 678
874 272
675 879
583 881
968 414
769 359...

output:

836607737

result:

ok single line: '836607737'

Test #7:

score: 4
Accepted
time: 60ms
memory: 455432kb

input:

49999 29 10
16726 12082
6732 37340
40485 10847
44869 24166
36684 39431
3249 27243
45483 37633
19260 22383
19143 13528
6706 7421
11839 47225
46922 11790
47525 3196
43850 42710
4893 2839
37556 41101
5873 45077
2696 21251
39836 36172
42780 38464
41481 9865
33864 24756
3474 13763
31198 28752
46506 3786
...

output:

562505285

result:

ok single line: '562505285'

Test #8:

score: 4
Accepted
time: 114ms
memory: 465436kb

input:

99990 95 1
73567 71079
3326 88384
36374 90162
37653 80637
66761 72061
61493 34910
70782 93885
15325 87771
81082 95813
1016 32294
3583 89414
63054 88734
902 70455
89078 71307
5572 73684
90068 2718
7522 87784
1880 77032
9206 13625
40074 66039
84079 94646
7226 26891
10757 47921
52558 82703
77478 24017
...

output:

625332568

result:

ok single line: '625332568'

Test #9:

score: 4
Accepted
time: 118ms
memory: 465744kb

input:

100000 100 9
71142 34813
51716 56682
16198 52726
21972 98808
8782 18661
64438 93384
62968 16698
66396 20240
57089 24568
53302 66919
46404 72330
31924 16432
72942 87322
42977 1430
31924 75941
59006 24077
93987 11904
11298 45520
31924 66491
52351 81769
98877 37816
48438 29764
10467 60332
46402 82645
3...

output:

854741172

result:

ok single line: '854741172'

Test #10:

score: 4
Accepted
time: 757ms
memory: 685668kb

input:

999992 282751 9
539736 453462
540111 477459
971608 731703
333352 646885
474338 449903
682998 756565
514706 652705
319529 600819
450009 862650
270005 950006
215331 949892
900851 316199
20848 879879
601074 688617
353471 60779
988869 786268
982113 354443
464498 645452
282384 979334
404667 275071
465382...

output:

242487770

result:

ok single line: '242487770'

Test #11:

score: 4
Accepted
time: 44ms
memory: 452136kb

input:

29997 29997 1
17887 27517
28638 28750
17146 20323
2484 14534
14496 9485
28598 1702
16812 20561
12792 10249
21147 12075
26413 21147
23535 12297
12912 2957
16747 4815
3695 4722
15400 11359
12046 1690
23085 4025
15400 29146
11317 21167
21147 27124
26258 22579
27050 28822
21147 12210
23688 24822
16221 1...

output:

208568713

result:

ok single line: '208568713'

Test #12:

score: 4
Accepted
time: 51ms
memory: 452600kb

input:

39999 9999 1
17847 8767
34288 3223
31953 15747
19891 38574
22166 12007
22545 10176
15852 4478
25563 38574
25054 22861
19512 38574
25737 7144
4838 36701
36463 6714
23038 3579
18929 21494
14933 19526
1630 30203
28237 23398
4178 14268
16048 18342
9864 7531
22278 29214
14797 32203
20 13687
3267 27370
29...

output:

622134586

result:

ok single line: '622134586'

Test #13:

score: 4
Accepted
time: 73ms
memory: 454476kb

input:

49937 9618 9
28900 21057
23426 3158
11352 23430
22070 36326
3612 43977
6755 2034
33497 5866
26127 35204
45896 2194
2698 6508
47136 7404
25263 35056
14916 24075
25469 5476
2913 24445
38256 32997
2634 14195
26348 45158
39795 30522
32668 23847
48619 32881
11604 33190
32221 44730
39330 46327
37184 15880...

output:

808296259

result:

ok single line: '808296259'

Test #14:

score: 4
Accepted
time: 106ms
memory: 467596kb

input:

99997 24814 1
70294 6008
96698 51718
85730 12149
7399 68219
19240 98520
6806 60763
60944 9942
95042 84051
52276 11791
92228 26864
31607 4577
50572 55556
91020 39530
99630 58302
46987 75388
13481 8988
73782 79423
13592 71106
68888 24123
69314 52558
1827 1930
2403 39363
71227 97979
62460 62818
93260 8...

output:

889032739

result:

ok single line: '889032739'

Test #15:

score: 4
Accepted
time: 178ms
memory: 484856kb

input:

149934 23430 1
19524 118124
102651 94642
105977 88780
22851 21085
118124 26220
51965 41865
94877 88657
45954 42676
15024 104515
88216 18289
71244 65571
85897 126980
121323 96801
149044 144348
118914 98362
41777 84779
95166 106140
50549 5071
69568 144894
133722 26506
32976 45985
139222 118124
57802 8...

output:

967739919

result:

ok single line: '967739919'

Test #16:

score: 4
Accepted
time: 208ms
memory: 506648kb

input:

199993 199993 10
22574 120810
132171 199046
85192 167635
166147 64640
92778 56014
52056 77741
141487 104029
192429 193787
24289 66737
56456 33117
12406 121250
139510 72340
37754 175679
27730 106773
34512 45901
169159 145205
167199 9455
124988 27796
183030 167303
176967 58418
116279 112701
122931 786...

output:

210281061

result:

ok single line: '210281061'

Test #17:

score: 4
Accepted
time: 212ms
memory: 502348kb

input:

199995 199995 1
74797 7556
81442 135821
185092 167712
42234 63649
134023 46082
134318 22976
128405 107157
36520 80426
167712 198927
132017 118773
288 1341
132649 22081
174449 167712
94580 179706
151008 160583
48868 128462
4161 125098
142488 88139
167712 47267
153225 129410
167712 100469
16050 89116
...

output:

241088863

result:

ok single line: '241088863'

Test #18:

score: 4
Accepted
time: 228ms
memory: 504272kb

input:

199996 25550 1
153865 144748
17792 151663
128934 59764
43644 133644
130769 176056
95288 123429
93190 140114
194626 118534
30624 86065
30624 103291
63137 117845
45411 57110
93790 4225
44261 92925
163904 119841
194626 20904
26921 194626
191171 32365
50763 194808
133250 162111
174639 5106
39176 139051
...

output:

346947170

result:

ok single line: '346947170'

Test #19:

score: 4
Accepted
time: 221ms
memory: 510600kb

input:

200000 24970 9
177589 184389
22449 43171
11368 15434
177589 124467
34715 172282
160344 28685
138184 4137
18031 177589
123360 2900
167503 74776
150552 180476
177589 198321
191168 145709
26761 40657
76252 155219
199611 140454
80426 54961
64064 134456
48347 114493
190498 177589
177589 2360
154689 13729...

output:

659235481

result:

ok single line: '659235481'

Test #20:

score: 4
Accepted
time: 206ms
memory: 505012kb

input:

200000 24973 9
118915 67700
120517 143573
66710 142095
140916 9440
144299 173165
179864 150849
108850 86634
183838 27249
56514 33874
45014 108850
185494 52388
160940 76515
27076 33073
194583 114506
127559 66004
44294 183831
7460 108850
198987 150508
45758 70252
59682 157110
49593 150673
67251 108850...

output:

371741367

result:

ok single line: '371741367'

Test #21:

score: 4
Accepted
time: 224ms
memory: 512632kb

input:

199999 15797 9
111155 118437
116181 103286
171596 117870
169742 58066
37077 12461
115452 191571
126892 134514
115497 151658
189077 76880
28368 17080
186378 14214
62745 18534
142023 76880
150833 175023
26865 15461
49984 107836
92202 136608
184737 112189
86026 148269
76880 147485
101065 111876
39958 1...

output:

923057136

result:

ok single line: '923057136'

Test #22:

score: 4
Accepted
time: 1429ms
memory: 751772kb

input:

999996 144346 1
424407 435560
510418 674573
150671 452726
128147 920948
510027 239220
972006 448279
281095 505374
655082 730449
15872 689268
70185 87349
741472 521817
723804 255286
811374 494975
443017 139873
406268 753301
20508 211500
191993 316128
730092 917846
868622 770478
738161 787331
87502 24...

output:

243642588

result:

ok single line: '243642588'

Test #23:

score: 4
Accepted
time: 1315ms
memory: 745100kb

input:

999997 139853 10
920585 690093
197610 611492
23489 349039
700388 280314
98990 218300
673469 707504
635638 180992
710821 330460
525503 990906
707504 708854
273138 955457
931868 88040
878331 611354
968123 352800
971264 681683
844600 397899
843885 192964
615112 555275
242987 872652
20593 782797
330349 ...

output:

66295209

result:

ok single line: '66295209'

Test #24:

score: 4
Accepted
time: 1654ms
memory: 748188kb

input:

999955 103989 10
925289 178114
308672 659094
669662 430933
561446 607489
799171 728376
452539 684799
9192 983985
974919 329141
696226 20531
172583 956538
197830 414632
820211 684799
180437 882577
684799 945146
18047 421707
266023 709380
377290 420390
920102 395704
702819 208991
700053 332653
479543 ...

output:

296918790

result:

ok single line: '296918790'

Test #25:

score: 4
Accepted
time: 1836ms
memory: 749188kb

input:

999936 123275 9
536599 787920
7994 306084
494368 947491
507729 297675
824018 568122
28934 711706
235386 798753
343255 798753
953136 324491
67553 200106
466356 485763
569760 432140
864787 466356
236533 560892
469895 353561
686792 47275
575895 202643
798753 887888
735885 356054
335977 648100
900249 62...

output:

209607906

result:

ok single line: '209607906'

Extra Test:

score: 0
Extra Test Passed