QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#736817#9631. Median ReplacementDelovWA 1ms4424kbC++143.5kb2024-11-12 13:31:172024-11-12 13:31:18

Judging History

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

  • [2024-11-12 13:31:18]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:4424kb
  • [2024-11-12 13:31:17]
  • 提交

answer

#include <bits/stdc++.h>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
#define Rep(i,a,b) for(int i=a;i<=b;++i)
#define Dwn(i,a,b) for(int i=a;i>=b;--i)

const int maxn=3e2+10,Mod=1e9+7;

int pw(int x,int p){ int res=1,base=x;while(p){ if(p&1)res=1LL*res*base%Mod;p>>=1;base=1LL*base*base%Mod; }return res; }
int Inv(int x){ return pw(x,Mod-2); }

int n;
int L[maxn],R[maxn];
vector<int>vec;

int f[2][2][maxn],g[2][2][maxn];
int h[2];
int deg;
int S[maxn];

void Mul(int *f,int *g){
	Rep(i,0,deg)(f[i]) && (g[i]=(g[i]+1LL*f[i]*h[0])%Mod);
}

void Dec(int *f,int *g,const int k){
	Rep(i,0,deg)(f[i]) && (g[i]=(g[i]+1LL*f[i]*k)%Mod);
	Rep(i,0,deg)(f[i]) && (g[i+1]=(g[i+1]-f[i])%Mod);
}

void Add(int *f,int *g,const int k){
	Rep(i,0,deg)(f[i]) && (g[i]=(g[i]+1LL*f[i]*k)%Mod);
	Rep(i,0,deg)(f[i]) && (g[i+1]=(g[i+1]+f[i])%Mod);
}

int ps[maxn][maxn],sum[maxn][maxn];
int fac[maxn],inv[maxn];
int pre[maxn],suf[maxn];

void Init(const int n=302){
	fac[0]=inv[0]=1;Rep(i,1,n)fac[i]=1LL*fac[i-1]*i%Mod;
	inv[n]=Inv(fac[n]);Dwn(i,n-1,1)inv[i]=1LL*inv[i+1]*(i+1)%Mod;
	Rep(i,1,n){
		ps[i][0]=1;
		Rep(j,1,n)ps[i][j]=1LL*ps[i][j-1]*i%Mod;
	}
	Rep(i,1,n)Rep(j,1,n)sum[i][j]=(sum[i][j-1]+ps[j][i])%Mod;
}

int Lag(int n,int K){
	if(K==0)return n;
	if(n<=300)return sum[K][n];
	pre[0]=suf[K+2]=1;
	Rep(i,1,K+1)pre[i]=1LL*pre[i-1]*(n-i)%Mod;
	Dwn(i,1,K+1)suf[i]=1LL*suf[i+1]*(n-i)%Mod;
	int res=0;
	Rep(i,1,K+1)
		res=(res
			+ (((n-i)&1) ? -1LL : 1LL)
			* sum[K][i]*pre[i-1]%Mod*suf[i+1]
		)%Mod;
	return res;
}

int F[2][2],G[2][2],All;
int Calc(int m){
	memset(F,0,sizeof(F));
	memset(G,0,sizeof(G));
	F[0][0]=1;
	Rep(i,1,n){
		Rep(x,0,1)Rep(y,0,1)if(x+y<2){
			if(m>R[i])G[y][0]=(G[y][0]+1LL*F[x][y]*(R[i]-L[i]+1))%Mod;
			else if(m<L[i]){
				if(x+y+1<2)G[y][1]=(G[y][1]+1LL*F[x][y]*(R[i]-L[i]+1))%Mod;
			}else{
				G[y][0]=(G[y][0]+1LL*F[x][y]*(m-L[i]))%Mod;
				if(x+y+1<2)G[y][1]=(G[y][1]+1LL*F[x][y]*(R[i]-m+1))%Mod;
			}
		}
		Rep(x,0,1)Rep(y,0,1)F[x][y]=G[x][y],G[x][y]=0;
	}
	return (0LL + All -F[0][0]-F[0][1]-F[1][0])%Mod;
}

void solve(){
	cin>>n;vec.resize(0);
	Rep(i,1,n)cin>>L[i],vec.emplace_back(L[i]);
	Rep(i,1,n)cin>>R[i],vec.emplace_back(R[i]);
	sort(vec.begin(),vec.end());
	vec.erase(unique(vec.begin(),vec.end()),vec.end());
	deg=vec.size();
	int Ans=0;memset(f,0,sizeof(f)),memset(g,0,sizeof(g));
	All=1;Rep(i,1,n)All=1LL*All*(R[i]-L[i]+1)%Mod;
	f[0][0][0]=1;
	for(int t=0;t+1<vec.size();++t){
		int l=vec[t]+1,r=vec[t+1]-1;
		if(l>r)continue;
		h[0]=0,h[1]=0;
		Rep(i,1,n){
			if(L[i]>r){ h[0]=R[i]-L[i]+1;Mul(f[0][0],g[0][1]); }

			else if(R[i]<l){ h[0]=R[i]-L[i]+1;Rep(x,0,1)Rep(y,0,1)if(x+y<2)Mul(f[x][y],g[y][0]); }
			else {
				Dec(f[0][0],g[0][1],R[i]+1);
				Rep(x,0,1)Rep(y,0,1)if(x+y<2)Add(f[x][y],g[y][0],-L[i]);
			}
			Rep(x,0,1)Rep(y,0,1)if(x+y<2)
				Rep(i,0,deg)f[x][y][i]=g[x][y][i],g[x][y][i]=0;
		}Rep(i,0,deg)S[i]=0;
		Rep(x,0,1)Rep(y,0,1)if(x+y<2)Rep(i,0,deg)S[i]=(S[i]+f[x][y][i])%Mod;
		--r;
		if(l<=r)
			Rep(i,0,deg)if(S[i]){
				int A=(Lag(r,i+1)-Lag(l-1,i+1))%Mod,B=(Lag(r+1,i+1)-Lag(l,i+1))%Mod;
				int C=(Lag(r,i)-Lag(l-1,i))%Mod;
				Ans=(Ans-1LL*S[i]*A%Mod-1LL*S[i]*C%Mod+1LL*B*S[i])%Mod;
			}
		++r;
		Ans=(Ans+1LL*(Calc(r)-Calc(r+1))*r)%Mod;
	}
	for(auto it : vec)Ans=(Ans+1LL*(Calc(it)-Calc(it+1))*it)%Mod;;
	cout<<Ans<<"\n";
}

int tex;
int main (){ ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);Init();cin>>tex;while(tex--)solve(); }


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 4424kb

input:

10
5
5 1 4 3 2
14 2 5 3 2
5
4 5 1 2 3
13 7 1 2 3
5
5 2 5 3 1
10 2 12 3 2
5
5 5 3 1 5
57 5 3 1 5
5
2 2 3 3 5
4 5 4 4 5
5
4 5 3 5 3
13 7 3 5 3
5
5 1 4 2 3
14 3 4 2 3
5
1 2 5 4 5
2 8 5 7 5
5
1 1 3 5 1
8 2 3 8 1
5
4 4 4 2 3
5 10 5 2 3

output:

180
2390
280
265
182
2393
120
296
3776
131

result:

wrong answer 2nd lines differ - expected: '170', found: '2390'