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

22 篇文章 0 订阅
9 篇文章 0 订阅

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);
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值