CF GYM 100548 Color(2014ACM西安现场赛Problem F)

ProblemF. Color

Description

Recently, Mr. Bigrecieved n flowers from his fans. He wants to recolor those flowerswith m colors. The flowers are put in a line. It is not allowed tocolor any adjacent flowers with the same color. Flowers i and
i + 1are said to be adjacent for every i, 1 ≤ i < n. Mr. Big alsowants the total number of different colors of the n flowers beingexactly k.

Two ways areconsidered different if and only if there is at least one flowerbeing colored

with differentcolors.

Input

The first line ofthe input gives the number of test cases, T. T test cases follow. Tis 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 ≤ 10^9, 1 ≤ k ≤ 10^6, k ≤ n, m).

Output

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

Samples


Sample Input


Sample Output


2

3 2 2

3 2 1


Case #1: 2

Case #2: 0

知识点:

组合数学-容斥原理,快速幂,逆元。

题目大意:

共有m
种颜色,为n盆排成一直线的花涂色。要求相邻花的颜色不相同,且使用的颜色恰好是k种。问一共有几种涂色方案(结果除10e9+7取余数)。

解题思路:

首先可以将m
与后面的讨论分离。从m
种颜色中取出k
种颜色涂色,取色部分有C(m, k)
种情况;

然后通过尝试可以发现,第一个有k种选择,第二个因不能与第一个相同,只有(k-1)
种选择,第三个也只需与第二个不同,也有(k-1)
种选择。总的情况数为k ×(k-1)^(n-1)。但这仅保证了相邻颜色不同,总颜色数不超过k种,并没有保证恰好出现k种颜色;

接着就是一个容斥问题,上述计算方法中包含了只含有2、3、…、(k-1)种颜色的情况,需要通过容斥原理去除。假设出现p
(2 <= p <= k-1)种颜色,从k种颜色中选取p种进行涂色,方案数为C(k,p)
× p × (p-1)^(n-1) ;

综上,最后的总方案数为C(m,k) × ( k × (k-1)^(n-1) +
∑((-1)^p × C(k, p) × p × (p-1)^(n-1) ) (2 <= p <= k-1);

最后,需要注意1 ≤ n, m ≤10^9,在进行指数运算时,需要使用快速幂。对于组合数,只需要计算C(m,k)和C(k,p)
(1 <= p <= k),可以采用递推法,即C[x,i] = C[x, i-1] * (n-i+1) / i,因为要取模,所以需要用到i的逆元。

参考代码:

#include <iostream>
using namespace std;

const int MOD = 1000000007;
const int MAXN = 1000010;
int nCase, cCase;
long long n, m, k, ans1, ans2, C[MAXN];

inline long long pow_mod(long long p, long long k) {
    long long ans = 1;
    while (k) {
        if (k & 1) {
            ans = (ans * p) % MOD;
        }
        p = (p * p) % MOD;
        k >>= 1;
    }
    return ans;
}

inline long long inverse(long long num) {
    return pow_mod(num, MOD-2);
}

void calcC(long long n) {
    C[0] = 1;
    for (int i = 1; i <= k; i++) {
        C[i] = ((C[i-1] * (n-i+1)) % MOD) * inverse(i) % MOD;
    }
}

void solve() {
    calcC(m);
    ans1 = C[k];

    calcC(k);
    ans2 = 0;
    int sgn = 1;
    for (int l = k; l >= 1; l--) {
        ans2 = (ans2 + (sgn * l * pow_mod(l-1, n-1)) % MOD * C[l] % MOD + MOD) % MOD;
        sgn = -sgn;
    }

    cout << "Case #" << ++cCase << ": " << ans1*ans2 % MOD << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin >> nCase;
    while (nCase--) {
        cin >> n >> m >> k;
        solve();
    }
    return 0;
}
时间: 2024-10-11 14:12:44

CF GYM 100548 Color(2014ACM西安现场赛Problem F)的相关文章

CF GYM 100548 Built with Qinghuai and Ari Factor(2014ACM西安现场赛Problem A)

ProblemA. Built with Qinghuai and Ari Factor Description DISCLAIMER: Allnames, incidents, characters and places appearing in this problem arefictitious. Any resemblance to actual events or locales or realpersons, living or dead, is purely coincidenta

CF GYM 100548 Last Defence(2014ACM西安现场赛Problem K)

ProblemK. Last Defence Description Given two integersA and B. Sequence S is defined as follow: ? S0 = A ? S1 = B ? Si = |Si?1 ?Si?2| for i ≥ 2 Count the number ofdistinct numbers in S. Input The first line ofthe input gives the number of test cases,

CF GYM 100548 The Problem Needs 3D Arrays(2014ACM西安现场赛Problem C)

ProblemC. The Problem Needs 3D Arrays Description A permutation is asequence of integers p1, p2, . . . , pn,consisting of n distinct positive integers and each of them does notexceed n. Assume that r(S) of sequence S denotes the number ofinversions i

CF GYM 100548 The Problem to Make You Happy(2014ACM西安现场赛Problem H)

ProblemH. The Problem to Make You Happy Description Alice and Bob aregood friends, as in every other storyline. One day Alice and Bob areplaying an interesting game. The game is played on a directedgraph with n vertices and m edges, Alice and Bob hav

2014西安现场赛F题 UVALA 7040

地址 题意:求在m种颜色中挑选k种颜色,给n个花朵涂色有几种方法. 分析:画图可以发现,基本的公式就是k ×(k-1)^(n-1).但这仅保证了相邻颜色不同,总颜色数不超过k种,并没有保证恰好出现k种颜色:接着就是一个容斥问题,上述计算方法中包含了只含有2.3.….(k-1)种颜色的情况,需要通过容斥原理去除.假设出现p (2 <= p <= k-1)种颜色,从k种颜色中选取p种进行涂色,方案数为C(k,p) × p × (p-1)^(n-1) :总的方案数就是C(m,k) × ( k × (

Last Defense 西安现场赛 欧几里得出发的应用

这道题的意思是给你两个数A, B, s0 = A, s1 = B, si = |si-1 - si-2|, 让你求出里面有几个不同的数, 首先我们可以确定对于数y和x,y一定能写成kx+b的形式,在数列的生成过程中,会出现kx+b.x.(k-1)x+b.(k-2)x+b.x.....2x+b.x.x+b.b.x,其中出现的不同数字个数就是(kx+b)/ x,之后问题变成了数x和b的问题,最后可以发现这就是一个辗转相除法的过程.每做一次辗转相除gcd(x,y),不同数字个数就多了x/ y.最后加上

codeforces 100548F (西安现场赛F题):容斥原理

题目大意: 对n个排成一排的物品涂色,有m种颜色可选. 要求相邻的物品颜色不相同,且总共恰好有K种颜色,问所有可行的方案数 分析: 从m种颜色中选出k种,有c(m,k)种方法,那么我们只用考虑 k种颜色的涂法即可 显然第一个物品有k种涂法,后面的因为不能跟前面的相同都只有k-1种涂法 因此容易想到一个公式:k*(k-1)^(n-1) 但是这个公式算的是 不超过k种颜色的涂法,题目要求必须k种,怎么办呢? 先考虑一个简化版的问题: 用而且用完5种颜色涂不相关的五个物品的方案数 用阶乘的方法可以算出

CF Gym 101955G Best ACMer Solves the Hardest Problem

链接:https://codeforces.com/gym/101955/problem/G 题意:在二维平面上四种操作: 1,加一个带权的点: 2,删去一个点: 3,给一个点周围欧几里得距离为sqrt(k)的存在的点点权都加w: 4,查询一个到点欧几里得距离为sqrtk的点权和. x, y<6000, k<1e7, sigma(询问次数)<1e6,time:12s 题解:原本以为是数据结构,发现距离为k的x,y其实不多,直接存vector<pii>dis[maxk]暴力即可

第39届ACM国际大学生程序设计竞赛 亚洲区域赛(现场赛)西安站

 第39届ACM国际大学生程序设计竞赛 亚洲区域赛(现场赛)西安赛区总结报告 报告人:田思明 队名:ACpioneer 队长:陈志阳,队员:顾振兴,田思明 西安区域赛告下帷幕,我和陈志阳,顾振兴组成的ACpioneer队最终获得了一块宝贵的铜牌.首先要感谢陈志阳和顾振兴两位杰出队友的努力训练和出色表现,我作为一个新人跟着他们学到了很多很多,也十分珍惜和他们在一起的训练,比赛时光,其次要感谢陈志老师,不辞辛劳陪我们5队和6队前往西安参加比赛,还要感谢集训队所有曾经帮过我们的所有队员们,记得cdy