Colorful Bricks CodeForces - 1081C ( 组合数学 或 DP )
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.
There are nn bricks lined in a row on the ground. Chouti has got mm paint buckets of different colors at hand, so he painted each brick in one of those mm colors.
Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are kkbricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).
So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo 998244353998244353.
Input
The first and only line contains three integers nn, mm and kk (1≤n,m≤2000,0≤k≤n−11≤n,m≤2000,0≤k≤n−1) — the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
Output
Print one integer — the number of ways to color bricks modulo 998244353998244353.
Examples
3 3 0
3
3 2 1
4
Note
In the first example, since k=0k=0, the color of every brick should be the same, so there will be exactly m=3m=3 ways to color the bricks.
In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all 44 possible colorings.
题意:
给你n个排成一列的方块,有m种颜色,让你求出这n个方块中有k个方块的颜色和左边的方块颜色不同的方案数量。
思路:
可以有两个方法来做这题,
我们先来看第一种方法,组合数学的方式,我们知道第一个方块是不会被计入到这k个数中的,所以无论如何排列,第一个方块都有m种取色的方案。
然后我们只需要在后面的n-1个方块中,选择k个,使之和左边的颜色不同,n-1中取k个,我们可以用组合数学的公式求出。
然后对于每一个被选择作为k个之一的方块,这个方块的唯一颜色限定就是要求和左边的颜色不同,而左边的颜色无论取什么,
对当前的方块来说只是一种取色,那么我们这个方块可以从除了左边的方块颜色中的m-1个颜色中任意选取一个。
而要求和左边颜色相同的方块,它们的颜色不不归属自己的选择,因为要和左边的颜色一样,
那么我们可以得到这题的公式,就是ans=m*C(n-1,k)* ((m-1)^k )
记得取模就好了。
方案1的代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n,m,k;
ll dp[2020][2020];
const ll mod=998244353ll;
ll quick(ll a,ll b,ll m)
{
ll ans=1;
while(b)
{
if(b&1)
{
ans=(a*ans)%m;
}
a=(a*a)%m;
b>>=1;
}
return ans;
}
ll inverse(ll a,ll p){return quick(a,p-2,p);} //模素数求逆元
// a 中选b个 ,结果对m取模
ll comp(ll a,ll b,ll m) //组合数取模
{
if(a<b) return 0;
if(a==b) return 1;
ll ans=1,ca=1,cb=1;
for(ll i=0;i<b;i++)
{
ca=(ca*(a-i))%m;
cb=(cb*(b-i))%m;
}
ans=(ca*inverse(cb,m))%m;
return ans;
}
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>m>>k;
ll ans=m;
ans=(ans+mod)%mod;
ans*=comp(n-1ll,k,mod);
ans=(ans+mod)%mod;
ans*=powmod(m-1ll,k,mod);
ans=(ans+mod)%mod;
// ans*=powmod(m,n-k,mod);
// ans=(ans+mod)%mod;
cout<<ans<<endl; return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
方案2:DP
我们定义二维DP的状态如下:
dp[i][j] 表示,到第i个位置时,有j个方块和左边的颜色不同的方案数。
我们再思考dp[i][j]这个状态可以从哪里转移过来。
我们考虑当前第i个位置的两种选择,
1,和左边相同的颜色。
那么应该加上上一个状态的j值,即 dp[i][j]+=dp[i-1][j]
2,和左边的值不相同,
那么当前的dp[i][j] 应该加上 dp[i-1][j-1]*(m-1)
即上一个状态,是j个不容方块时,乘上当前i块可以有m-1种取色。
最后的dp[n][k] 就是我们要得到的答案,
记得取模。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n,m,k;
ll dp[][];
const ll mod=998244353ll; int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n>>m>>k;
// dp[i][j] 到第i个位置,有k个和左边不同的
repd(i,,n)
{
dp[i][]=m;
}
repd(i,,n)
{
repd(j,,k)
{
dp[i][j]+=dp[i-][j];
dp[i][j]=(dp[i][j]+mod)%mod;
dp[i][j]+=dp[i-][j-]*(m-1ll);
dp[i][j]=(dp[i][j]+mod)%mod;
}
}
cout<<dp[n][k]<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Colorful Bricks CodeForces - 1081C ( 组合数学 或 DP )的更多相关文章
- Avito Cool Challenge 2018:C. Colorful Bricks
C. Colorful Bricks 题目链接:https://codeforces.com/contest/1081/problem/C 题意: 有n个横向方块,一共有m种颜色,然后有k个方块的颜色 ...
- Avito Cool Challenge 2018 C. Colorful Bricks 【排列组合】
传送门:http://codeforces.com/contest/1081/problem/C C. Colorful Bricks time limit per test 2 seconds me ...
- codeforces 1194F (组合数学)
Codeforces 11194F (组合数学) 传送门:https://codeforces.com/contest/1194/problem/F 题意: 你有n个事件,你需要按照1~n的顺序完成这 ...
- 【bzoj5004】开锁魔法II 组合数学+概率dp
题目描述 有 $n$ 个箱子,每个箱子里有且仅有一把钥匙,每个箱子有且仅有一把钥匙可以将其打开.现在随机打开 $m$ 个箱子,求能够将所有箱子打开的概率. 题解 组合数学+概率dp 题目约定了每个点的 ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学
https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...
- codeforces 932E Team Work(组合数学、dp)
codeforces 932E Team Work 题意 给定 \(n(1e9)\).\(k(5000)\).求 \(\Sigma_{x=1}^{n}C_n^xx^k\). 题解 解法一 官方题解 的 ...
- Codeforces 840C 题解(DP+组合数学)
题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...
- Codeforces 722E 组合数学 DP
题意:有一个n * m的棋盘,你初始在点(1, 1),你需要去点(n, m).你初始有s分,在这个棋盘上有k个点,经过一次这个点分数就会变为s / 2(向上取整),问从起点到终点的分数的数学期望是多少 ...
随机推荐
- C#委托、事件、线程
这是几个简单的例子,但是实际的开发中委托还还只在反射时用到过,事件的话只自己做了一次,并且还是特意去用的 ,实际上可以不用.线程的话,因为需要,所以用的会多点,这里主要是WS上的线程. 委托 在前面的 ...
- Docker进阶之三:Docker应用场景
场景一:节省项目环境部署时间 1.单项目打包 每次部署项目到测试.生产等环境,都要部署一大堆依赖的软件.工具,而且部署期间出现问题几率很大,不经意就花费了很长时间. Docker主要理念就是环境打包部 ...
- Spring加载流程源码分析03【refresh】
前面两篇文章分析了super(this)和setConfigLocations(configLocations)的源代码,本文来分析下refresh的源码, Spring加载流程源码分析01[su ...
- 在线生成二维码的API接口
现在很多大网站都有这样的一个功能,使用手机扫描一下网页上的二维码便可快速在手机上访问网站.想要实现这样的功能其实很简单,下面麦布分享几个在线生成网址二维码的API接口.都是采用http协议接口,无需下 ...
- mysql 设置自增主键id的起始值
修改user表,主键自增从20000开始 alter table user AUTO_INCREMENT=20000;
- I/O输出流基础之FileOutputStream
OutputStream:是所有字节输出流的父类,其作用是:用这个流把网络数据(getOutputStream()),或者内存中的字节数组数据写到文件系统中文件系统(FileOutputStream) ...
- Java并发——线程介绍
前言: 互联网时代已经发展到了现在.从以前只考虑小流量到现在不得不去考虑高并发的问题.扯到了高并发的问题就要扯到线程的问题.你是否问过自己,你真正了解线程吗?还是你只知道一些其他博客里写的使用方法.下 ...
- Java web的一些面试题
1.Tomcat 的优化经验 答:去掉对 web.xml 的监视,把 jsp 提前编辑成 Servlet. 有富余物理内存的情况,加大 tomcat 使用的 jvm 的内存 2.HTTP 请求的 GE ...
- 前端零基础 --css转换--skew斜切变形 transfor 3d
前端零基础 --css转换--skew斜切变形 transfor 3d==============重要不紧急! 重要紧急 重要不紧急 不重要紧急 不重要不紧急
- vue中使用百度地图,悬浮窗搜索功能
https://www.cnblogs.com/shuaifing/p/8185311.html 侵删 <template> <div id="all"> ...