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)\).这样就变成了以 ...
随机推荐
- PMD 编译 语法分析 词法分析 抽象语法树
编译原理 163 课堂 http://mooc.study.163.com/learn/-1000002001?tid=1000003000#/learn/content?type=detail&am ...
- centos6启动故障排除
centos6中boot文件被全部删除的故障排除 /boot文件里关于启动的核心文件有三个,/vmlinuz-2.6.32-696.e16.x86_64,initramfs-2.6.32-696.el ...
- destoon 自定义session丢失
destoon 在使用session之前 应该实例化 $session = new dsession(); destoon通过配置文件加载了不同session存储方式.如果你使用session的页面 ...
- Hogan的安装和使用
Hogan的安装和使用 通过npm安装hogan: npm install hogan.js --save-dev CommonJs下的使用方式: // 引入hogan var hogan = req ...
- Python9-网络编程2-day31
基于UDP协议的socket #server import socket sk = socket.socket(type=socket.SOCK_DGRAM) sk.bind(('127.0.0.1' ...
- QT入门学习笔记2:QT例程
转至:http://blog.51cto.com/9291927/2138876 Qt开发学习教程 一.Qt开发基础学习教程 本部分博客主要根据狄泰学院唐老师的<QT实验分析教程>创作,同 ...
- SQL前后端分页
/class Page<T> package com.neusoft.bean; import java.util.List; public class Page<T> { p ...
- JS中如何操作数组
背景:随笔中所应用到的代码来自于上一篇随笔,MVC&JQuery如何根据List动态生成表格,部分代码不再重复. 代码如下: $("#btnTan").click(func ...
- hdu4489 组合公式+dp
这里对于题意在说明一下, 题目中要求的排列必须是波浪形,每一个在排列中的人不是波峰就是波谷,如果它既不是波峰也不是波谷排列就是错的. 对于我这种数学渣渣来说,做一道dp题要好久,%>_<% ...
- NAT(地址转换技术)详解(转载)
作者:逃离地球的小小呆 来源:CSDN 原文:https://blog.csdn.net/gui951753/article/details/79593307版权声明:本文为博主原创文章,转载请附上博 ...