Codeforces gym 100548 F (2014-2015 icpc西安现场赛) 数学

Color
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 Sample Output
2
3 2 2
3 2 1
Case #1: 2
Case #2: 0


题意:小球排成一排,从m种颜色中选取k种颜色给n个球上色,要求相邻的球的颜色不同,求可行的方案数,答案模1e9+7


题解:

我们设必须用 i 种颜色两两不相邻的涂格子的方案数为 b(i) ;

很明显: 我们令 a(k)=k·(k-1)n-1, 然后有.

如果你知道二项式反演的话,那么这个问题就已经解决了,因为

 

是不是觉得二项式反演很厉害,下面我将给出它的证明。

  • 二项式反演公式:

      

 

  • 证明:

   

   

   然后让我们对进行分析:

    我们预热一下:

      有A,B,C,D,E,F,G 7个人,我们要先从中选出4个候选人,再从中选出3个作为master。

      那么我们可以很容易写出其方案数:C(7,4)*C(4,3)

      但是我们用另一种邪恶的思路思考这个问题我们可以先内部选出3个master,在从剩下的4个人选出一个来:C(7,3)*C(4,1)

      所以我们很容易的得到了这个等式:

 

    所以有

    然后你会发现剩下的部分就是 (1 - 1)n-j的项张开而已,所以:

    ,所以原式 = b(n) .

       所以:成立。



#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll P=1000000007;
ll ni[1000005];
ll quick(ll a,ll k,ll s){
    ll ans=1;
    while(k){
        if(k&1)ans=ans*a%s;
        a=a*a%s;
        k/=2;
    }
    return ans;
}
ll C(ll n,ll m){
    ll ans=1,i;
    for(i=n;i>n-m;i--)ans=ans*i%P;
    ll d=1;
    for(i=1;i<=m;i++)d=d*i%P;
    ans=ans*quick(d,P-2,P)%P;
    return ans;
}
int main(){
    ll t,cas=1,i;
    scanf("%lld",&t);
    ni[1]=ni[0]=1;
    for(i=2;i<=1000000;i++)ni[i]=(P-P/i)*ni[P%i]%P;
    while(t--){
        ll n,m,k;
        scanf("%lld%lld%lld",&n,&m,&k);
        printf("Case #%lld: ",cas++);
        if(n!=1&&k==1){
            printf("0\n");
            continue;
        }
        ll ans=C(m,k);
        ll now=1,sig=1,d=0;
        for(i=k;i>=1;i--){  
        	d=(d+(sig*i*quick(i-1,n-1,P))%P*now%P+P)%P;  
        	sig=-sig;
        	now=now*i%P;
        	now=now*ni[k-i+1]%P;
		} 
        ans=ans*d%P;
        printf("%lld\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值