QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#398313#4893. Imbalancejinqihao20230 331ms8960kbC++145.2kb2024-04-25 10:45:272024-04-25 10:45:27

Judging History

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

  • [2024-04-25 10:45:27]
  • 评测
  • 测评结果:0
  • 用时:331ms
  • 内存:8960kb
  • [2024-04-25 10:45:27]
  • 提交

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<<19)+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<=20)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: 0ms
memory: 8960kb

input:

2 2 0

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: -10
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: 0
Time Limit Exceeded

Test #137:

score: 20
Accepted
time: 331ms
memory: 8640kb

input:

114 20 0

output:

849724285

result:

ok 1 number(s): "849724285"

Test #138:

score: -20
Time Limit Exceeded

input:

114 22 0

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #2:

0%