QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371627#7838. 往日之影NATURAL60 0ms0kbC++172.3kb2024-03-30 14:33:222024-03-30 14:34:32

Judging History

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

  • [2024-03-30 14:34:32]
  • 评测
  • 测评结果:0
  • 用时:0ms
  • 内存:0kb
  • [2024-03-30 14:33:22]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
inline int qread()
{
	int a=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch)){(a*=10)+=(ch^48);ch=getchar();}
	return a*f;
}
int T,mod,n,a[4],cnt,b[4],op;
inline int fmod(int x)
{
	if(x>=mod)x-=mod;
	return x;
}
struct node
{
	int a,b;
}ans,an;
inline node operator +(node x,node y){return (node){fmod(x.a+y.a),fmod(x.b+y.b)};}
inline node operator -(node x,node y){return (node){fmod(x.a-y.a+mod),fmod(x.b-y.b+mod)};}
inline node operator *(node x,node y){return (node){(1ll*x.a*y.a-1ll*x.b*y.b%mod+mod)%mod,(1ll*x.a*y.b+1ll*x.b*y.a)%mod};}
inline int qpow(long long x,long long y)
{
	long long cx=1;
	while(y)
	{
		(y&1)&&((cx*=x)%=mod);
		(x*=x)%=mod;
		y>>=1;
	}
	return cx;
}
inline node qpow(node x,long long y)
{
	node cx=(node){1,0};
	while(y)
	{
		if(y&1)cx=cx*x;
		x=x*x;
		y>>=1;
	}
	return cx;
}
inline node get_w(int x)
{
	if(x==0)return (node){1,0};
	if(x==1)return (node){0,1};
	if(x==2)return (node){mod-1,0};
	if(x==3)return (node){0,mod-1};
	return (node){0,0};
}
inline void dfs(int h,int sz,int pw)
{
	if(h==4)
	{
		pw=4-pw;
		an=get_w(pw)*(node){sz,0};
		for(int i=0;i<=3;++i)an=an*qpow(get_w(i*2),1ll*b[i]*(b[i]-1)/2);
		for(int i=0;i<=3;++i)for(int j=i+1;j<=3;++j)an=an*qpow(get_w(i+j),1ll*b[i]*b[j]);
		return ans=ans+an,void();
	}
	b[op]+=a[h];dfs(h+1,sz,(pw+op*a[h]*h)&3);b[op]-=a[h];
	if(a[h]&&!b[1])b[op]+=a[h]-1,++b[1];dfs(h+1,1ll*cnt*a[h]%mod,(pw+op*(a[h]-1)*h+1)&3);--b[1],b[op]-=a[h]-1;
	if(a[h]&&!b[3])b[op]+=a[h]-1,++b[3];dfs(h+1,1ll*cnt*a[h]%mod,(pw+op*(a[h]-1)*h+3)&3);--b[3],b[op]-=a[h]-1;
	if(a[h]>=2&&!b[1]&&!b[3])b[op]+=a[h]-2,++b[1],++b[3];dfs(h+1,1ll*cnt*a[h]%mod*(a[h]-1)%mod,(pw+op*(a[h]-2)*h)&3);--b[1],--b[3],b[op]-=a[h]-2;
	return ;
}
int main()
{
	T=qread(),mod=qread();
	while(T--)
	{
		n=qread();
		for(int i=0;i<=3;++i)a[i]=qread();
		if(!n)puts("0");
		else if(n==1)
		{
			if(a[0])puts("1");
			else puts("0");
		}
		else if(n==2)
		{
			if(a[0]==2||a[1]==2)printf("%d\n",qpow(2,mod-2));
			else puts("0");
		}
		else
		{
			cnt=qpow(qpow(2,(1ll*n*(n-1)/2))*qpow(4,n)%mod,mod-2);
			op=0;b[0]=b[1]=b[2]=b[3]=0;dfs(0,1,0);
			op=2;b[0]=b[1]=b[2]=b[3]=0;dfs(0,1,0);
			printf("%lld\n",1ll*ans.a*cnt%mod);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

1 998244353
3
1 1 0 1

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Test #23:

score: 0
Time Limit Exceeded

input:

999 999999001
2
2 0 0 0
3
3 0 0 0
4
4 0 0 0
5
5 0 0 0
6
6 0 0 0
7
7 0 0 0
8
8 0 0 0
9
9 0 0 0
10
10 0 0 0
11
11 0 0 0
12
12 0 0 0
13
13 0 0 0
14
14 0 0 0
15
15 0 0 0
16
16 0 0 0
17
17 0 0 0
18
18 0 0 0
19
19 0 0 0
20
20 0 0 0
21
21 0 0 0
22
22 0 0 0
23
23 0 0 0
24
24 0 0 0
25
25 0 0 0
26
26 0 0 0
27...

output:


result:


Subtask #5:

score: 0
Skipped

Dependency #1:

0%