QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#829851#9777. Balance in All ThingsKevin5307AC ✓5223ms498144kbC++234.4kb2024-12-24 14:10:392024-12-24 14:10:45

Judging History

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

  • [2024-12-24 14:10:45]
  • 评测
  • 测评结果:AC
  • 用时:5223ms
  • 内存:498144kb
  • [2024-12-24 14:10:39]
  • 提交

answer

//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
using LL=ll;
int p;
struct Mod
{
	LL m, p;
	void init(int pp) { m = ((__int128)1 << 64) / pp; p = pp; }
	LL operator ()(LL x)
	{
		x=x - ((__int128(x) * m) >> 64) * p;
		if(x<0) x+=p;
		return x;
	}
} mod;
int n,k;
int S1;
int S2;
int A[1000100],B[1000100],C[1000100],D[1000100];
int f[444],g[1000100];
ll tr1[402][82000];
ll tr2[402][82000];
int ipw2[1001000];
ll Binom[1002][1002];
ll fact[1002],rfact[1002];
ll ksm(ll a,ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1) ans=mod(ans*a);
		b>>=1;
		a=mod(a*a);
	}
	return ans;
}
ll Coef[1002][1002];
ll Chk[1002][1002];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>k>>p;
	mod.init(p);
	for(int i=0;i<1002;i++)
		Binom[i][i]=Binom[i][0]=1;
	for(int i=2;i<1002;i++)
		for(int j=1;j<i;j++)
			Binom[i][j]=mod(Binom[i-1][j-1]+Binom[i-1][j]);
	ipw2[0]=1;
	for(int i=1;i<1001000;i++)
		ipw2[i]=mod(1ll*ipw2[i-1]*(p+1)/2);
	fact[0]=1;
	for(int i=1;i<1002;i++)
		fact[i]=mod(fact[i-1]*i);
	for(int i=0;i<1002;i++)
		for(int j=0;j+j<=i;j++)
			Coef[i][j]=mod(mod(mod(Binom[i][j]*Binom[i-j][j])*fact[j])*ipw2[j]);
	for(int i=0;i<1002;i++)
		for(int j=0;j<=i;j++)
			Chk[i][j]=mod(Binom[i][j]*fact[j]);
	S1=n+1;
	for(int i=0;i<=n+n;i++)
		for(int j=0;j<=n+n-i;j++)
			for(int k=0;k<=n+n-i-j;k++)
			{
				int l=n+n-i-j-k;
				if(-3*i-j+k+3*l==0)
				{
					A[S2]=i;
					B[S2]=j;
					C[S2]=k;
					D[S2]=l;
					S2++;
				}
			}
	for(int i=0;i<=n;i++)
		for(int j=0;j<S2;j++)
		{
			int x=i,y=n+n-i-i,z=i;
			int a=::A[j],b=::B[j],c=::C[j],d=::D[j];
			int A=a;
			int B=x-a;
			int C=b-B;
			int F=d;
			int E=z-d;
			int D=c-E;
			if(A<0||B<0||C<0||D<0||E<0||F<0) continue;
			ll SS1=0,SS2=0;
			if(B+D+F!=A+C+E) continue;
			if(A+C-B<0) continue;
			for(int v1=0;v1<=min(B,C);v1++)
			{
				ll coef=Coef[b][v1];
				int bp=b-v1-v1;
				coef=mod(coef*Binom[bp][B-v1]);
				coef=mod(coef*Chk[A][B-v1]);
				SS1+=coef;
			}
			for(int v2=0;v2<=min(D,E);v2++)
			{
				ll coef=Coef[c][v2];
				int cp=c-v2-v2;
				coef=mod(coef*Binom[cp][D-v2]);
				coef=mod(coef*Chk[A+C-B][D-v2]);
				SS2+=coef;
			}
			SS1=mod(SS1);
			SS1=mod(SS1*fact[F]);
			SS2=mod(SS2);
			tr1[i][j]=mod(SS1*SS2);
		}
	for(int i=0;i<=n;i++)
		for(int j=0;j<S2;j++)
		{
			tr2[i][j]=1;
			int X=i,Y=n+n-i-i,Z=i;
			int A=::A[j],B=::B[j],C=::C[j],D=::D[j];
			if(A+A>X||D+D>Z||B<A||C<D)
			{
				tr2[i][j]=0;
				continue;
			}
			tr2[i][j]=mod(1ll*Binom[X][A]*Binom[Z][D]);
			tr2[i][j]=mod(mod(1ll*Binom[X-A][A]*tr2[i][j])*Binom[Z-D][D]);
			tr2[i][j]=mod(1ll*tr2[i][j]*ipw2[A+D]);
			tr2[i][j]=mod(mod(1ll*tr2[i][j]*fact[A])*fact[D]);
			X-=A*2;
			Z-=D*2;
			B-=A;
			C-=D;
			int V1=X;
			int V2=Z;
			if(V1>B||V2>C)
			{
				tr2[i][j]=0;
				continue;
			}
			int V3=B-V1;
			int V4=C-V2;
			if(V3+V4!=Y)
			{
				tr2[i][j]=0;
				continue;
			}
			ll ways=0;
			for(int i=0;i<=min(V3,V4);i++) if(V3-i<=V1&&V4-i<=V2&&(V1-V3+i)==(V2-V4+i))
			{
				ll coef=mod(Coef[Y][i]);
				coef=mod(coef*fact[V1-V3+i]);
				coef=mod(coef*Binom[Y-i-i][V3-i]);
				coef=mod(coef*Chk[V1][V3-i]);
				coef=mod(coef*Chk[V2][V4-i]);
				ways+=coef;
			}
			ways=mod(ways);
			tr2[i][j]=mod(tr2[i][j]*ways);
		}
	f[0]=1;
	for(int i=1;i<=k;i++)
		if(i&1)
		{
			memset(g,0,sizeof(g));
			for(int a=0;a<=n;a++) if(f[a])
				for(int b=0;b<S2;b++)
					g[b]=mod(g[b]+1ll*f[a]*tr2[a][b]);
		}
		else
		{
			memset(f,0,sizeof(f));
			for(int b=0;b<S2;b++) if(g[b])
				for(int a=0;a<=n;a++)
					f[a]=mod(f[a]+1ll*g[b]*tr1[a][b]);
		}
	if(k&1)
	{
		LL ans=accumulate(g,g+S2,0ll)%p;
		cout<<ans<<'\n';
	}
	else
	{
		LL ans=accumulate(f,f+n+1,0ll)%p;
		cout<<ans<<'\n';
	}
	return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 49356kb

input:

3 1 1000000007

output:

15

result:

ok single line: '15'

Test #2:

score: 0
Accepted
time: 25ms
memory: 91588kb

input:

100 3 1000000007

output:

894710378

result:

ok single line: '894710378'

Test #3:

score: 0
Accepted
time: 6ms
memory: 54828kb

input:

6 6 1000000007

output:

103387851

result:

ok single line: '103387851'

Test #4:

score: 0
Accepted
time: 11ms
memory: 49592kb

input:

2 6 998244353

output:

729

result:

ok single line: '729'

Test #5:

score: 0
Accepted
time: 5223ms
memory: 461724kb

input:

400 20 713862469

output:

561801910

result:

ok single line: '561801910'

Test #6:

score: 0
Accepted
time: 4913ms
memory: 461628kb

input:

397 17 861815723

output:

764050494

result:

ok single line: '764050494'

Test #7:

score: 0
Accepted
time: 4660ms
memory: 447860kb

input:

391 14 189546607

output:

131011086

result:

ok single line: '131011086'

Test #8:

score: 0
Accepted
time: 4064ms
memory: 427292kb

input:

382 11 922402597

output:

58290636

result:

ok single line: '58290636'

Test #9:

score: 0
Accepted
time: 3569ms
memory: 396176kb

input:

370 8 911997731

output:

823449745

result:

ok single line: '823449745'

Test #10:

score: 0
Accepted
time: 2951ms
memory: 360464kb

input:

355 5 708230279

output:

173013324

result:

ok single line: '173013324'

Test #11:

score: 0
Accepted
time: 2704ms
memory: 326096kb

input:

337 19 742344989

output:

592413780

result:

ok single line: '592413780'

Test #12:

score: 0
Accepted
time: 2552ms
memory: 318088kb

input:

334 16 816423319

output:

567577272

result:

ok single line: '567577272'

Test #13:

score: 0
Accepted
time: 2335ms
memory: 300068kb

input:

328 13 463459417

output:

77314092

result:

ok single line: '77314092'

Test #14:

score: 0
Accepted
time: 2129ms
memory: 295780kb

input:

319 10 469180903

output:

94351928

result:

ok single line: '94351928'

Test #15:

score: 0
Accepted
time: 1800ms
memory: 293988kb

input:

307 7 240226001

output:

147458009

result:

ok single line: '147458009'

Test #16:

score: 0
Accepted
time: 1400ms
memory: 269576kb

input:

292 4 725635439

output:

275908993

result:

ok single line: '275908993'

Test #17:

score: 0
Accepted
time: 1229ms
memory: 251276kb

input:

274 18 518962097

output:

200937333

result:

ok single line: '200937333'

Test #18:

score: 0
Accepted
time: 1161ms
memory: 245432kb

input:

271 15 765905797

output:

683367155

result:

ok single line: '683367155'

Test #19:

score: 0
Accepted
time: 1003ms
memory: 240432kb

input:

265 12 240076537

output:

6822852

result:

ok single line: '6822852'

Test #20:

score: 0
Accepted
time: 912ms
memory: 230124kb

input:

256 9 319057337

output:

9076486

result:

ok single line: '9076486'

Test #21:

score: 0
Accepted
time: 709ms
memory: 208348kb

input:

244 6 257631131

output:

53842743

result:

ok single line: '53842743'

Test #22:

score: 0
Accepted
time: 546ms
memory: 206048kb

input:

229 3 953863681

output:

247424565

result:

ok single line: '247424565'

Test #23:

score: 0
Accepted
time: 7ms
memory: 45724kb

input:

1 20 998244353

output:

1

result:

ok single line: '1'

Test #24:

score: 0
Accepted
time: 8ms
memory: 55668kb

input:

8 4 998244353

output:

740255777

result:

ok single line: '740255777'

Test #25:

score: 0
Accepted
time: 8ms
memory: 50736kb

input:

5 20 998244353

output:

725931122

result:

ok single line: '725931122'

Test #26:

score: 0
Accepted
time: 4660ms
memory: 498144kb

input:

400 1 1000000007

output:

705610633

result:

ok single line: '705610633'

Test #27:

score: 0
Accepted
time: 4672ms
memory: 497028kb

input:

400 2 1000000007

output:

917456162

result:

ok single line: '917456162'

Test #28:

score: 0
Accepted
time: 14ms
memory: 46796kb

input:

1 1 1000000007

output:

1

result:

ok single line: '1'

Extra Test:

score: 0
Extra Test Passed