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. MySQL8.0 on Windows下重置root密码的BUG

    很多人都知道MySQL忘记root密码之后可以通过skip-grant-tables来暂时免密登录MySQL,从而修改root密码,但是这种方式一方面有安全隐患,另一方面也并不怎么适用于Windows ...

  2. thinkPHP HTML输出多条查询结果

    控制端 $select=M('Baojiao')->query("select * from think_Baojiao where COMPANY='$comname'") ...

  3. Linux云计算运维-Redis

    Redis简介 Redis是一款开源的,ANSI C语言编写的,高级键值(key-value)缓存和支持永久存储NoSQL数据库产品. Redis采用内存(In-Memory)数据集(DataSet) ...

  4. P1551 亲戚题解

    标准并查集板子题 没啥好说的,分明是白书上的(除了输入方式外一点都没改动) #include<cstdio> #include<iostream> using namespac ...

  5. 10-PI开发手册-ERP发布服务供外围系统调用(RFC类型)

      一.      文档信息 版本号* 更新日期* 姓名* 更新内容及更新理由* 备注* V1.0 2019/02/19 fanjb 文档创建 以福利接口13589(Z00HRJ_GJJ_REV_FI ...

  6. Generetor函数与线程之间的思考

    在解析这个问题之前,首先,我们来了解一下es6标准里新增解决异步的两种规范 Promise与Generetor Promise 其实Promise的本质 还是基于js程式的回调处理----这一点看它的 ...

  7. 03-MySQL表操作

    MySQL表操作 1.介绍 表就相当于文件,表中的一条记录就相当与文件的一行内容,不同的是,表中的一条记录有对应的标题,成为表的字段. 2.创建表 2.1语法 create table 表名( 字段名 ...

  8. c# 串口SerialPort

    创建SerialPortFun类 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  9. Playfair 加密

    题目真的好长但是意思很简单 89.加密 (15分)C时间限制:3 毫秒 | C内存限制:3000 Kb题目内容:一种Playfair密码变种加密方法如下:首先选择一个密钥单词(称为pair)(字母不重 ...

  10. Java导出压缩包工具类

    /** * @Title: exportZip * @Description:TODO(导出建压缩文件) * @param delAdviceinfo 为项目实体类 * @date 2018年4月4日 ...