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)\).这样就变成了以 ...
随机推荐
- UI调试神器 for ios:Reveal的使用与破解
aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAkACQAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aH
- TypeError: Cannot read property 'tap' of undefined
E:\vue-project\vue-element-admin-master>npm run build:prod vue-element-admin@3.8.1 build:prod E:\ ...
- pandas归一化操作
归一化操作有两种 1.max和min的归一化操作 min-max标准化(Min-Max Normalization) 返回结果0~1 公式: 实例: 如: 随机生成假数据如下 df = DataFra ...
- 【nginx】nginx.sh nginx 安装脚本
#! /bin/shcd /usr/local/srcwget http://nginx.org/download/nginx-1.10.1.tar.gzecho 'download success' ...
- Aizu - 1386 Starting a Scenic Railroad Service (思维乱搞)
给你n个区间,求: 1:最多有多少区间与同一个区间相交. 2:相交部分的最大区间数目. Sample Input 1 4 1 3 1 3 3 6 3 6 Sample Output 1 2 2 Sam ...
- hdu 6312
Problem Description Alice and Bob are playing a game.The game is played on a set of positive integer ...
- 安装lwqq
$ git clone https://github.com/xiehuc/pidgin-lwqq.git $ cd pidgin-lwqq/ $ sudo apt-get install cmake ...
- luogu1742 最小圆覆盖
狗题卡我精度--sol #include <algorithm> #include <iostream> #include <cstdlib> #include & ...
- JQuery中根据属性或属性值获得元素(6种情况获取方法)
根据属性获得元素 1.比如要获取页面p标签中属性有id的元素 $("p[id]").css("color","red"); 根据属性值获得元 ...
- 如何利用App打造自明星实现自盈利
1.了解各个概念 为了大家都能看懂这篇文章,先说明几个概念. App(Application):可以在移动设备上使用,满足人们咨询.购物.社交.娱乐.搜索等需求的一切应用程序. ...