Problem Statement

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.

Constraints

  • 1≤N,K≤2,000

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.

Sample Input 1

2 2

Sample Output 1

4

The following 4 sequences are possible:

  • (0,1,0,2)
  • (0,0,1,2)
  • (0,2,0,1)
  • (0,0,2,1)

Sample Input 2

3 1

Sample Output 2

1

The following 1 sequence is possible:

  • (0,0,0)

Sample Input 3

2 3

Sample Output 3

14

Sample Input 4

2000 2000

Sample Output 4

546381702

    不妨把0颜色看成白球,把其他颜色看成彩球,那么我们来讨论一下什么样的序列是合法的。
仅有一个序列中的每一个白球都能在其后方匹配到一个是其颜色里出现第一次的彩球,这个序列才合法。
所以我们就可以从后向前dp,状态的定义和转移在代码里都有,只不过特判一下k==1直接输出1就好了。
/*
f[i][j] 表示 已经出现过 i种颜色的球,
并且还剩j种颜色的球没有被匹配的方案数。 考虑加入一种新球的话:
f[i+1][j+1] += f[i][j] * (n-i) * C(k*(i+1)-j-2,k-2) 考虑加入一种白球的话:
f[i][j-1] += f[i][j] 初始化 f[0][0] = 1
*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2005;
const int ha=1000000007;
int jc[maxn*maxn],ni[maxn*maxn];
int n,k,f[maxn][maxn]; inline int add(int x,int y){
x+=y;
return x>=ha?x-ha:x;
} inline int ksm(int x,int y){
int an=1;
for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha;
return an;
} inline void init(){
jc[0]=1;
for(int i=1;i<=4004000;i++) jc[i]=jc[i-1]*(ll)i%ha;
ni[4004000]=ksm(jc[4004000],ha-2);
for(int i=4004000;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
} inline int getC(int x,int y){
return jc[x]*(ll)ni[y]%ha*(ll)ni[x-y]%ha;
} inline void dp(){
f[0][0]=1;
for(int i=0;i<=n;i++)
for(int j=i;j>=0;j--) if(f[i][j]){
f[i+1][j+1]=add(f[i+1][j+1],f[i][j]*(ll)(n-i)%ha*(ll)getC(k*(i+1)-j-2,k-2)%ha);
if(j) f[i][j-1]=add(f[i][j-1],f[i][j]);
} /*
for(int i=0;i<=n;i++){
for(int j=0;j<=i;j++) printf("%d ",f[i][j]);
puts("");
}
*/
} int main(){
init();
scanf("%d%d",&n,&k);
if(k==1) puts("1");
else{
dp();
printf("%d\n",f[n][0]);
}
return 0;
}

  

 

ATcoder 2000 Leftmost Ball的更多相关文章

  1. AtCoder AGC002F Leftmost Ball (DP、组合计数)

    题目链接: https://atcoder.jp/contests/agc002/tasks/agc002_f 题解: 讲一下官方题解的做法: 就是求那个图(官方题解里的)的拓扑序个数,设\(dp[i ...

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

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

  3. 【AGC 002F】Leftmost Ball

    Description Snuke loves colorful balls. He has a total of N*K balls, K in each of his favorite N col ...

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

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

  5. AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合

    原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...

  6. 2018.10.25 atcoder Leftmost Ball(计数dp+组合数学)

    传送门 dp妙题啊. 我认为DZYODZYODZYO已经说的很好了. 强制规定球的排序方式. 然后就变成了一个求拓扑序数量的问题. 代码: #include<bits/stdc++.h> ...

  7. AtCoder Grand Contest 002 F:Leftmost Ball

    题目传送门:https://agc002.contest.atcoder.jp/tasks/agc002_f 题目翻译 你有\(n*k\)个球,这些球一共有\(n\)种颜色,每种颜色有\(k\)个,然 ...

  8. *AtCoder Grand Contest 002F - Leftmost Ball

    $n \leq 2000,k \leq 2000$,现$n$种球每种有$k$个,在一种排列中,会把每种颜色的球第一个出现的涂成第0种(不同于原来的n种)颜色,问最终会出现多少种不同的序列.膜1e9+7 ...

  9. Atcoder Grand Contest 002 F - Leftmost Ball(dp)

    Atcoder 题面传送门 & 洛谷题面传送门 这道 Cu 的 AGC F 竟然被我自己想出来了!!!((( 首先考虑什么样的序列会被统计入答案.稍微手玩几组数据即可发现,一个颜色序列 \(c ...

随机推荐

  1. PHP memcache扩展安装 for Windows

    一.下载并安装memcached服务器端软件    1.下载memcached软件 32位下载地址: memcached-win32-1.4.4-14.zip(直接下载),memcached-win3 ...

  2. 洛谷 P2580 于是他错误的点名开始了

    题目背景 XS中学化学竞赛组教练是一个酷爱炉石的人. 他会一边搓炉石一边点名以至于有一天他连续点到了某个同学两次,然后正好被路过的校长发现了然后就是一顿欧拉欧拉欧拉(详情请见已结束比赛CON900). ...

  3. MongoDB最简单的入门教程之四:使用Spring Boot操作MongoDB

    Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作.Spring Boot的目的是提供一组工具,以便快速构建容易配置的Spring应用程序,省去大量传统S ...

  4. dircolors - 设置‘ls'显示结果的颜色

    SYNOPSIS[总览] dircolors [-b] [--sh] [--bourne-shell] [-c] [--csh] [--c-shell] [-p] [--print-database] ...

  5. faster rcnn的改进方向

    http://blog.csdn.net/linolzhang/article/details/74159463 http://blog.csdn.net/linolzhang/article/det ...

  6. PHP11 日期和时间

    学习要点 UNIX时间戳 将其他格式的日期转成UNIX时间戳格式 基于UNIX时间戳的日期计算 获取并格式化输出日期 修改PHP的默认时间 微秒的使用    Unix时间戳 相关概念 Unix tim ...

  7. android开发链接

    http://blog.csdn.net/zz2043191420/article/details/47338591

  8. 前段开发 react native tab功能

    import React, { Component } from 'react'; import { StyleSheet, Text, View, Image, } from 'react-nati ...

  9. Java中的线程安全和非线程安全以及锁的几个知识点

    1. 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读取完,其他线程才可使用.不会出现数据不一致或者数据污染. 线程不安全就是不提供 ...

  10. [JOYOI] 1061 Mobile Service

    题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 一个公司有三个移动服务员.如果某个地方有一个请求,某个员工必须赶到那个地方去(那个 ...