https://codeforces.com/problemset/problem/1081/C

这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况。

$k=0$ 就是全部同色, $k=1$ 就是左边一个色右边一个色, $m(m-1)$ ,再选转折点有 $i-1$ 种 $C_{i-1}^{1} $( $i$ 个球。 $i-1$ 个空挡都可以插)。

到 $k=2$ 呢?可以是三种不同颜色,也可以是左右左,也就是 $m(m-1)(m-1)$ ,再选转折点有 $C_{i-1}^{2}$ 。

到k=3呢?更复杂了? $m(m-1)(m-1)(m-1)$ ,中间的用隔板法算,也就是 $C_{i-1}^{3}$ ?

所以答案其实就是 $m(m-1)^kC_{n-1}^k$ ,是组合数学?

隔板法就是:(可以为空的情况)给每组默认加上一个小球,然后隔板就可以选所有球的间隔。(不能为空的情况:)选所有球的间隔

ll qpow(ll x,ll n){
ll res=;
while(n){
if(n&)
res=res*x%p;
x=x*x%p;
n>>=;
}
return res;
} void init(int n){
fac[]=;
for(int i=;i<=n;i++){
fac[i]=fac[i-]*i%p;
}
invfac[n]=qpow(fac[n],p-);
//费马小定理
for(int i=n;i>=;i--){
invfac[i-]=invfac[i]*i%p;
}
//线性求阶乘逆元
} ll C(int n,int m){
return fac[n]*invfac[n-m]%p*invfac[m]%p;
}

dp解法: $f[i][j]=f[i-1][j]+f[i-1][j-1]$ ,以位置 $i$ 为结尾的,有j次转折,分在此转折和跟随左侧颜色两种情况

好不容易抄了一个线性求 $invfac$ ,居然费马小定理的时候传入一个 $n$ 而不是 $fac[n]$ ,我是服气了的,最后还忘记把 $2$ 改成 $k$ ,我大概是智障吧。

#include<bits/stdc++.h>
using namespace std;
#define ll long long ll dp[][]={}; //dp[i][k]以i为结尾的,有k块砖与左边的颜色不一样
ll p=; ll pow_mm1[];
ll fac[];
ll invfac[]; ll qpow(ll x,ll n){
ll res=;
while(n){
if(n&)
res=res*x%p;
x=x*x%p;
n>>=;
}
return res;
} void init(int n){
fac[]=;
for(int i=;i<=n;i++){
fac[i]=fac[i-]*i%p;
}
invfac[n]=qpow(fac[n],p-);
//费马小定理
for(int i=n;i>=;i--){
invfac[i-]=invfac[i]*i%p;
}
//线性求阶乘逆元
} ll C(int n,int m){
return fac[n]*invfac[n-m]%p*invfac[m]%p;
} int n,m,k;
int main(){
init(); pow_mm1[]=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=;i++){
pow_mm1[i]=pow_mm1[i-]*(m-)%p;
} /*for(int i=1;i<=n;i++){
dp[i][0]=m;
//都是同一种颜色
} for(int i=1;i<=n;i++){
dp[i][1]=m*(m-1)%p*(i-1)%p;
//左边的至少一块砖其中一种颜色,右边的另一种颜色,左边有1~i-1块砖
for(int k=2;k<=i-1;k++){
dp[i][k]=(m*pow_mm1[k])%p*C(i-1,k)%p;
}
}
*/ //dp[i][k]=(m*pow_mm1[k])%p*C(i-1,k)%p;
printf("%lld\n",(m*pow_mm1[k])%p*C(n-,k)%p); }

拓展阅读:组合数学的水题?https://www.cnblogs.com/kuangbin/archive/2012/08/28/2661066.html

2019-01-16

Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学的更多相关文章

  1. Codeforces - 9D - How many trees? - 简单dp - 组合数学

    https://codeforces.com/problemset/problem/9/D 一开始居然还想直接找公式的,想了想还是放弃了.原来这种结构是要动态规划. 状态是知道怎么设了,$t_{nh} ...

  2. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  3. Codeforces - 702A - Maximum Increase - 简单dp

    DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp 遇到了这道题 https://codeforce ...

  4. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  5. Codeforces - 909C - Python Indentation - 简单dp

    http://codeforces.com/problemset/problem/909/C 好像以前做过,但是当时没做出来,看了题解也不太懂. 一开始以为只有上面的for有了循环体,这里的state ...

  6. CodeForces 30C Shooting Gallery 简单dp

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/36944227 题目链接:点击打开链接 给定 ...

  7. Codeforces - 1033C - Permutation Game - 简单dp - 简单数论

    https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...

  8. CodeForces 22B Bargaining Table 简单DP

    题目很好理解,问你的是在所给的图中周长最长的矩形是多长嗯用坐标(x1, y1, x2, y2)表示一个矩形,暴力图中所有矩形易得递推式:(x1, y1, x2, y2)为矩形的充要条件为: (x1, ...

  9. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

随机推荐

  1. 关于HTML中文乱码问题

    系统:ubuntu 14.04 软件:bluefish 一.乱码原因 1.不同编码内容混杂:HTML乱码是因为html编码问题照成(常见gb2312与utf-8两种编码内容同一时候存在照成) 2.未设 ...

  2. HTML--比较实用的小例子

    常用的前端实例: 1略 2.在网页商城中的图片当我们把鼠标放上去之后,图片会显示一个有颜色的外边框,图片某一部分的字体的颜色并发生改变 鼠标放上去之前 鼠标放上去之后: 实现的代码: <!DOC ...

  3. Effective C++ 条款17 以独立语句将newed对象置入智能指针

      对于函数: int priority(); void processWidget(std::tr1::  shared_ptr<Widget> pw,int priority); 调用 ...

  4. APACHE局域网配置域名访问

    /** * * @email 514320008@qq.com * @author jshaibozhong * */ 1,打开APACHE的目录  \Apache2\conf\extra\httpd ...

  5. C# json反序列化 对象中嵌套数组 (转载) 可能会导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

    C# json反序列化 对象中嵌套数组 (转载)   看图: 这里可以看到是二层嵌套!!使用C#如何实现?? 思路:使用list集合实现 → 建立类 → list集合 → 微软的   Newtonso ...

  6. 【Leetcode】经典的Jump Game in JAVA

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. Arcgis Engine(ae)接口详解(8):临时元素(element)

    //主地图的地图(map)对象 IMap map = null; IActiveView activeView = null; //IGraphicsContainer用于操作临时元素,可以通过map ...

  8. 使用Apache Ant合并多个jar

    Apache Ant下载地址 下载解压后进入bin目录,并在此目录打开cmd 在cmd中运行ant,运行结果为: Buildfile: build.xml does not exist! Build ...

  9. NSTimer 使用小结

    目录 1. NSRunLoopCommonModes和Timer 2. NSThread和Timer 3. GCD中的Timer 返回目录 1. NSRunLoopCommonModes和Timer ...

  10. 关于eclipse的resource文件没有发布到tomcat上的解决方案

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/luman1991/article/details/53457302