QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#398315 | #4893. Imbalance | jinqihao2023 | 20 | 1895ms | 20496kb | C++14 | 5.2kb | 2024-04-25 10:45:58 | 2024-04-25 10:45:59 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
int n,k,m;
string s,t;
int power(int a,int b){int res=1;for(;b;b>>=1){if(b&1)res=1ll*res*a%mod;a=1ll*a*a%mod;}return res;}
namespace work1
{
const int N=125,M=(1<<21)+5;
int f[2][M];
void add(int &x,int y){x+=y;if(x>=mod)x-=mod;}
void solve()
{
memset(f,0,sizeof(f));
int now=0;
for(int i=1;i<=m;i++)now|=((s[i]-'0')<<m-i);
f[m&1][now]=1;
int all=(1<<k-1)-1;
for(int i=m;i<n;i++)
{
memset(f[i+1&1],0,sizeof(f[i+1&1]));
for(int j=0;j<(1<<k-1);j++)if(f[i&1][j])
{
for(int l=0;l<2;l++)
{
int now=(j<<1)|l;
if(i>=k-1 && __builtin_popcount(now)==k/2)continue;
add(f[i+1&1][now&all],f[i&1][j]);
}
}
}
int ans=0;
for(int i=0;i<(1<<k-1);i++)add(ans,f[n&1][i]);
printf("%d\n",ans);
}
}
int ct,ctt;
namespace work2
{
const int N=125;
int num,st[N],ed[N],stx,sty,ans;
int f[N][N][N],g[N][N][N],stt[N][N],edd[N][N],a[N][N];
void sp(int x,int y){for(int i=0;i<=num;i++)swap(a[x][i],a[y][i]);}
inline void add(int &x,int y){x+=y;if(x>=mod)x-=mod;}
inline int md(int x){if(x>=mod)return x-mod;return x;}
int gauss()
{
int ans=1;
for(int i=0;i<=num;i++)
{
if(!a[i][i])for(int j=i+1;j<=num;j++)if(a[j][i]){sp(i,j),ans=md(mod-ans);break;}
if(!a[i][i])return 0;ans=1ll*ans*a[i][i]%mod;
int temp=power(a[i][i],mod-2);
for(int j=i+1;j<=num;j++)
{
int now=1ll*a[j][i]*temp%mod;
for(int l=i;l<=num;l++)a[j][l]=(a[j][l]-1ll*a[i][l]*now%mod+mod)%mod;
}
}
return ans;
}
bool vis[N][N];
void dfs1(int step)
{
if(step==num+1)
{
int mx=0;
for(int i=0;i<=num;i++)mx=max(mx,ed[i]);
// if(ct%10000==0)cout<<ct<<endl;
for(int i=0;i<=num;i++)for(int j=0;j<=num;j++)a[i][j]=0;
int edx=0,edy=0;
edx=(n%k==0?k:n%k),edy=ed[num];
for(int i=1;i<=num;i++)st[i]=ed[i-1]-k/2;
for(int i=1;i<num;i++)if(st[i]>ed[i] || ed[i]-st[i]>k)return ;
if(sty>ed[0])return ;if(st[num]>edy)return ;
if(ed[0]-sty>k-stx)return ;if(edy-st[num]>edx)return ;
// printf("st:(%d,%d) ",stx,sty);
// for(int i=1;i<=num;i++)printf("(%d,%d) ",0,st[i]);printf("\n");
// printf("ed:");
// for(int i=0;i<num;i++)printf("(%d,%d) ",k,ed[i]);printf("(%d,%d) \n",edx,edy);
for(int j=0;j<=k;j++)for(int l=0;l<=mx;l++)edd[j][l]=0;
if(!vis[edx][edy])edd[edx][edy]=1;
for(int j=edx;j>=0;j--)for(int l=edy;l>=0;l--)if(edd[j][l])
{
if(j>0)add(edd[j-1][l],edd[j][l]);
if(j>0 && l>0)add(edd[j-1][l-1],edd[j][l]);
}
for(int i=1;i<=num;i++)for(int j=0;j<num;j++)if(st[i]<=ed[j])
{
int res=f[st[i]][k][ed[j]];
for(int l=0;l<edx;l++)res=(res-1ll*f[st[i]][l][edy]*g[ed[j]][l+1][edy+1]%mod+mod)%mod;
a[i][j]=res;
}else a[i][j]=0;
for(int j=0;j<num;j++)if(sty<=ed[j])
{
int res=stt[k][ed[j]];
for(int l=0;l<edx;l++)res=(res-1ll*stt[l][edy]*g[ed[j]][l+1][edy+1]%mod+mod)%mod;
a[0][j]=res;
}else a[0][j]=0;
for(int i=1;i<=num;i++)if(st[i]<=edy)
{
int res=edd[0][st[i]];
a[i][num]=res;
}
a[0][num]=edd[stx][sty];
// int now=ans;
ans=(ans+gauss())%mod;
// ct++;
// if(ans!=now)ctt++;
return ;
}
if(!step)for(int i=k/2+1;i<=k;i++)ed[step]=i,dfs1(step+1),ed[step]=0;
else if(step<num)for(int i=ed[step-1]+1;i<=ed[step-1]+k/2;i++)ed[step]=i,dfs1(step+1),ed[step]=0;
else for(int i=ed[step-1]-k/2;i<=ed[step-1]+k/2;i++)ed[step]=i,dfs1(step+1),ed[step]=0;
}
void init()
{
for(int i=0;i<=k;i++)for(int j=0;j<=n;j++)vis[i][j]=0;
for(int i=0;i<=n;i++)for(int j=0;j<=k;j++)for(int l=0;l<=n;l++)f[i][j][l]=g[i][j][l]=0;
int ax=0,ay=0;
for(int i=1;i<=m;i++)
{
vis[ax][ay]=1;
if(s[i]=='1')ax++,ay++;
else ax++;
}
stx=ax,sty=ay;
for(int i=0;i<=n;i++)
{
if(vis[0][i])continue;
f[i][0][i]=1;
for(int j=0;j<=k;j++)for(int l=0;l<=n;l++)if(f[i][j][l])
{
if(j<k && !vis[j+1][l])f[i][j+1][l]=(f[i][j+1][l]+f[i][j][l])%mod;
if(j<k && l<n && !vis[j+1][l+1])f[i][j+1][l+1]=(f[i][j+1][l+1]+f[i][j][l])%mod;
}
}
for(int i=0;i<=n;i++)
{
if(vis[k][i])continue;
g[i][k][i]=1;
for(int j=k;j>=0;j--)for(int l=n;l>=0;l--)if(g[i][j][l])
{
if(j>0 && !vis[j-1][l])g[i][j-1][l]=(g[i][j-1][l]+g[i][j][l])%mod;
if(j>0 && l>0 && !vis[j-1][l-1])g[i][j-1][l-1]=(g[i][j-1][l-1]+g[i][j][l])%mod;
}
}
for(int i=0;i<=k;i++)for(int j=0;j<=n;j++)stt[i][j]=0;
if(!vis[stx][sty])stt[stx][sty]=1;
for(int j=0;j<=k;j++)for(int l=0;l<=n;l++)if(stt[j][l])
{
if(j<k)stt[j+1][l]=(stt[j+1][l]+stt[j][l])%mod;
if(j<k && l<n)stt[j+1][l+1]=(stt[j+1][l+1]+stt[j][l])%mod;
}
}
void solve()
{
num=(n-1)/k;
// cout<<num<<endl;
init();
dfs1(0);
s=t,stx=sty=0;
for(int i=0;i<=num;i++)st[i]=ed[i]=0;
init();
dfs1(0);
printf("%d\n",ans);
}
}
int main()
{
// int time_st=clock();
// freopen("imbalance.in","r",stdin);
// freopen("imbalance.out","w",stdout);
scanf("%d %d %d",&n,&k,&m);
assert(m==0);
cin>>s,t=s,reverse(s.begin(),s.end()),s=' '+s,t=' '+t;
// work2::solve();
if(k<=22)work1::solve();
else work2::solve();
// cerr<<(clock()-time_st)/1e6<<endl;
// cerr<<ct<<endl;
// cerr<<ctt<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 10
Accepted
time: 8ms
memory: 20200kb
input:
2 2 0
output:
2
result:
ok 1 number(s): "2"
Test #2:
score: 0
Runtime Error
input:
2 2 1 0
output:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #1:
0%
Subtask #4:
score: 20
Accepted
Test #137:
score: 20
Accepted
time: 372ms
memory: 20496kb
input:
114 20 0
output:
849724285
result:
ok 1 number(s): "849724285"
Test #138:
score: 20
Accepted
time: 1299ms
memory: 20208kb
input:
114 22 0
output:
918046462
result:
ok 1 number(s): "918046462"
Test #139:
score: 20
Accepted
time: 1895ms
memory: 7564kb
input:
114 24 0
output:
471169566
result:
ok 1 number(s): "471169566"
Test #140:
score: 20
Accepted
time: 1068ms
memory: 8092kb
input:
114 26 0
output:
540055361
result:
ok 1 number(s): "540055361"
Test #141:
score: 20
Accepted
time: 299ms
memory: 8316kb
input:
114 28 0
output:
997530597
result:
ok 1 number(s): "997530597"
Test #142:
score: 20
Accepted
time: 408ms
memory: 8252kb
input:
114 30 0
output:
37439521
result:
ok 1 number(s): "37439521"
Test #143:
score: 20
Accepted
time: 308ms
memory: 8780kb
input:
114 32 0
output:
448438493
result:
ok 1 number(s): "448438493"
Test #144:
score: 20
Accepted
time: 362ms
memory: 20256kb
input:
113 20 0
output:
942733157
result:
ok 1 number(s): "942733157"
Test #145:
score: 20
Accepted
time: 1307ms
memory: 20484kb
input:
113 22 0
output:
547536565
result:
ok 1 number(s): "547536565"
Test #146:
score: 20
Accepted
time: 1720ms
memory: 7576kb
input:
113 24 0
output:
219952878
result:
ok 1 number(s): "219952878"
Test #147:
score: 20
Accepted
time: 923ms
memory: 7768kb
input:
113 26 0
output:
763274765
result:
ok 1 number(s): "763274765"
Test #148:
score: 20
Accepted
time: 204ms
memory: 8288kb
input:
113 28 0
output:
910952876
result:
ok 1 number(s): "910952876"
Test #149:
score: 20
Accepted
time: 385ms
memory: 8208kb
input:
113 30 0
output:
968408969
result:
ok 1 number(s): "968408969"
Test #150:
score: 20
Accepted
time: 280ms
memory: 8452kb
input:
113 32 0
output:
118567934
result:
ok 1 number(s): "118567934"
Test #151:
score: 20
Accepted
time: 362ms
memory: 20192kb
input:
112 20 0
output:
275087743
result:
ok 1 number(s): "275087743"
Test #152:
score: 20
Accepted
time: 1296ms
memory: 20496kb
input:
112 22 0
output:
185644824
result:
ok 1 number(s): "185644824"
Test #153:
score: 20
Accepted
time: 1555ms
memory: 7804kb
input:
112 24 0
output:
557785519
result:
ok 1 number(s): "557785519"
Test #154:
score: 20
Accepted
time: 794ms
memory: 7992kb
input:
112 26 0
output:
522996775
result:
ok 1 number(s): "522996775"
Test #155:
score: 20
Accepted
time: 446ms
memory: 8252kb
input:
112 28 0
output:
134122652
result:
ok 1 number(s): "134122652"
Test #156:
score: 20
Accepted
time: 353ms
memory: 8212kb
input:
112 30 0
output:
502459554
result:
ok 1 number(s): "502459554"
Test #157:
score: 20
Accepted
time: 249ms
memory: 8356kb
input:
112 32 0
output:
169309797
result:
ok 1 number(s): "169309797"
Test #158:
score: 20
Accepted
time: 355ms
memory: 20192kb
input:
111 20 0
output:
360310827
result:
ok 1 number(s): "360310827"
Test #159:
score: 20
Accepted
time: 1259ms
memory: 20268kb
input:
111 22 0
output:
516490684
result:
ok 1 number(s): "516490684"
Test #160:
score: 20
Accepted
time: 1395ms
memory: 7420kb
input:
111 24 0
output:
501679698
result:
ok 1 number(s): "501679698"
Test #161:
score: 20
Accepted
time: 677ms
memory: 7772kb
input:
111 26 0
output:
43788136
result:
ok 1 number(s): "43788136"
Test #162:
score: 20
Accepted
time: 429ms
memory: 7904kb
input:
111 28 0
output:
5764962
result:
ok 1 number(s): "5764962"
Test #163:
score: 20
Accepted
time: 332ms
memory: 8144kb
input:
111 30 0
output:
918617250
result:
ok 1 number(s): "918617250"
Test #164:
score: 20
Accepted
time: 228ms
memory: 8396kb
input:
111 32 0
output:
982496307
result:
ok 1 number(s): "982496307"
Test #165:
score: 20
Accepted
time: 14ms
memory: 17832kb
input:
114 114 0
output:
321821768
result:
ok 1 number(s): "321821768"
Test #166:
score: 20
Accepted
time: 28ms
memory: 10528kb
input:
114 50 0
output:
860957763
result:
ok 1 number(s): "860957763"
Test #167:
score: 20
Accepted
time: 30ms
memory: 10460kb
input:
113 50 0
output:
307614098
result:
ok 1 number(s): "307614098"
Test #168:
score: 20
Accepted
time: 34ms
memory: 20196kb
input:
110 10 0
output:
615608372
result:
ok 1 number(s): "615608372"
Test #169:
score: 20
Accepted
time: 11ms
memory: 9776kb
input:
100 50 0
output:
475715516
result:
ok 1 number(s): "475715516"
Test #170:
score: 20
Accepted
time: 13ms
memory: 13744kb
input:
111 78 0
output:
617855013
result:
ok 1 number(s): "617855013"
Test #171:
score: 20
Accepted
time: 213ms
memory: 7292kb
input:
100 26 0
output:
960228335
result:
ok 1 number(s): "960228335"
Test #172:
score: 20
Accepted
time: 145ms
memory: 7536kb
input:
99 28 0
output:
17612739
result:
ok 1 number(s): "17612739"
Test #173:
score: 20
Accepted
time: 300ms
memory: 7824kb
input:
107 28 0
output:
462764365
result:
ok 1 number(s): "462764365"
Subtask #5:
score: 0
Skipped
Dependency #2:
0%