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 ...
随机推荐
- Java编译时根据调用该方法的类或对象所属的类决定
class Base{ int x = 1; static int y = 2; } class Subclass extends Base{ int x = 4; i ...
- Jenkins邮件扩展插件Email Extension Plugin配置使用
1.在管理插件中搜索并安装邮件扩展插件Email Extension Plugin: 2.在任务中增加构建后操作步骤,选择Editable Email Notification; 3.在高级中Add ...
- tomcat 安全配置文档
1.配置文档中使用$CATALINA_HOME变量声明为tomcat的安装目录并明确写出了tomcat的配置文件路径,此路径为测试环境的路径,线上系统对应配置文件的路径可能不一样,在进行相关配置时,应 ...
- 使用python书写的小说爬虫
1.写了一个简单的网络爬虫 初期1 (后期将会继续完善) #小说的爬取 import requests import random from bs4 import BeautifulSoup base ...
- Django-常用设置(settings.py)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 当前项目的根目录,Django会依此来定位工程内的相关文件 ...
- 基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table
基于VueJS的render渲染函数结合自定义组件打造一款非常强大的IView 的Table https://segmentfault.com/a/1190000015970367
- dockerfile 的最佳实践
Dockerfile 编写nginx容器 [root@mast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y ...
- chrome ubuntu启动不了
安装好后,点击图标没反应 用命令行运行chrome并显示log:google-chrome --enable-logging=stderr --log-level=4 报错如下: 解决:安装更高版本的 ...
- PHP03 移动互联网和PHP
学习要点 移动互联网 云计算 网络通信协议 Apache http服务器 PHP运行原理 学习目标 理解网络通信协议 掌握PHP运行原理 WAMP开发环境的搭建 移动互联网 定义 移动互联网,就是 ...
- Sybase PowerDesigner 16.5注册码
Sybase PowerDesigner16.5安装之后一段时间就会过期,破解办法是:将pdflm16.dll文件覆盖原来的即可. pdflm16.dll文件下载地址:链接:https://pan.b ...