题目链接

题意

\(A+B\)个球排成一行,左边\(A\)个为红球,右边\(B\)个为蓝球。

最开始可以选择两个数\(s,t\),每次操作可以取左起第\(1\)或\(s\)或\(t\)个球。问有多少种不同的取球序列。

Sample

Sample Input 1

3 3

Sample Output 1

20

Explanation

There are 20 ways to give 3 red balls and 3 blue balls. It turns out that all of them are possible.

Here is an example of the operation (r stands for red, b stands for blue):

You choose s=3,t=4.

Initially, the row looks like rrrbbb.

You remove 3rd ball (r) and give it to Snuke. Now the row looks like rrbbb.

You remove 4th ball (b) and give it to Snuke. Now the row looks like rrbb.

You remove 1st ball (r) and give it to Snuke. Now the row looks like rbb.

You remove 3rd ball (b) and give it to Snuke. Now the row looks like rb.

You remove 1st ball (r) and give it to Snuke. Now the row looks like b.

You remove 1st ball (b) and give it to Snuke. Now the row is empty.

This way, Snuke receives balls in the order rbrbrb.

思路

官方题解

将剩下的球的序列转化成二维平面上的点,则取球过程为起点为\((A,B)\),终点为\((0,0)\)的路径。

显然,可以一直向左走,因为向左走就对应着取该序列的第一个元素;

但是向下走是需要满足一定的条件的,那就是红球的个数小于\(s\)或\(t\)中的某一个。

在阴影区域内可以随意向左走或向下走。

因此,可以枚举\((p,q)\),从\((A,B)\)到\((p,q)\)的路径条数可以通过预处理组合数然后\(O(1)\)算出,而从\((p,q)\)到\((0,0)\)的路径条数可以预处理出。

以\(q-1=4\)为例,

分界线为\(x=0\)时,有\(\binom{4}{0}\)种,

分界线为\(x=1\)时,有\(\binom{4}{0}+\binom{4}{1}\)种,

分界线为\(x=2\)时,有\(\binom{4}{0}+\binom{4}{1}+\binom{4}{2}\)种,

分界线为\(x=3\)时,有\(\binom{4}{0}+\binom{4}{1}+\binom{4}{2}+\binom{4}{3}\)种,

分界线为\(x=4\)时,有\(\binom{4}{0}+\binom{4}{1}+\binom{4}{2}+\binom{4}{3}+\binom{4}{4}\)种,

分界线为\(x=5\)时,有\(\binom{4}{0}+\binom{4}{1}+\binom{4}{2}+\binom{4}{3}+\binom{4}{4}\)种,

……

分界线为\(x=n(n\geq 4)\)时,有\(\binom{4}{0}+\binom{4}{1}+\binom{4}{2}+\binom{4}{3}+\binom{4}{4}\)种.

对于\((p,q)\),将分界线为\(x=0,1,2,...,p\)的情况累和,即得路径条数。

Code

参考:

#include <bits/stdc++.h>
#define maxn 4000
#define maxm maxn+10
using namespace std;
typedef long long LL;
LL temp[maxm][maxm], C[maxm][maxm];
const LL mod = 1e9+7;
LL add(LL a, LL b) { return (a+b) % mod; }
LL mul(LL a, LL b) { return (a*b) % mod; }
void init() {
for (int i = 0; i <= maxn; ++i) C[i][0] = 1;
for (int i = 1; i <= maxn; ++i) {
for (int j = 1; j <= i; ++j) C[i][j] = add(C[i-1][j], C[i-1][j-1]);
}
for (int i = 0; i <= maxn; ++i) {
temp[i][0] = C[i][0];
for (int j = 1; j <= maxn; ++j) {
temp[i][j] = add(temp[i][j-1], C[i][j]);
}
}
for (int i = 0; i <= maxn; ++i) {
for (int j = 1; j <= maxn; ++j) temp[i][j] = add(temp[i][j], temp[i][j-1]);
}
}
int main() {
init();
int a, b;
LL ans = 0;
scanf("%d%d", &a, &b);
for (int p = 0; p <= a; ++p) {
for (int q = 0; q <= b-1; ++q) {
if (p+q > a) continue;
if (q == 0) ans = add(ans, 1);
else ans = add(ans, mul(C[b-1][q], temp[q-1][p]));
}
}
printf("%lld\n", ans);
return 0;
}

Atcoder CODE FESTIVAL 2017 qual B E - Popping Balls 组合计数的更多相关文章

  1. Code Festival 2017 Qual B E Popping Balls

    传送门 神仙计数! 我的计数真的好差啊= = 不过这个题真的神仙 看了题解把整个过程在草稿纸上重写了一遍才想明白= =(一张草稿纸就没有了!!!) 计数的关键就是在于 枚举的有效性和独立性[不能重复计 ...

  2. 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数

    蒟蒻__stdcall终于更新博客辣~ 一下午+一晚上=一道计数题QAQ 为什么计数题都这么玄学啊QAQ Prelude 题目链接:这里是传送门= ̄ω ̄= 下面我将分几个步骤讲一下这个题的做法,大家不 ...

  3. Atcoder CODE FESTIVAL 2017 qual B D - 101 to 010 dp

    题目链接 题意 对于一个\(01\)串,如果其中存在子串\(101\),则可以将它变成\(010\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...

  4. 题解【AtCoder - CODE FESTIVAL 2017 qual B - D - 101 to 010】

    题目:https://atcoder.jp/contests/code-festival-2017-qualb/tasks/code_festival_2017_qualb_d 题意:给一个 01 串 ...

  5. atcoder/CODE FESTIVAL 2017 qual B/B(dfs染色判断是否为二分图)

    题目链接:http://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_c 题意:给出一个含 n ...

  6. Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分

    题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...

  7. Atcoder CODE FESTIVAL 2017 qual C C - Inserting 'x' 回文串

    题目链接 题意 给定字符串\(s\),可以在其中任意位置插入字符\(x\). 问能否得到一个回文串,若能,需插入多少个\(x\). 思路 首先统计出现次数为奇数的字符\(cnt\). \(cnt\ge ...

  8. Atcoder CODE FESTIVAL 2017 qual B C - 3 Steps 二分图

    题目链接 题意 给定一个无向图,\(n\)个点,\(m\)条边(\(n,m\leq 1e5\)). 重复如下操作: 选择相异的两点u,v满足从点u出发走三条边恰好能到达点v.在这样的u,v点对之间添一 ...

  9. [Atcoder Code Festival 2017 Qual A Problem D]Four Coloring

    题目大意:给一个\(n\times m\)的棋盘染四种颜色,要求曼哈顿距离为\\(d\\)的两个点颜色不同.解题思路:把棋盘旋转45°,则\((x,y)<-(x+y,x-y)\).这样就变成了以 ...

随机推荐

  1. eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?

    eclipse 中main()函数中的String[] args如何使用? 右击你的项目,选择run as中选择 run configuration,选择arguments总的program argu ...

  2. matplotlib subplot 子图

    总括 MATLAB和pyplot有当前的图形(figure)和当前的轴(axes)的概念,所有的作图命令都是对当前的对象作用.可以通过gca()获得当前的axes(轴),通过gcf()获得当前的图形( ...

  3. 【贪心】bzoj1592: [Usaco2008 Feb]Making the Grade 路面修整

    贪心的经典套路:替换思想:有点抽象 Description FJ打算好好修一下农场中某条凹凸不平的土路.按奶牛们的要求,修好后的路面高度应当单调上升或单调下降,也 就是说,高度上升与高度下降的路段不能 ...

  4. 洛谷 P1147 连续自然数和

    洛谷 P1147 连续自然数和 看到dalao们的各种高深方法,本蒟蒻一个都没看懂... 于是,我来发一篇蒟蒻友好型的简单题解 #include<bits/stdc++.h> using ...

  5. 201621123080《Java程序设计》第三周学习总结

    Week03-面向对象入门 1. 本周学习总结 2. 书面作业 1.以面向对象方式改造数据结构作业'有理数'(重点) 1.1 截图你主要代码(需要在程序中出现你的学号和姓名)并粘贴程序的git地址. ...

  6. Linux基础学习-LVM逻辑卷管理遇到的问题

    LVM学习逻辑卷管理创建逻辑卷遇到的问题 1 实验环境 系统 内核 发行版本 CentOS 2.6.32-754.2.1.el6.x86_64 CentOS release 6.10 (Final) ...

  7. Python爬虫二

    常见的反爬手段和解决思路 1)明确反反爬的主要思路 反反爬的主要思路就是尽可能的去模拟浏览器,浏览器在如何操作,代码中就如何去实现;浏览器先请求了地址url1,保留了cookie在本地,之后请求地址u ...

  8. Applied Nonparametric Statistics-lec5

    今天继续two-sample test Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/6 Mann ...

  9. POJ:1753-Flip Game(二进制+bfs)

    题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Description Fl ...

  10. TCP的三次握手和四次握手

    三次握手(建立连接) 首先,服务器进程(B)先创建传控制块TCB(用来存储连接信息,如连接表,发送和接收序号等),准备接收客户进程(A)的请求.然后服务器进程处于LISTEN(收听)状态,等待客户的连 ...