HDU - 5145 NPY and girls 莫队算法+逆元

题目链接:https://cn.vjudge.net/problem/HDU-5145

题意:区间[l, r] 内的数全排列能得到多少种不同的排列

题解:假设sum是当前的数量,再加上一个就是乘上sum+1,乘上这个数+1 的数量的逆元,减去一个是乘上sum的逆元,乘上这个数的数量

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lowbit(x) x&(-x)
const int N=30010;
const ll mod=1e9+7;
struct node{
	int l,r;
	int id;
}a[N];
int n,m,CM;
int c[N];
ll num[N]; 
ll ans[N];
ll inv[N];
bool cmp(node x,node y)
{
	if(x.l/CM!=y.l/CM) return x.l<y.l;
	else return x.r<y.r;
}
ll ksm(ll x, ll y)
{
	ll res=1;
	while(y)
	{
		if(y&1) res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
int main()
{
	for(int i=1;i<=30000;i++)
	{
		inv[i]=ksm(1LL*i,mod-2);
	}
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)scanf("%d",&c[i]);
		CM=(int)sqrt(n); 

		for(int i=1;i<=m;i++)scanf("%d%d",&a[i].l,&a[i].r),a[i].id=i;
		sort(a+1,a+1+m,cmp);
		int l,r;
		ll res,sum;
		for(int i=1,j=1;j<=m;i++)
		{
			memset(num,0,sizeof(num));
			l=a[j].l+1,r=a[j].l;
			res=1;
			sum=0;
			for(;j<i*CM && j<=m;j++)
			{
				while(a[j].l<l)
				{
					l--;
					sum++;
					if(sum>0)res=res*sum%mod;
					num[c[l]]++;
					if(num[c[l]]>0)res=res*inv[num[c[l]]]%mod;
				}
				
				while(a[j].l>l)
				{
					if(sum>0)res=res*inv[sum]%mod;
					if(num[c[l]]>0)res=res*num[c[l]]%mod;
					sum--;
					num[c[l]]--;
					l++;
				}
				
				while(a[j].r<r)
				{
					if(sum>0)res=res*inv[sum]%mod;
					if(num[c[r]]>0)res=res*num[c[r]]%mod;
					sum--;
					num[c[r]]--;
					r--;
				}
				while(a[j].r>r)
				{
					r++;
					sum++;
					if(sum>0)res=res*sum%mod;
					num[c[r]]++;
					if(num[c[r]]>0)res=res*inv[num[c[r]]]%mod;
				//	cout<<sum<<" "<<res<<" "<<inv[num[c[r]]]<<" "<<c[r]<<" "<<num[c[r]]<<endl;
				}
			
			//	cout<<a[j].id<<" "<<res<<endl;
				ans[a[j].id]=res;
				l=a[j].l,r=a[j].r;
			}
		}
		for(int i=1;i<=m;i++)
		{
			printf("%lld\n",ans[i]);	
		}
	}
	
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值