Codeforces 1081C. Colorful Bricks

给你n个方格排成一行,有m种颜色,然后要把这n个方格分成k+1段,每段涂不同的颜色,问有多少种方法。
排列组合问题,首先要在n-1个位置里面选出k个位置当作段与段的分割点,然后每段涂的时候有m*(m-1)^k种,二者相乘即使答案。
要注意的是计算组合数的时候也要取mod,因为组合数的增加也是很快的。
还有要上快速幂计算

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n,m,k,ans,c[2020][2020];
const LL mod = 998244353 ; 

LL fast_multi(LL m, LL n, LL mod)
{
    LL ans = 0;
    while (n)
    {
        if (n & 1)
            ans += m;
        m = (m + m) % mod;
        m %= mod;
        ans %= mod;
        n >>= 1;
    }
    return ans;
}
LL fast_pow(LL a, LL n, LL mod)
{
    LL ans = 1;
    while (n)
    {
        if (n & 1)
            ans = fast_multi(ans, a, mod);
        a = fast_multi(a, a, mod);
        ans %= mod;
        a %= mod;
        n >>= 1;
    }
    return ans;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	//freopen("data.in","r",stdin);
	//freopen("data.out","w",stdout);
	c[0][0] = c[1][0] = 1;
	for(int i = 1;i<2020;i++)
		for(int j = 0;j<2020;j++)
			 c[i][j] = (c[i-1][j]+c[i-1][j-1])%mod;
	cin>>n>>m>>k;
	cout<<((c[n-1][k]*m)%mod*fast_pow(m-1,k,mod))%mod;
	return 0; 
} 

猜你喜欢

转载自blog.csdn.net/winhcc/article/details/85054526