当前位置:网站首页>【AtCoder2000】Leftmost Ball (DP+组合数)
【AtCoder2000】Leftmost Ball (DP+组合数)
2022-06-11 07:23:00 【CaptainHarryChen】
题意
Snuke喜欢五颜六色的球。他总共有N×K个球,有N种颜色,每种颜色的球有K个。颜色编号为1到N。他将按照任意顺序排列所有球。然后,对于每种颜色,他将该颜色的最左边的球涂成颜色0,颜色0不同于N种原始颜色中的任何颜色。如将球排列为(1,2,1,2),染色后就变为 (0,0,1,2)。
所有操作后,球的颜色序列有多少种,求这个方案数mod 10^9+7。
题解
发现一个性质:对任何前缀,0的个数必须≥ ≥ 出现的其它颜色种类数
可定义dp[i][j] d p [ i ] [ j ] 表示有i个0,j种颜色都已经放完的方案数
转移:
新放了一个0
dp[i][j]+=dp[i−1][j] d p [ i ] [ j ] + = d p [ i − 1 ] [ j ]
新放了一种颜色,这个颜色的第一个球必须放在最开始(表示这时候已经出现过这种颜色),(还有一个球变为了0)这种颜色还剩下K−2 K − 2 个球可以随便放,则需乘上组合数
dp[i][j]+=dp[i][j−1]×CK−2N×K−i−(j−1)(K−1)−1 d p [ i ] [ j ] + = d p [ i ] [ j − 1 ] × C N × K − i − ( j − 1 ) ( K − 1 ) − 1 K − 2
最后答案为dp[N][N]×N! d p [ N ] [ N ] × N ! (dp时没有考虑颜色的区别,还需乘上颜色的排列数)
代码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=2005,MOD=1000000007;
int PowMod(int a,int b)
{
int ret=1;
while(b)
{
if(b&1)
ret=1LL*ret*a%MOD;
a=1LL*a*a%MOD;
b>>=1;
}
return ret;
}
int fac[MAXN*MAXN],ifac[MAXN*MAXN];
void Init()
{
fac[0]=1;
for(int i=1;i<MAXN*MAXN;i++)
fac[i]=1LL*fac[i-1]*i%MOD;
ifac[MAXN*MAXN-1]=PowMod(fac[MAXN*MAXN-1],MOD-2);
for(int i=MAXN*MAXN-2;i>=0;i--)
ifac[i]=1LL*ifac[i+1]*(i+1)%MOD;
}
int C(int n,int r)
{
return 1LL*fac[n]*ifac[n-r]%MOD*ifac[r]%MOD;}
int N,K,dp[MAXN][MAXN];
int main()
{
Init();
scanf("%d%d",&N,&K);
if(K==1)
{
puts("1");
return 0;
}
dp[0][0]=1;
for(int i=0;i<=N;i++)
for(int j=0;j<=i;j++)
{
if(i)
dp[i][j]=(dp[i][j]+dp[i-1][j])%MOD;
if(j)
dp[i][j]=(dp[i][j]+1LL*dp[i][j-1]*C(N*K-i-(K-1)*(j-1)-1,K-2)%MOD)%MOD;
}
int ans=1LL*dp[N][N]*fac[N]%MOD;
printf("%d\n",ans);
return 0;
}边栏推荐
- P3327 [sdoi2015] approximate sum (Mobius inversion + formula)
- Compound RateModel合約解析
- Education expert wangzhongze solves students' problems with one move
- JVM学习记录(七)——类加载过程与双亲委派模型
- MS office level II wrong question record [10]
- 【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
- MS office level II wrong question record [9]
- QT 基于QScrollArea的界面嵌套移动
- Education expert Mr. wangzhongze: family education focuses on self growth
- 【CF#693 (Div. 3)】B. Fair Division
猜你喜欢

Biological sequence intelligent analysis platform blog (1)

big.js--使用/实例

Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please

big. Js-- use / instance

JVM Learning record (7) - - class Loading Process and parent delegation Model

一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试

二、用户登录和注册

资深OpenStacker - 彭博、Vexxhost升级为OpenInfra基金会黄金成员

Education expert wangzhongze solves students' problems with one move

【Oracle 数据库】奶妈式教程day04 排序查询
随机推荐
C language inherits memory management mechanism (unfinished)
【CF#693 (Div. 3)】B. Fair Division
nosqlzoo刷题-1
C memory alignment
12. integer to Roman numeral
Post-processing of ffmpeg miscellaneous notes
QT interface nested movement based on qscrollarea
Seata的几种事务模式
@JsonProperty注解
Summary of classic interview questions
Menu double linkage effect in uniapp
Android and IOS reverse analysis / security detection / penetration testing framework
1734. arrangement after decoding XOR
[STL source code analysis] summary notes (10): hashtable exploration
Education expert wangzhongze solves students' problems with one move
Use definite integral to calculate triangle area
Adventure of small X
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台
R语言并行计算实战教程
【Oracle 数据库】奶妈式教程day03 排序查询