跳到正文

题解

题解:P13873 [蓝桥杯 2024 省 Java/Python A] 智力测试

_cout_约 4 分钟
原文
文章信息与版本

文章信息

收录
2026年6月18日
更新
2026年6月23日
篇幅
约 4 分钟
获赞
1
题目
P13873

版本

  1. v2当前
  2. v1

题解

点击跳转题意

注:下文中 aa、bb 数组表示离散化后的 RR、CC 数组。

看到题面就很不舒服,于是我们转化题意为,你可以从 (x,y)(x,y),跳到 (x′,y)(x',y),满足 ax′=ax+1a_{x'}=a_x+1,从 (x,y)(x,y),跳到 (x,y′)(x,y') 同理。

那么就是随便做了,我们设两者的 aa、bb 差值分别为 xx 、yy,答案就是

(x+yx)×(powaatr−1×powaasr−1)×(powbbtc−1×powbbsc−1)\binom{x+y}{x} \times \left(powa_{a_{t_r}-1} \times powa_{a_{s_r}}^{-1}\right) \times \left(powb_{b_{t_c}-1} \times powb_{b_{s_c}}^{-1}\right)

其中 powapowa、powbpowb 为 aa、bb 前缀乘积。

C++
#include <bits/stdc++.h>
#define int long long
#define N 400005
using namespace std;
const int mod=1e9+7;
int c[N],a[N],b[N],fac[N],sq[N],num1[N],num2[N],pow1[N],pow2[N],cn[N];
int qpow(int x,int y)
{
	int res=1;
	while(y)
	{
		if(y&1)
			res=res*x%mod;
		y>>=1;
		x=x*x%mod;
	}
	return res;
}
void init(int n,int m)
{
	int cnt=0;
	for(int i=1;i<=n;i++)
		c[++cnt]=a[i];
	sort(c+1,c+cnt+1);
	int res=0;
	for(int i=1;i<=cnt;i++)
	{
		if(c[i]==c[i-1])
			cn[i]=cn[i-1];
		else
			cn[i]=++res;
	}
	for(int i=1;i<=n;i++)
		a[i]=cn[lower_bound(c+1,c+cnt+1,a[i])-c];
	cnt=0;
	for(int i=1;i<=m;i++)
		c[++cnt]=b[i];
	sort(c+1,c+cnt+1);
	res=0;
	for(int i=1;i<=cnt;i++)
	{
		if(c[i]==c[i-1])
			cn[i]=cn[i-1];
		else
			cn[i]=++res;
	}
	for(int i=1;i<=m;i++)
		b[i]=cn[lower_bound(c+1,c+cnt+1,b[i])-c];
}
int C(int n,int m)
{
	return fac[n]*sq[m]%mod*sq[n-m]%mod;
} 
bool check(int x,int y,int r,int c)
{
	if(a[r]<a[x] || b[c]<b[y] || (a[r]==a[x] && r!=x) || (b[c]==b[y] && c!=y))
		return 1;
	return 0; 
}
signed main()
{
	fac[0]=sq[0]=1;
	for(int i=1;i<N;i++)
		fac[i]=fac[i-1]*i%mod,sq[i]=qpow(fac[i],mod-2);
	int n,m,t;
	cin>>n>>m>>t;
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=m;i++)
		cin>>b[i];
	init(n,m);
	for(int i=1;i<=n;i++)
		num1[a[i]]++;
	for(int i=1;i<=m;i++)
		num2[b[i]]++;
	pow1[0]=1;
	for(int i=1;i<=n+m;i++)
		pow1[i]=pow1[i-1]*num1[i]%mod;
	pow2[0]=1;
	for(int i=1;i<=n+m;i++)
		pow2[i]=pow2[i-1]*num2[i]%mod;
	while(t--)
	{
		int x,y,r,c;
		cin>>x>>y>>r>>c;
		if(check(x,y,r,c))
		{
			cout<<0<<endl;
			continue;
		}
		int ans=C(a[r]-a[x]+b[c]-b[y],b[c]-b[y]);
		if(a[r]!=a[x])
			ans*=pow1[a[r]-1]%mod*qpow(pow1[a[x]],mod-2)%mod,ans%=mod;
		if(b[c]!=b[y])
			ans*=pow2[b[c]-1]%mod*qpow(pow2[b[y]],mod-2)%mod,ans%=mod;
		cout<<ans<<endl;
	}
} 

看到这里,就点个赞吧!

原文