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. 【MySQL大系】《Mysql集群架构》

    原文地址(微信):[技术文章]<Mysql集群架构> 本文地址:http://www.cnblogs.com/aiweixiao/p/7258444.html 点击关注微信公众号 1.主要 ...

  2. python open 函数的读写追加

  3. idea标注yml资源配置文件

  4. SpringBoot:Invalid character found in method name. HTTP method names must be tokens

    问题背景 关于SpringBoot应用挂了很久之后,会发生Invalid character found in method name. HTTP method names must be token ...

  5. DAY22、面向对象

    一.复习:1.面向过程与面向对象 过程:程序流程化,可拓展性差 对象:程序流程多样化,可拓展性强 面向对象引入属性 | 方法的概念,通过所属者.语法调用2.拥有名称空间的对象:有__dict__属性, ...

  6. Conda常见命令

    Anaconda,Miniconda,Conda,Pip的区别: Anaconda:用于科学计算的python发行版,里面预装好了conda,某个版本的python,众多packages,科学计算工具 ...

  7. 多线程的休息室WaitSet详细介绍

    1. 所有对象都会有一个wait set,用于存放调用了该对象wait方法之后进入block状态的线程; 2. 线程被notify之后,不一定会立即执行; 3. 线程从wait set中唤醒的顺序不一 ...

  8. Making every developer more productive with Visual Studio 2019

    Today, in the Microsoft Connect(); 2018 keynote, Scott Guthrie announced the availability of Visual ...

  9. jmeter压测数据库,抓包工具,python基础

    jmeter压力测试 前提场景的设置:单场景(单个接口进行压力测试一个请求)或混合场景(有业务流程的场景进行压力测试多个请求),压测时间一般在5--1515分组具体看需求. 数据准备:数据量少和数据量 ...

  10. Q_UNUSED 的使用

    在构建比较大型的工程的时候,若继承,重构虚函数,写数据model等等比较多的时候,会给出大量的  “未引用的形参” ,“warning: unused parameter ” 的告警.这种告警多了会影 ...