A - Color Gym - 100548F 数论板子 组合数学

Description

    Recently, Mr. Big recieved n flowers from his fans. He wants to recolor those flowers with m colors. The flowers are put in a line. It is not allowed to color any adjacent flowers with the same color. Flowers i and i + 1 are said to be adjacent for every i, 1 ≤ i < n. Mr. Big also wants the total number of different colors of the n flowers being exactly k. Two ways are considered different if and only if there is at least one flower being colored with different colors.

Input

The first line of the input gives the number of test cases, T. T test cases follow. T is about 300 and in most cases k is relatively small. For each test case, there will be one line, which contains three integers n, m, k (1 ≤ n, m ≤ 109 , 1 ≤ k ≤ 106 , k ≤ n, m).

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the number of ways of different coloring methods modulo 109 + 7.

Samples

Sample Input

2

3 2 2

3 2 1

Sample Output

Case #1: 2

Case #2: 0


题意:

  

做法:


数论菜鸡。。直接贴代码了。。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1000005;
const int mod=(int)1e9+7;
ll n,m,k,inv[maxn],ans,C_k[maxn];
void init(){
    inv[0]=inv[1]=1;
    for(int i=2;i<maxn;i++){
        inv[i]=(mod-mod/i)*inv[mod%i]%mod;
    }
}
ll quick(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1) ans=ans*a%mod;
        a=a*a%mod;
        b/=2;
    }
    return ans;
}
ll C(ll n,ll m){
    ll ans=1;
    for(int i=1;i<=m;i++){
        ans=ans*(n-i+1)%mod*inv[i]%mod;
    }
    return ans;
}
void deal(){

    ll temp=1;
    C_k[0]=1;
    for(int i=1;i<=k;i++){
        temp=(temp*(k-i+1)%mod*inv[i]%mod);
        C_k[i]=temp;
    }
    ll tmp=1;
    for(int i=k;i>=0;i--){
        ans=((ans%mod+C_k[i]*i%mod*quick(i-1,n-1)%mod*tmp)+mod)%mod;
        //printf("%lld  %lld  %lld",C_k,i,quick(i-1,n-1));
        tmp=tmp*(-1);
    }
    ans=ans*C(m,k)%mod;
    if(ans<0) ans+=mod;
}
int main(){
    init();
	int t,cas=0;
	cin>>t;
	while(t--){
        ans=0;
        scanf("%lld%lld%lld",&n,&m,&k);
        deal();
        printf("Case #%d: %lld\n",++cas,ans);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41955236/article/details/81673375