QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#782689 | #9651. 又一个欧拉数问题 | QBF | 100 ✓ | 1755ms | 45868kb | C++23 | 5.3kb | 2024-11-25 21:00:33 | 2024-11-25 21:00:40 |
Judging History
answer
#include<bits/stdc++.h>
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define ci const int
#define ll long long
using namespace std;
ci mod=998244353,N=4e5+5;
int fac[N],dfac[N],inv[N];
inline void add(int &x,ci v){
x+=v,x-=x<mod?0:mod;
}
inline void sub(int &x,ci v){
x-=v,x+=x<0?mod:0;
}
ll qk(ll x,int y=mod-2){
ll ans=1;
while(y){
if(y&1)ans=ans*x%mod;
x=x*x%mod,y>>=1;
}
return ans;
}
ll C(ci n,ci m){
if(n<m||m<0)return 0;
return (ll)fac[n]*dfac[m]%mod*dfac[n-m]%mod;
}
int n,k,w[8],co[8],cnt[8];
int trs[4][4][N],ed[4][N],cur[8],tmp[8];
int dp[N][4];
void Pre(){
for(int S=0;S<1<<k-2;++S){
memset(cur,0,sizeof(cur));
cur[S]=1;
for(int i=1;i<=n;++i){
memset(tmp,0,sizeof(tmp));
for(int s=0;s<1<<k-2;++s)
for(int t=0;t<1<<k-1;++t){
int nx=s|t;
if(nx&1)add(tmp[nx>>1],(ll)cur[s]*co[t]%mod);
else add(trs[S][nx>>1][i],(ll)cur[s]*co[t]%mod*dfac[i]%mod);
if(nx==0)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i]%mod);
if(nx==1)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i+1]%mod);
if(nx==2)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i]%mod*dfac[2]%mod);
if(nx==3)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i+2]%mod);
if(nx==4)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i]%mod*dfac[2]%mod);
if(nx==5)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i+1]%mod*dfac[2]%mod);
if(nx==6)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i]%mod*dfac[3]%mod);
if(nx==7)add(ed[S][i],(ll)cur[s]*co[t]%mod*dfac[i+3]%mod);
}
memcpy(cur,tmp,sizeof(cur));
}
}
}
ci g=3,ig=qk(3);
int R[N];
void NTT(int *a,ci n,ci tp){
for(int i=0;i<n;++i)R[i]=(R[i>>1]>>1)|((i&1)?(n>>1):0);
for(int i=0;i<n;++i)if(i<R[i])swap(a[i],a[R[i]]);
for(int len=2;len<=n;len<<=1){
ci gn=qk(tp==1?g:ig,(mod-1)/len);
for(int l=0;l<n;l+=len)
for(int k=l,al,ar,cur=1;k<l+(len>>1);++k)
al=a[k],ar=a[k+(len>>1)],
a[k]=(al+(ll)ar*cur)%mod,
a[k+(len>>1)]=(al-(ll)ar*cur%mod+mod)%mod,
cur=(ll)cur*gn%mod;
}
if(tp==-1){
ci inv=qk(n);
for(int i=0;i<n;++i)a[i]=(ll)a[i]*inv%mod;
}
}
namespace poly{
const int N=2097153,mo=998244353;
int n,m,a[N],b[N],G[N],Ginv[N],rev[N];
ll ksm(ll x,int y){ll s=1;for(;y;y>>=1,x=x*x%mo)if(y&1)s=s*x%mo;return s;}
void init(int n)
{
for(int i=1;i<n;i<<=1)
{
int g0=ksm(3,(mo-1)/(i<<1)),g0i=ksm(g0,mo-2),g=1,gi=1;
fu(j,i,i+i) G[j]=g,Ginv[j]=gi,g=1ull*g*g0%mo,gi=1ull*gi*g0i%mo;
}
}
void ntt(int n,int *a,int pd)
{
fu(i,0,n) if(i<rev[i]) swap(a[i],a[rev[i]]);
for(int i=1,x;i<n;i<<=1)
for(int *j=a;j<a+n;j+=(i<<1))
{
for(int *k=j,*buf=(pd?G:Ginv)+i;k<j+i;++k,++buf)
{
x=1ull*k[i]*(*buf)%mo;
if((k[i]=*k+mo-x)>=mo) k[i]-=mo;
if((*k+=x)>=mo) *k-=mo;
}
}
if(!pd)
{
int invn=ksm(n,mo-2);
fu(i,0,n) a[i]=1ull*a[i]*invn%mo;
}
}
int gt(int deg){
int n=1,s=0;while(n<deg) n<<=1,s++;
fu(i,0,n) rev[i]=(rev[i>>1]>>1)|((i&1)<<(s-1));
init(n);
return n;
}
void nt(int *f,ci n){
ntt(n,f,1);
}
void nt2(int *f,int *g,int *h,ci n){
fu(i,0,n) h[i]=1ull*f[i]*g[i]%mo;
ntt(n,h,0);
}
// void NTT(int n1,int n2,int deg,int *f,int *g)
// {
// int n=1,s=0;while(n<deg) n<<=1,s++;
// init(n);
// for(int i=n1;i<n;++i)f[i]=0;
// for(int i=n2;i<n;++i)g[i]=0;
// fu(i,0,n) rev[i]=(rev[i>>1]>>1)|((i&1)<<(s-1));
// ntt(n,f,1);
// ntt(n,g,1);
// fu(i,0,n) f[i]=1ull*f[i]*g[i]%mo;
// ntt(n,f,0);
// }
}
int A[4][N],B[4][N],c[N];
void Solve(ci l,ci r){
if(l==r){
if(l==1)++dp[1][0];
return;
}
ci mid=l+r>>1;
Solve(l,mid);
if(r-l<=150){
for(int i=l;i<=mid;++i)
for(int s=0;s<1<<k-2;++s)
if(dp[i][s])
for(int j=mid+1;j<=r;++j)
for(int t=0;t<1<<k-2;++t)
add(dp[j][t],(ll)dp[i][s]*trs[s][t][j-i]%mod);
}
else{
int n1=mid-l+1,n2=r-l;
int G=poly::gt(n1+n2-1);
for(int s=0;s<1<<k-2;++s){
int n1=0;
for(int i=0;i<G;++i)A[s][i]=0;
for(int i=l;i<=mid;++i)A[s][n1++]=dp[i][s];
poly::nt(A[s],G);
}
for(int s=0;s<1<<k-2;++s)
for(int t=0;t<1<<k-2;++t){
int n2=0;
for(int i=0;i<G;++i)B[t][i]=0;
for(int i=1;i<=r-l;++i)B[t][n2++]=trs[s][t][i];
poly::nt(B[t],G);
poly::nt2(A[s],B[t],c,G);
for(int i=mid+1;i<=r;++i)add(dp[i][t],c[i-l-1]);
}
// for(int s=0;s<1<<k-2;++s)
// for(int t=0;t<1<<k-2;++t){
// int n1=0,n2=0;
// for(int i=l;i<=mid;++i)A[n1++]=dp[i][s];
// for(int i=1;i<=r-l;++i)B[n2++]=trs[s][t][i];
// poly::NTT(n1,n2,n1+n2-1,A,B);
// for(int i=mid+1;i<=r;++i)add(dp[i][t],A[i-l-1]);
// }
}
Solve(mid+1,r);
}
int main(){
fac[0]=fac[1]=dfac[0]=dfac[1]=inv[1]=1;
for(int i=2;i<N;++i)
fac[i]=(ll)fac[i-1]*i%mod,
inv[i]=(ll)(mod-mod/i)*inv[mod%i]%mod,
dfac[i]=(ll)dfac[i-1]*inv[i]%mod;
scanf("%d%d",&n,&k);
for(int s=0;s<1<<k-1;++s)scanf("%d",&w[s]),cnt[s]=cnt[s>>1]^(s&1);
int st=clock();
for(int A=0;A<1<<k-1;++A){
ci B=((1<<k-1)-1)^A;
for(int t=B;;t=(t-1)&B){
(cnt[t]?sub:add)(co[t|A],w[A]);
if(!t)break;
}
}
Pre();
int ans=0;
n=n-k+1;
Solve(1,n);
n=n+k-1;
for(int i=1;i<=n-k+1;++i)for(int s=0;s<1<<k-2;++s)add(ans,(ll)dp[i][s]*ed[s][n-k+1-i+1]%mod);
ans=(ll)ans*fac[n]%mod;
printf("%d",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 0ms
memory: 13180kb
input:
4 4 698120943 122288832 680575548 463848799 156774757 854895700 608223343 677744207
output:
129451994
result:
ok 1 number(s): "129451994"
Test #2:
score: 5
Accepted
time: 0ms
memory: 14300kb
input:
5 4 550891357 916542306 749948554 123704496 62951279 389103312 185283715 952036050
output:
603530316
result:
ok 1 number(s): "603530316"
Test #3:
score: 5
Accepted
time: 0ms
memory: 13776kb
input:
5 4 0 0 0 0 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 5
Accepted
time: 4ms
memory: 13672kb
input:
9 4 932794876 983187846 61110015 10089567 242406926 990649413 302889463 707289292
output:
623984278
result:
ok 1 number(s): "623984278"
Test #5:
score: 5
Accepted
time: 0ms
memory: 13548kb
input:
10 4 0 0 0 0 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #6:
score: 5
Accepted
time: 4ms
memory: 13852kb
input:
7 4 75656923 89231235 268411832 331473274 613621490 724088841 938061331 436247598
output:
828280933
result:
ok 1 number(s): "828280933"
Subtask #2:
score: 10
Accepted
Dependency #1:
100%
Accepted
Test #7:
score: 10
Accepted
time: 4ms
memory: 13228kb
input:
17 4 502378168 0 899256313 98988040 502378168 899256313 495866185 403390128
output:
955634034
result:
ok 1 number(s): "955634034"
Test #8:
score: 10
Accepted
time: 2ms
memory: 14164kb
input:
12 4 973896694 511296006 0 486948347 0 0 486948347 511296006
output:
717853738
result:
ok 1 number(s): "717853738"
Test #9:
score: 10
Accepted
time: 0ms
memory: 14428kb
input:
19 4 0 0 0 0 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #10:
score: 10
Accepted
time: 4ms
memory: 14760kb
input:
11 4 419369069 674825741 201692543 290396938 869076983 225876646 409445596 934556751
output:
579300967
result:
ok 1 number(s): "579300967"
Test #11:
score: 10
Accepted
time: 4ms
memory: 13856kb
input:
16 4 399991278 671519396 535203044 718737955 71028311 806731162 326724957 940847965
output:
842945071
result:
ok 1 number(s): "842945071"
Test #12:
score: 10
Accepted
time: 4ms
memory: 14772kb
input:
17 4 836771329 338008804 131570815 515413785 262236691 408466186 362787701 152542617
output:
293433305
result:
ok 1 number(s): "293433305"
Subtask #3:
score: 5
Accepted
Test #13:
score: 5
Accepted
time: 4ms
memory: 13144kb
input:
2 2 0 0
output:
0
result:
ok 1 number(s): "0"
Test #14:
score: 5
Accepted
time: 0ms
memory: 13636kb
input:
3 2 0 142044554
output:
704013496
result:
ok 1 number(s): "704013496"
Test #15:
score: 5
Accepted
time: 0ms
memory: 13088kb
input:
4 2 0 0
output:
0
result:
ok 1 number(s): "0"
Test #16:
score: 5
Accepted
time: 0ms
memory: 14164kb
input:
5 2 0 0
output:
0
result:
ok 1 number(s): "0"
Test #17:
score: 5
Accepted
time: 143ms
memory: 34500kb
input:
99487 2 0 0
output:
0
result:
ok 1 number(s): "0"
Test #18:
score: 5
Accepted
time: 157ms
memory: 30436kb
input:
99738 2 693173587 283412622
output:
815899210
result:
ok 1 number(s): "815899210"
Subtask #4:
score: 10
Accepted
Test #19:
score: 10
Accepted
time: 4ms
memory: 13788kb
input:
3 3 977925087 204858071 813705548 204858071
output:
225177337
result:
ok 1 number(s): "225177337"
Test #20:
score: 10
Accepted
time: 4ms
memory: 14692kb
input:
4 3 455457192 542787161 728591379 0
output:
494572650
result:
ok 1 number(s): "494572650"
Test #21:
score: 10
Accepted
time: 4ms
memory: 13348kb
input:
5 3 280102847 175353772 822890581 718141506
output:
0
result:
ok 1 number(s): "0"
Test #22:
score: 10
Accepted
time: 4ms
memory: 13552kb
input:
93 3 517938077 480306276 173009841 0
output:
132737095
result:
ok 1 number(s): "132737095"
Test #23:
score: 10
Accepted
time: 4ms
memory: 13268kb
input:
85 3 812966373 8069068 241411799 241442405
output:
835292882
result:
ok 1 number(s): "835292882"
Test #24:
score: 10
Accepted
time: 0ms
memory: 12616kb
input:
93 3 740309863 562024812 723775833 67304547
output:
79626905
result:
ok 1 number(s): "79626905"
Subtask #5:
score: 10
Accepted
Dependency #4:
100%
Accepted
Test #25:
score: 10
Accepted
time: 8ms
memory: 23432kb
input:
3204 3 390926493 321900190 164112702 172373440
output:
25228045
result:
ok 1 number(s): "25228045"
Test #26:
score: 10
Accepted
time: 4ms
memory: 14388kb
input:
118 3 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #27:
score: 10
Accepted
time: 0ms
memory: 21432kb
input:
1812 3 743708154 0 458364972 539879381
output:
369996118
result:
ok 1 number(s): "369996118"
Test #28:
score: 10
Accepted
time: 7ms
memory: 29384kb
input:
3997 3 506494422 310846999 0 0
output:
180977771
result:
ok 1 number(s): "180977771"
Test #29:
score: 10
Accepted
time: 13ms
memory: 21576kb
input:
3919 3 850826367 581870616 262788170 385598679
output:
718155036
result:
ok 1 number(s): "718155036"
Test #30:
score: 10
Accepted
time: 13ms
memory: 26056kb
input:
3908 3 268833736 15860337 13324648 101653332
output:
307317750
result:
ok 1 number(s): "307317750"
Subtask #6:
score: 15
Accepted
Dependency #5:
100%
Accepted
Test #31:
score: 15
Accepted
time: 100ms
memory: 27288kb
input:
27617 3 649538421 649538421 697411864 348705932
output:
147599656
result:
ok 1 number(s): "147599656"
Test #32:
score: 15
Accepted
time: 91ms
memory: 27756kb
input:
32594 3 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #33:
score: 15
Accepted
time: 46ms
memory: 22048kb
input:
18203 3 971232001 436685091 53582375 579077060
output:
15944343
result:
ok 1 number(s): "15944343"
Test #34:
score: 15
Accepted
time: 109ms
memory: 27712kb
input:
39024 3 761026701 107837672 107837672 129379980
output:
733762036
result:
ok 1 number(s): "733762036"
Test #35:
score: 15
Accepted
time: 109ms
memory: 22988kb
input:
39934 3 860432642 218393709 0 137811711
output:
959310258
result:
ok 1 number(s): "959310258"
Test #36:
score: 15
Accepted
time: 102ms
memory: 32696kb
input:
39441 3 647870660 428771613 299764381 537645434
output:
403002156
result:
ok 1 number(s): "403002156"
Subtask #7:
score: 5
Accepted
Dependency #6:
100%
Accepted
Test #37:
score: 5
Accepted
time: 233ms
memory: 28648kb
input:
65961 3 573372243 470586592 899656037 952529871
output:
727883299
result:
ok 1 number(s): "727883299"
Test #38:
score: 5
Accepted
time: 476ms
memory: 37944kb
input:
95856 3 657353865 0 340890488 0
output:
443504623
result:
ok 1 number(s): "443504623"
Test #39:
score: 5
Accepted
time: 107ms
memory: 23128kb
input:
43044 3 735177548 164240636 263066805 263066805
output:
357165044
result:
ok 1 number(s): "357165044"
Test #40:
score: 5
Accepted
time: 471ms
memory: 39768kb
input:
99124 3 0 626743689 688853562 309390791
output:
488790683
result:
ok 1 number(s): "488790683"
Test #41:
score: 5
Accepted
time: 485ms
memory: 38288kb
input:
99895 3 599127905 0 269443581 399116448
output:
308060904
result:
ok 1 number(s): "308060904"
Test #42:
score: 5
Accepted
time: 482ms
memory: 30916kb
input:
99441 3 81228584 583326742 103263243 128429203
output:
142331215
result:
ok 1 number(s): "142331215"
Subtask #8:
score: 10
Accepted
Dependency #2:
100%
Accepted
Test #43:
score: 10
Accepted
time: 5ms
memory: 13876kb
input:
4 4 0 0 0 0 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #44:
score: 10
Accepted
time: 5ms
memory: 14024kb
input:
5 4 719850794 868458999 534771757 496641034 108328567 58126453 451622145 127965210
output:
199502123
result:
ok 1 number(s): "199502123"
Test #45:
score: 10
Accepted
time: 13ms
memory: 29796kb
input:
1620 4 575012072 993907457 465640749 414558489 536940500 884536134 536940500 579348968
output:
276727543
result:
ok 1 number(s): "276727543"
Test #46:
score: 10
Accepted
time: 15ms
memory: 31244kb
input:
2000 4 583522935 653359292 238637874 720209712 120795105 906170921 202280741 436530633
output:
247950749
result:
ok 1 number(s): "247950749"
Test #47:
score: 10
Accepted
time: 11ms
memory: 30832kb
input:
1903 4 0 0 0 0 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #48:
score: 10
Accepted
time: 13ms
memory: 29660kb
input:
1970 4 634852705 681848099 480528749 96325370 426074420 662814695 282626889 407588785
output:
358841946
result:
ok 1 number(s): "358841946"
Subtask #9:
score: 10
Accepted
Dependency #8:
100%
Accepted
Test #49:
score: 10
Accepted
time: 379ms
memory: 34220kb
input:
35788 4 137592553 167362125 666174864 893867308 935259273 409053348 908484962 421828880
output:
317183526
result:
ok 1 number(s): "317183526"
Test #50:
score: 10
Accepted
time: 148ms
memory: 31272kb
input:
13180 4 455644004 629655674 433939914 99482062 167912374 215744296 744048010 856909532
output:
724269763
result:
ok 1 number(s): "724269763"
Test #51:
score: 10
Accepted
time: 339ms
memory: 33108kb
input:
25810 4 50511582 266090813 782665122 316602395 681641958 25053907 922678864 732153540
output:
316456760
result:
ok 1 number(s): "316456760"
Test #52:
score: 10
Accepted
time: 348ms
memory: 33876kb
input:
39626 4 0 0 0 0 0 0 0 0
output:
0
result:
ok 1 number(s): "0"
Test #53:
score: 10
Accepted
time: 384ms
memory: 34056kb
input:
39520 4 304751689 247786932 175360249 662006566 300713484 967876352 912150432 961654612
output:
581564252
result:
ok 1 number(s): "581564252"
Test #54:
score: 10
Accepted
time: 385ms
memory: 38964kb
input:
39654 4 199691859 32920622 161790938 562758769 16726982 895821371 135168521 518536619
output:
389091873
result:
ok 1 number(s): "389091873"
Subtask #10:
score: 20
Accepted
Dependency #9:
100%
Accepted
Test #55:
score: 20
Accepted
time: 843ms
memory: 36884kb
input:
70610 4 68292738 168290466 829953887 829953887 168290466 929951615 99997728 829953887
output:
139356701
result:
ok 1 number(s): "139356701"
Test #56:
score: 20
Accepted
time: 874ms
memory: 38172kb
input:
83154 4 40222982 0 40222982 40222982 958021371 0 877575407 0
output:
810635777
result:
ok 1 number(s): "810635777"
Test #57:
score: 20
Accepted
time: 766ms
memory: 34648kb
input:
50832 4 105333371 857557033 892910982 260281951 843295773 154948580 892910982 892910982
output:
241653357
result:
ok 1 number(s): "241653357"
Test #58:
score: 20
Accepted
time: 1755ms
memory: 40336kb
input:
99201 4 259092713 0 0 811163900 446173166 552071187 739151640 187080453
output:
150187101
result:
ok 1 number(s): "150187101"
Test #59:
score: 20
Accepted
time: 1748ms
memory: 39448kb
input:
99797 4 367311954 251136106 555832002 724805462 945161134 244359464 684015618 92172056
output:
25758638
result:
ok 1 number(s): "25758638"
Test #60:
score: 20
Accepted
time: 1737ms
memory: 45868kb
input:
99065 4 671004682 687177570 888521328 363461538 143576590 52815535 910840671 989086834
output:
379711332
result:
ok 1 number(s): "379711332"