ZZULIOJ 2826: 使用技能

ZZULIOJ 2826: 使用技能

在这里插入图片描述

#include<cstdio>
typedef long long LL;
const int N = 100010, MOD = 1e9+7;
int n,m;
LL fact[N],infact[N];
int ksm(LL a,int b)
{
    
    
	LL res=1;
	for(;b;b>>=1)
	{
    
    
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
	}
	return res;
} 
int C(int a,int b)
{
    
    
	return fact[a]*infact[b]%MOD*infact[a-b]%MOD;
}
int main()
{
    
    
	fact[0]=infact[0]=1;
	for(int i=1;i<=100001;i++)
	{
    
    
		fact[i]=fact[i-1]*i%MOD;
		infact[i]=infact[i-1]*ksm(i,MOD-2)%MOD;
	}
	int T;scanf("%d",&T);
	while(T--)
	{
    
    
		scanf("%d%d",&n,&m);
		LL ans=0;
		for(int i=1;i<=n;i++)
		{
    
    
			ans+=(LL)C(n,i)*i%MOD*i%MOD*m%MOD*ksm(m-1,n-i)%MOD;
			ans%=MOD;
		}
		ans=ans*ksm(ksm(m,n),MOD-2)%MOD;
		printf("%lld\n",ans);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_52792570/article/details/121177331