UVALive 7040 F Color 容斥&组合数&快速幂&阶乘逆元

16 篇文章 0 订阅

Recently, Mr. Big recieved n owers from his fans. He wants to recolor those owers with m colors. The owers are put in a line. It is not allowed to color any adjacent owers 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 owers being exactly k.

Two ways are considered different if and only if there is at least one ower 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.

Sample Input

2

3 2 2

3 2 1

Sample Output

Case #1: 2

Case #2: 0

 

题目大意:

给n个连成一条线的块上色,相邻两块不能用同样的颜色,并且恰好用所给m种颜色中的任意k种颜色上色。

 

一开始想的过于简单了列出结果的式子是 C(k,m) * k * (k-1)^(n-1),但是这个式子里包括用的颜色不足k种的情况,同时还有重复,可以说是很蠢很单纯了。

后来,唉,那就容斥吧,好麻烦orz。

式子大概是这样子的:

C(k,m) * ( k*(k-1)^(n-1) - C(1,k)*(k-1)*(k-2)^(n-1) + C(2,k)*(k-2)*(k-3)^(n-1) ... +(-1)^i * C(i,k)*(k-i)*(k-i-1)^(n-1) ...

组合数C(m,n) = n*...*(n-m+1)/m!;

求阶乘逆元。还有快速幂。略。

注!因为有可能中间值有负数,所以累加的时候+mod再%mod!

 

#include<cstdio>
typedef long long ll;
const int maxk=1e6+5;
const ll mod=1e9+7;

ll fac[maxk],rev[maxk];

ll fast_pow(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1){
            b--;
            ans=ans*a%mod;
        }
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
ll inv(ll a, ll n) {
    return fast_pow(a, mod-2);
}
void init()
{
     ll k=maxk-5;
     fac[0]=1;
     for(int i=1;i<=k;++i)
          fac[i]=fac[i-1]*i%mod;
     rev[k]=inv(fac[k],mod-2);
     for(int i=k-1;i>=0;--i)
          rev[i]=rev[i+1]*(i+1)%mod;
}
ll C(ll n,ll m){
    if(m<0)return 0;
    if(m>n-m)m=n-m;
    ll up=1,down=1;
    for(ll i=0;i<m;i++){
        up=up*(n-i)%mod;
        down=down*(i+1)%mod;
    }
    return up*inv(down,mod)%mod;
}

ll cal(ll k,ll n)
{
     ll ans=k*fast_pow(k-1,n-1);
     for(int i=1;i<k;++i){
          ll x=fac[k];
          x=x*rev[k-i]%mod*rev[i]%mod;
          x=x*(k-i)%mod*fast_pow(k-i-1,n-1);
          if(i&1) x=-x;
          ans=(ans+x+mod)%mod;
     }
     return ans;
}

int main()
{
    int t;
    ll n,m,k;
    init();
    scanf("%d",&t);
    for(int cas=1;cas<=t;++cas){
        scanf("%lld%lld%lld",&n,&m,&k);
        ll ans=C(m,k);
        ans=(ans*cal(k,n))%mod;
        printf("Case #%d: %lld\n",cas,ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值