列表

详情


NC13884. Paint Box

描述

We have n empty boxes, so let’s recolor those boxes with m colors.
The boxes are put in a line. It is not allowed to color any adjacent boxes with the same color. Boxes i and i+1 are said to be adjacent for every i,1≤i≤n. 
And we also want the total number of different colors of the n boxes being exactly k. 
Two ways are considered different if and only if there is at least one box being colored with different colors.

输入描述

The first line of the input contains integer T(1≤T≤100) -the number of the test cases 
For each case: there will be one line, which contains three integers n,m,k(1≤n,m≤109 1≤k≤106, k≤n,m).

输出描述

For each test case, you need print an integer means the number of ways of different coloring methods modulo 109+7.

示例1

输入:

2
3 2 2
3 2 1

输出:

2
0

说明:

In the first example we have two ways:
121
212
where 1 and 2 are two different color.
In the second example we can't do that.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 297ms, 内存消耗: 376K, 提交时间: 2021-02-15 11:33:23

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll fast(ll a,ll b)
{
	ll ans=1;
	a%=mod;
	while(b)
	{
		if(b&1)
		ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
ll C(ll n,ll r)
{
	if(n<r)
	return 0;
	if(n-r<r)
	r=n-r;
	ll a=1,b=1;
	for(int i=0;i<r;i++)
	a=(a*(n-i))%mod,b=(b*(i+1))%mod;
	return a*fast(b,mod-2)%mod;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		ll n,m,k,i,res=1,ans=0;
		scanf("%lld %lld %lld",&n,&m,&k);
		for(i=k;i>=1;i--)
		{
			ans=(ans+i*fast(i-1,n-1)%mod*res%mod+mod)%mod;
			res=-1*res*i%mod*fast(k-i+1,mod-2)%mod;
		}
		ans=(ans*C(m,k))%mod;
		printf("%lld\n",ans);
	}
	return 0;
}

上一题