Atcoder CODE FESTIVAL 2017 qual B E - Popping Balls 组合计数
题目链接
题意
\(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 组合计数的更多相关文章
- Code Festival 2017 Qual B E Popping Balls
传送门 神仙计数! 我的计数真的好差啊= = 不过这个题真的神仙 看了题解把整个过程在草稿纸上重写了一遍才想明白= =(一张草稿纸就没有了!!!) 计数的关键就是在于 枚举的有效性和独立性[不能重复计 ...
- 【题解】Popping Balls AtCoder Code Festival 2017 qual B E 组合计数
蒟蒻__stdcall终于更新博客辣~ 一下午+一晚上=一道计数题QAQ 为什么计数题都这么玄学啊QAQ Prelude 题目链接:这里是传送门= ̄ω ̄= 下面我将分几个步骤讲一下这个题的做法,大家不 ...
- Atcoder CODE FESTIVAL 2017 qual B D - 101 to 010 dp
题目链接 题意 对于一个\(01\)串,如果其中存在子串\(101\),则可以将它变成\(010\). 问最多能进行多少次这样的操作. 思路 官方题解 转化 倒过来考虑. 考虑,最终得到的串中的\(' ...
- 题解【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 串 ...
- atcoder/CODE FESTIVAL 2017 qual B/B(dfs染色判断是否为二分图)
题目链接:http://code-festival-2017-qualb.contest.atcoder.jp/tasks/code_festival_2017_qualb_c 题意:给出一个含 n ...
- Atcoder CODE FESTIVAL 2017 qual C D - Yet Another Palindrome Partitioning 回文串划分
题目链接 题意 给定一个字符串(长度\(\leq 2e5\)),将其划分成尽量少的段,使得每段内重新排列后可以成为一个回文串. 题解 分析 每段内重新排列后是一个回文串\(\rightarrow\)该 ...
- Atcoder CODE FESTIVAL 2017 qual C C - Inserting 'x' 回文串
题目链接 题意 给定字符串\(s\),可以在其中任意位置插入字符\(x\). 问能否得到一个回文串,若能,需插入多少个\(x\). 思路 首先统计出现次数为奇数的字符\(cnt\). \(cnt\ge ...
- Atcoder CODE FESTIVAL 2017 qual B C - 3 Steps 二分图
题目链接 题意 给定一个无向图,\(n\)个点,\(m\)条边(\(n,m\leq 1e5\)). 重复如下操作: 选择相异的两点u,v满足从点u出发走三条边恰好能到达点v.在这样的u,v点对之间添一 ...
- [Atcoder Code Festival 2017 Qual A Problem D]Four Coloring
题目大意:给一个\(n\times m\)的棋盘染四种颜色,要求曼哈顿距离为\\(d\\)的两个点颜色不同.解题思路:把棋盘旋转45°,则\((x,y)<-(x+y,x-y)\).这样就变成了以 ...
随机推荐
- Java迭代器问题 有100个人围成一个圈从1开始报数,报到14的这个人就要退出,然后其他人重新开始,从1报数,到14退出问:最后剩下的是100人中的第几个人 用listIterator迭代元素,并对集合进行删除操作
package com.swift; import java.util.ArrayList; import java.util.List; import java.util.ListIterator; ...
- css文件和js文件后面带一个问号----2015-1103
经常看一些网站页面源代码中的css文件和js文件后面带一个问号,后面跟着一连串数字或字符,这是干什么用的? 这个方法我也用过,而且很好用?,它的作用有两个:1.作为版本号,让自己方便记忆.查找:2.作 ...
- 【数学 技巧】divisor
没考虑重复lcm处理被卡TLE没A真是可惜 题目大意 $n$为$k-可表达的$当且仅当数$n$能被表示成$n$的$k$个因子之和,其中$k$个因子允许相等. 求$[A,B]$之间$k-可表达$的数的个 ...
- 标准C++(3)重载
一.函数的重载 c++中同一作用域下能够定义同名的函数(这就叫重载),但必须满足如下要求: 1.函数的参数列表必须不同,可以使参数数量不同,也可以使参数的类型不同,甚至是参数的顺序不同. 2.函数的返 ...
- nginx作为正向代理,反向代理的一些应用
正向代理代理的对象是客户端 反向代理代理的对象是服务端 举例说下nginx作为正向代理作访问控制 server{ listen 80; server_name localhost jeson.gaos ...
- #pragma与_Pragma(转载)
C90为预处理指令家族带来一位新成员:#pragma.一般情况下,大家很少见到它. #pragma的作用是为特定的编译器提供特定的编译指示,这些指示是具体针对某一种(或某一些)编译器的, ...
- Mysql之查看数据库版本
Mysql版本: 登入数据库的时候: select @@version; select version(); mysql> select @@version; +-----------+ | @ ...
- ubuntu 设置Path 开机启动脚本
vim /etc/rc.local export PATH=$PATH:/work/apps/node-v0.12.7-linux-x64/bin:/work/apps/ledisdb/binexpo ...
- BZOJ 5441: [Ceoi2018]Cloud computing
背包 #include<cstdio> #include<algorithm> using namespace std; int n,m,Len; long long F[2] ...
- HDU 3727 Jewel 主席树
题意: 一开始有一个空序列,然后有下面四种操作: Insert x在序列尾部加入一个值为\(x\)的元素,而且保证序列中每个元素都互不相同. Query_1 s t k查询区间\([s,t]\)中第\ ...