[AT2000] [agc002_f] Leftmost Ball
题目链接
AtCoder:https://agc002.contest.atcoder.jp/tasks/agc002_f
洛谷:https://www.luogu.org/problemnew/show/AT2000
Solution
对于一个任意的颜色序列,它合法当且仅当任意一个前缀序列都要满足白色数量大于等于颜色种类数(不包括白色)。
设\(f[i][j]\)表示当前填了\(i\)个白球,\(j\)种其他颜色的球的方案数,显然当\(i<j\)时\(f[i][j]=0\)。
考虑转移,我们考虑每次都填最前面那个没填过的位置:
- 填白球不会有任何影响,直接转移就好了。
- 否则我们可以从剩下\(n-(j-1)\)种颜色种任选一种,一个填在第一个能填的位置,剩下的\(k-2\)个填在剩下\(n*k-i-(j-1)(k-1)-1\)个位置里。
转移式子就是:
\]
暴力转移就好了,初值\(f[0][0]=1\),时间复杂度\(O(n^2)\)。
#include<bits/stdc++.h>
using namespace std;
void read(int &x) {
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
void print(int x) {
if(x<0) putchar('-'),x=-x;
if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}
#define lf double
#define ll long long
const int maxn = 2e3+10;
const int maxm = maxn*maxn;
const int inf = 1e9;
const lf eps = 1e-8;
const int mod = 1e9+7;
int add(int x,int y) {return x+y>mod?x+y-mod:x+y;}
int del(int x,int y) {return x-y<0?x-y+mod:x-y;}
int mul(int x,int y) {return 1ll*x*y-1ll*x*y/mod*mod;}
int n,k,f[maxn][maxn],fac[maxm],ifac[maxm],inv[maxm];
void prepare() {
inv[0]=inv[1]=fac[0]=ifac[0]=f[0][0]=1;
for(int i=2;i<maxm;i++) inv[i]=mul(mod-mod/i,inv[mod%i]);
for(int i=1;i<maxm;i++) fac[i]=mul(fac[i-1],i);
for(int i=1;i<maxm;i++) ifac[i]=mul(ifac[i-1],inv[i]);
}
int c(int nn,int mm) {return mul(fac[nn],mul(ifac[mm],ifac[nn-mm]));}
int main() {
read(n),read(k);prepare();if(k==1) puts("1"),exit(0);
for(int i=1;i<=n;i++)
for(int j=0;j<=i;j++)
f[i][j]=add(f[i-1][j],mul(f[i][j-1],mul(n-j+1,c(n*k-i-(k-1)*(j-1)-1,k-2))));
write(f[n][n]);
return 0;
}
[AT2000] [agc002_f] Leftmost Ball的更多相关文章
- 【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】AGC022 F - Leftmost Ball 计数DP
[题目]F - Leftmost Ball [题意]给定n种颜色的球各k个,每次以任意顺序排列所有球并将每种颜色最左端的球染成颜色0,求有多少种不同的颜色排列.n,k<=2000. [算法]计数 ...
- ATcoder 2000 Leftmost Ball
Problem Statement Snuke loves colorful balls. He has a total of N×K balls, K in each of his favorite ...
- AT2000 Leftmost Ball
设\(f[i][j]\)表示当前有\(i\)个白球,一共放完了\(j\)种球 显然有\(j <= i\) 对于每个状态目前已经放下去的球是固定了的,那么考虑转移 放白球 从\(f[i - 1][ ...
- AT2000 Leftmost Ball(计数dp+组合数学)
传送门 解题思路 设\(f[i][j]\)表示填了\(i\)个白色,\(j\)种彩色的方案数,那么显然\(j<=i\).考虑这个的转移,首先可以填一个白色,就是\(f[i][j]=f[i-1][ ...
- AT2000 Leftmost Ball 解题报告
题面 给你n种颜色的球,每个球有k个,把这n*k个球排成一排,把每一种颜色的最左边出现的球涂成白色(初始球不包含白色),求有多少种不同的颜色序列,答案对1e9+7取模 解法 设\(f(i,\;j)\) ...
- AtCoder Grand Contest 002 F:Leftmost Ball
题目传送门:https://agc002.contest.atcoder.jp/tasks/agc002_f 题目翻译 你有\(n*k\)个球,这些球一共有\(n\)种颜色,每种颜色有\(k\)个,然 ...
- AtCoder AGC002F Leftmost Ball (DP、组合计数)
题目链接: https://atcoder.jp/contests/agc002/tasks/agc002_f 题解: 讲一下官方题解的做法: 就是求那个图(官方题解里的)的拓扑序个数,设\(dp[i ...
随机推荐
- 2017"百度之星"程序设计大赛 - 初赛(A) 小C的倍数问题
谢谢帮忙刷访问量! 题解当然下考再发啦 答案为P-1的约数个数 // It is made by XZZ #include<cstdio> #include<algorithm> ...
- WPF DataGridTable
由于项目要显示表头合并,而数据源列随时变更,又不想重复的画表格,就实现动态数据(dynamic)绑定和配置数据列模板的方式 编辑DataGridColumnHeader样式实现表头合并:效果如下 实现 ...
- 深入解析QML引擎, 第4部分: 自定义解析器
原文 QML Engine Internals, Part 4: Custom Parsers ——————————————————————————————————————————— 上一篇 绑定类型 ...
- docker制作自己的镜像并上传dockerhub
1.首先注册自己的dockerhub账号,注册地址:https://hub.docker.com 2.在linux服务器登录自己的账号:docker login --username=qiaoyeye ...
- async+await 让界面飞,让双手爽
.net 4.5已经发布很久了,但是一直也没有静下心来好好的研究微软给开发者带来的喜悦. 今天我将简单的介绍下 async + await 这对搭档的出现,如何让频繁假死的界面飞起来(其实只是不再阻塞 ...
- 内容安全策略(CSP)
内容安全策略(CSP),其核心思想十分简单:网站通过发送一个 CSP 头部,来告诉浏览器什么是被授权执行的与什么是需要被禁止的.其被誉为专门为解决XSS攻击而生的神器. 1.CSP是什么 CSP指的是 ...
- appium+python自动化☞appium python api大全
整理了一些常用的appium python api,供学习使用...
- Power Designer逆向工程导入Oracle表,转为模型加注释
1.打开PowerDesigner ——文件——Reverse Engineer——DataBase 2.选择所要连接数据库版本,此处使用的是oracle version 11g. 3.点击红色区域, ...
- TPO-18 C1 Apply for a part-time job on campus
TPO-18 C1 Apply for a part-time job on campus 第 1 段 1.Listen to a conversation between a student and ...
- python-redis哈希模式
命令: hset info name hgetall info hkeys info hvlls info m系列批量处理: ---------------------------------- ...