Description

Snuke loves colorful balls. He has a total of N*K balls, K in each of his favorite N colors. The colors are numbered 1 through N.He will arrange all of the balls in a row from left to right, in arbitrary order. Then, for each of the N colors, he will paint the leftmost ball of that color into color 0, a color different from any of the N original colors.After painting, how many sequences of the colors of the balls are possible? Find this number modulo 109+7. 1≤N,K≤2000

Input

The input is given from Standard Input in the following format: N K

Output

Print the number of the possible sequences of the colors of the balls after painting, modulo 109+7.

 
题意:有$n$种颜色的球,标号$1$到$n$,每种颜色有$k$个。将$nk$个球随机排列后,将每种颜色的第一个球涂成颜色$0$,求最终可能得到的颜色序列的方案数。
分析:
令$f(i,j)~(i\leq j)$表示已经放置了i个编号为0的球与j种第一次出现的位置最靠前的颜色的方案数。每次在当前的第一个空位放置一个颜色为$0$的球或是一种未出现的颜色的球。可得转移方程:
$$f(i,j)=f(i-1,j)+\binom{n-i+(n-j+1)\cdot(k-1)-1}{k-2}\cdot(n-j+1)\cdot f(i,j-1)$$
时间复杂度$O(nk)$。
 
 #include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
const int N=2e3+;
const int mod=1e9+;
int n,m,fac[N*N],inv[N*N],f[N][N];
int read()
{
int x=,f=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
void Mod(int& a,int b){a+=b;if(a>=mod)a-=mod;}
int power(int a,int b)
{
int ans=;
while(b)
{
if(b&)ans=1ll*ans*a%mod;
a=1ll*a*a%mod;b>>=;
}
return ans;
}
int C(int n,int m){return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;}
int main()
{
n=read();m=read();
if(m==){printf("");return ;}
fac[]=;
for(int i=;i<=n*m;i++)fac[i]=1ll*fac[i-]*i%mod;
inv[n*m]=power(fac[n*m],mod-);
for(int i=n*m;i>=;i--)inv[i-]=1ll*inv[i]*i%mod;
f[][]=;
for(int i=;i<=n;i++)
for(int j=;j<=i;j++)
{
f[i][j]=f[i-][j];
if(!j)continue;
Mod(f[i][j],1ll*f[i][j-]*(n-j+)%mod*C(n-i+(n-j+)*(m-)-,m-)%mod);
}
printf("%d",f[n][n]);
return ;
}

【AGC 002F】Leftmost Ball的更多相关文章

  1. 【agc002f】Leftmost Ball(动态规划)

    [agc002f]Leftmost Ball(动态规划) 题面 atcoder 洛谷 题解 我们从前往后依次把每个颜色按顺序来放,那么如果当前放的是某种颜色的第一个球,那么放的就会变成\(0\)号颜色 ...

  2. 【AGC002F】Leftmost Ball DP 数学

    题目大意 有\(n\)种颜色的球,每种\(m\)个.现在zjt把这\(nm\)个球排成一排,然后把每种颜色的最左边的球染成第\(n+1\)种颜色.求最终的颜色序列有多少种,对\(1000000007\ ...

  3. 【agc002f】Leftmost Ball

    题目大意 有n种颜色,每种k个球.将这些球任意排列,将每种颜色中最前面的一个求涂成白色(就是n+1种颜色),求最终的排列的方案的个数. 解题思路 考虑如何计算不会算重, 按颜色顺序,每次往排列插入k个 ...

  4. 【AGC 005F】Many Easy Problems

    Description One day, Takahashi was given the following problem from Aoki: You are given a tree with ...

  5. 【AGC 036C】GP2

    https://atcoder.jp/contests/agc036/tasks/agc036_c 题意 有一个长度为 $n$ 的非负整数序列 $x$,初始时全为 $0$.一次操作定义为选择一对正整数 ...

  6. 【AtCoder】AGC022 F - Leftmost Ball 计数DP

    [题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...

  7. 【AGC】增长服务1-远程配置示例

    ​ [AGC]增长服务1-远程配置示例 前言:上一次笔者给大家带来了AGC领域的性能管理服务的学习.这次我们再继续深化学习AGC的相关知识.在文章开始之前,再给读者简单介绍一下AGC,以免第一次来的读 ...

  8. 【MVC 4】4.MVC 基本工具(Visual Studio 的单元测试、使用Moq)

     作者:[美]Adam Freeman      来源:<精通ASP.NET MVC 4> 3.Visual Studio 的单元测试 有很多.NET单元测试包,其中很多是开源和免费的.本 ...

  9. 【面试题】100IT名企java面试必考面试题

    一.Java 基础部分 1.   JAVA 的基本数据类型有哪些 ?   String 是不是基本数据类型 ? Java  有 8 种基本数据类型:           byte    int     ...

随机推荐

  1. Video clip 视频剪辑:入门级

    作为一个对小说漫画电视剧电影的设计有着自己独特需求的人,一直对视频剪辑有着浓厚的兴趣,之前用爱剪辑这种通俗易上手的软件做过简单的小视频.但是这个毕竟满足不了我自己的需求而且属于完全门外汉级别.这次终于 ...

  2. Vue.js01:跑马灯效果

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 光盘安装win7系统教程

    光盘安装系统是最传统的安装系统的方法,虽然现在U盘安装和硬盘安装已经很方便,但仍有很多用户习惯光盘安装的方式,下面小编教大家如何利用光盘安装系统. 来源:https://www.haoxitongx. ...

  4. samba介绍和安装

    samba基本介绍 为什么需要samba 早期网络文件数据在不同主机之间传输大都可以使用Ftp完成,不过ftp使用有个小小的问题,它不能让你之间修改主机上的文件.要想修改必须要通过下载——修改——上传 ...

  5. 理解jQuery的$.extend与$.fn.extend

    https://www.cnblogs.com/xuxiuyu/p/5989743.html 上面这篇博客总结的很棒!!,以下对自己的认识做一个总结 <!DOCTYPE html> < ...

  6. CENTOS重新安装JDK

    centos 删除默认安装的JDK 重新安装JDK 1.删除JDK 通过xshell工具成功连接安装好的虚拟机之后可通过    rpm -qa | grep java  或 rpm -qa | gre ...

  7. springfox-swagger之swagger-bootstrap-ui

    swagger-bootstrap-ui是国内的一个swagger开源项目,从发起到现在已经有三年了.初次体验了一下,觉得还是挺不错的,就如当初使用mybatis-plus那样,因为有了mybatis ...

  8. [题解] Codeforces Round #549 (Div. 2) B. Nirvana

    Codeforces Round #549 (Div. 2) B. Nirvana [题目描述] B. Nirvana time limit per test1 second memory limit ...

  9. USB虚拟串口通信

    https://blog.csdn.net/errorhai/article/details/85333914

  10. mybatis将传入的Integer类型的0被识别成空字符串的问题

    更改mapper文件的sql如下: <if test="interger != null"> interger= #{interger} </if> 原因: ...