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(向上取整),问从起点到终点的分数的数学期望是多少 ...
随机推荐
- mysql的学习笔记(三)
1.外键约束(保持数据一致,完整.实现一对多或一对一) 父表(参照的表)和子表(有外键列的表)必须使用相同的存储引擎InnoDB,禁止使用临时表. 外键列和参照列必须具有相似的数据类型.其中数字的长度 ...
- Data Warehouse
Knowledge Discovery Process OLTP & OLAP 联机事务处理(OLTP, online transactional processing)系统:涵盖组织机构大部 ...
- [翻译] 对正在使用EF6x开发人员的一些话
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...
- 获得当前EIP的值
VS2017 内联汇编 __asm { call NEXT NEXT: pop eax }
- Spring学习(零):我们为什么要学习Spring
序言 通过使用Spring的IoC容器,可以对这些耦合关系(对Java代码而言)实现一个简单的文本化的操作:即是说通过一个或几个XML文文件,我们就可以方便的对应用对象的耦合关系进行浏览.修改和维护, ...
- Mondrian + JPivot 环境配置
一.环境准备 特别说明:Mondrian + JPivot 环境笔者已整理调试通过,可直接部署运行. 1.1 环境要求 JDK1.8+ 1.2 环境包说明 从 https://pan.baidu.co ...
- IOS多态在项目中的应用
今天我们讲述一个知识点(大家可能遗漏的) 多态是面试程序设计(OOP)一个重要特征,但在iOS中,可能比较少的人会留意这个特征,实际上在开发中我们可能已经不经意的使用了多态.比如说: 有一个table ...
- MySQL 笔记整理(10) --MySQL为什么有时会选错索引?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 10) --MySQL为什么有时会选错索引? MySQL中的一张表上可以 ...
- Spring Aop底层原理详解
Spring Aop底层原理详解(来源于csdn:https://blog.csdn.net/baomw)
- Eclipse代码快捷键
今天终于找到了代码注释快捷键:ctrl+shift+/取消注释快捷键:ctrl+shift+\ Java文件:注释和取消注释的快捷键都是:CTRL + / 或 Shift+Ctrl+C JS文件:注释 ...