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. 去掉所有的html标签,得到HTML标签中的所有内容

    text——含有HTML标签的字符串 var text1=text.replace(/<\/?.+?>/g,""); text=text1.replace(/ /g,& ...

  2. slice 与 splice 的区别

    slice: 定义一个数组:let b = ['a','b','c','d','e'] b:["a", "b", "c", "d& ...

  3. 【vue】使用vue+element搭建项目,Tree树形控件使用

    1.依赖安装 本例中,使用render-content进行树节点内容的自定义,因此需要支持JSX语法.(见参考资料第3个) 在Git bash中运行一下指令 cnpm install\ babel-p ...

  4. FLIR 相机采集程序

    https://www.ptgrey.com/Downloads/GetSecureDownloadItem/11048 Grasshopper3 4.1 MP Mono USB3 Vision (C ...

  5. Winform开发的应用环境和相关技术介绍

    随着时间的推移,Winform也算是能够坚持下来最久的技术之一了,它的昔日辉煌和现今的依旧活跃,导致了它依旧拥有者很庞大的用户群体,虽然目前很多技术日新月异的,曾经的ASP.ASP.NET WebFo ...

  6. Mac下开发ASP.NET Core应用,我用FineUICore!

    在 Mac 下开发 ASP.NET Core 2.0+ 应用,我用FineUICore! FineUICore:企业级 ASP.NET 控件库,10年持续更新,只为你来:http://fineui.c ...

  7. java中异常处理finally和return的执行顺序

    根据个人亲自测试,得出如下结果: 1.try,catch中有没有return,finnally都会执行,而且是先执行try,catch后,最后执行finnally语句: 2.如果finnally中有r ...

  8. JAVA基础知识回顾(面试资料)

    关于数据库知识和面试:https://www.cnblogs.com/yanqb/p/9894943.html 关于数据库知识和面试:https://www.cnblogs.com/yanqb/p/1 ...

  9. ansible-playbook(nginx例)

    一.创建目录结构 cd /etc/ansible/roles/ mkdir nginx/{files,templates,vars,handlers,meta,default,tasks} -pv 二 ...

  10. 译注(3): NULL-计算机科学上最糟糕的失误

    原文:the worst mistake of computer science 注释:有些术语不知道怎么翻译,根据自己理解的意思翻译了,如有不妥,敬请提出:) 致谢: @vertextao @fra ...