ATcoder 2000 Leftmost Ball
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的更多相关文章
- AtCoder AGC002F Leftmost Ball (DP、组合计数)
题目链接: https://atcoder.jp/contests/agc002/tasks/agc002_f 题解: 讲一下官方题解的做法: 就是求那个图(官方题解里的)的拓扑序个数,设\(dp[i ...
- 【AtCoder】AGC022 F - Leftmost Ball 计数DP
[题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...
- 【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 ...
- 【agc002f】Leftmost Ball(动态规划)
[agc002f]Leftmost Ball(动态规划) 题面 atcoder 洛谷 题解 我们从前往后依次把每个颜色按顺序来放,那么如果当前放的是某种颜色的第一个球,那么放的就会变成\(0\)号颜色 ...
- AtCoder Grand Contest 002 (AGC002) F - Leftmost Ball 动态规划 排列组合
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC002F.html 题目传送门 - AGC002F 题意 给定 $n,k$ ,表示有 $n\times k$ ...
- 2018.10.25 atcoder Leftmost Ball(计数dp+组合数学)
传送门 dp妙题啊. 我认为DZYODZYODZYO已经说的很好了. 强制规定球的排序方式. 然后就变成了一个求拓扑序数量的问题. 代码: #include<bits/stdc++.h> ...
- AtCoder Grand Contest 002 F:Leftmost Ball
题目传送门:https://agc002.contest.atcoder.jp/tasks/agc002_f 题目翻译 你有\(n*k\)个球,这些球一共有\(n\)种颜色,每种颜色有\(k\)个,然 ...
- *AtCoder Grand Contest 002F - Leftmost Ball
$n \leq 2000,k \leq 2000$,现$n$种球每种有$k$个,在一种排列中,会把每种颜色的球第一个出现的涂成第0种(不同于原来的n种)颜色,问最终会出现多少种不同的序列.膜1e9+7 ...
- Atcoder Grand Contest 002 F - Leftmost Ball(dp)
Atcoder 题面传送门 & 洛谷题面传送门 这道 Cu 的 AGC F 竟然被我自己想出来了!!!((( 首先考虑什么样的序列会被统计入答案.稍微手玩几组数据即可发现,一个颜色序列 \(c ...
随机推荐
- 原生js的容易忽略的相似点(一)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- php用面向对象从mysql取数据
<?php //建立数据库的链接@$_mysqli = new mysqli('localhost','root','123456','dbname');if(mysqli_connect_er ...
- SQLite -创建表
SQLite -创建表 SQLite CREATE TABLE语句用于创建一个新表在任何给定的数据库.创建一个基本表包括表命名和定义其列,每列的数据类型 语法: CREATE TABLE语句的基本语法 ...
- Python3简明教程(九)—— 文件处理
文件是保存在计算机存储设备上的一些信息或数据.你已经知道了一些不同的文件类型,比如你的音乐文件,视频文件,文本文件.Linux 有一个思想是“一切皆文件”,这在实验最后的 lscpu 的实现中得到了体 ...
- zabbix 报警通知选项配置
{TRIGGER.STATUS} host: {HOSTNAME} IP: {HOST.IP} events_time:{EVENT.DATE} {EVENT.TIME} notice_time:{D ...
- golang 解析json 动态数组
#cat file { "Bangalore_City": "35_Temperature", "NewYork_City": " ...
- 光猫&路由器网络配置
前期准备:电脑(工业电脑).网线.光猫.路由器 1.检查连接光猫后能否正常上网:把网线两头的水晶头,一头插在光猫上的千兆口,一头插在电脑(工业电脑)的网口上,看电脑能否正常上网: 可以正常上网:说明光 ...
- modify django app models.py adn settings.py
from django.db import models from django.contrib import admin # from personal import models class Us ...
- 牛客网NOIP赛前集训营-提高组(第二场)A 方差
链接:https://www.nowcoder.com/acm/contest/173/A来源:牛客网 题目描述 一个长度为 m 的序列 b[1...m] ,我们定义它的方差为 ,其中 表示序列的平 ...
- Java之浅拷贝与深拷贝
----?浅拷贝 --- 概念 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.简单说,浅拷贝就是只复制所考虑的对象,而不复制它所引用的对象 --- 实现方 ...