Description

一个药瓶里装有n颗药丸,每天吃半颗,需要在2n天吃完。每天吃药时,从药瓶中取药,如果取到的药是整颗的,就把它分成两半,吃掉其中的一半,另一半重新放入瓶中;如果取到的是半颗药,则直接吃掉。问共有多少种吃药方法?

例如:

n=1,则吃药方法有1种(取一颗,取半颗)。

n=2,则有2种吃药方法(取一颗,取半颗,取一颗,取半颗;取一颗,取一颗,取半颗,取半颗)。

Input

有多组测试数据,每组测试数据占一行,每行包含一个整数n(<=100),表示药丸数量。

Output

每组测试数据输出占一行,每行包含一个整数,表示吃药方法的种数。

Sample Input 

1
2
3

Sample Output

1
2
5

先把模型抽象出来:用1表示吃一颗,0表示吃半颗,明显对于一个01序列,任何的0之前的1的个数都要不大1的个数;

换句话说对于任何前缀,1的个数总不少于0的个数;

那么这个就是一个卡特兰数的经典模型;其实oeis一下就知道了;

下面是一篇关于卡特兰数比较详细的讲解:卡特兰数详解;

另外要用到大数模板:高精度模板

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
using namespace std;struct BigInteger {typedef unsigned long long LL;static const int BASE = 100000000;static const int WIDTH = 8;vector<int> s;BigInteger& clean(){while(!s.back()&&s.size()>1)s.pop_back(); return *this;}BigInteger(LL num = 0) {*this = num;}BigInteger(string s) {*this = s;}BigInteger& operator = (long long num) {s.clear();do {s.push_back(num % BASE);num /= BASE;} while (num > 0);return *this;}BigInteger& operator = (const string& str) {s.clear();int x, len = (str.length() - 1) / WIDTH + 1;for (int i = 0; i < len; i++) {int end = str.length() - i*WIDTH;int start = max(0, end - WIDTH);sscanf(str.substr(start,end-start).c_str(), "%d", &x);s.push_back(x);}return (*this).clean();}BigInteger operator + (const BigInteger& b) const {BigInteger c; c.s.clear();for (int i = 0, g = 0; ; i++) {if (g == 0 && i >= s.size() && i >= b.s.size()) break;int x = g;if (i < s.size()) x += s[i];if (i < b.s.size()) x += b.s[i];c.s.push_back(x % BASE);g = x / BASE;}return c;}BigInteger operator - (const BigInteger& b) const {assert(b <= *this); // 减数不能大于被减数BigInteger c; c.s.clear();for (int i = 0, g = 0; ; i++) {if (g == 0 && i >= s.size() && i >= b.s.size()) break;int x = s[i] + g;if (i < b.s.size()) x -= b.s[i];if (x < 0) {g = -1; x += BASE;} else g = 0;c.s.push_back(x);}return c.clean();}BigInteger operator * (const BigInteger& b) const {int i, j; LL g;vector<LL> v(s.size()+b.s.size(), 0);BigInteger c; c.s.clear();for(i=0;i<s.size();i++) for(j=0;j<b.s.size();j++) v[i+j]+=LL(s[i])*b.s[j];for (i = 0, g = 0; ; i++) {if (g ==0 && i >= v.size()) break;LL x = v[i] + g;c.s.push_back(x % BASE);g = x / BASE;}return c.clean();}BigInteger operator / (const BigInteger& b) const {assert(b > 0);  // 除数必须大于0BigInteger c = *this;       // 商:主要是让c.s和(*this).s的vector一样大BigInteger m;               // 余数:初始化为0for (int i = s.size()-1; i >= 0; i--) {m = m*BASE + s[i];c.s[i] = bsearch(b, m);m -= b*c.s[i];}return c.clean();}BigInteger operator % (const BigInteger& b) const { //方法与除法相同BigInteger c = *this;BigInteger m;for (int i = s.size()-1; i >= 0; i--) {m = m*BASE + s[i];c.s[i] = bsearch(b, m);m -= b*c.s[i];}return m;}int bsearch(const BigInteger& b, const BigInteger& m) const{int L = 0, R = BASE-1, x;while (1) {x = (L+R)>>1;if (b*x<=m) {if (b*(x+1)>m) return x; else L = x;}else R = x;}}BigInteger& operator += (const BigInteger& b) {*this = *this + b; return *this;}BigInteger& operator -= (const BigInteger& b) {*this = *this - b; return *this;}BigInteger& operator *= (const BigInteger& b) {*this = *this * b; return *this;}BigInteger& operator /= (const BigInteger& b) {*this = *this / b; return *this;}BigInteger& operator %= (const BigInteger& b) {*this = *this % b; return *this;}bool operator < (const BigInteger& b) const {if (s.size() != b.s.size()) return s.size() < b.s.size();for (int i = s.size()-1; i >= 0; i--)if (s[i] != b.s[i]) return s[i] < b.s[i];return false;}bool operator >(const BigInteger& b) const{return b < *this;}bool operator<=(const BigInteger& b) const{return !(b < *this);}bool operator>=(const BigInteger& b) const{return !(*this < b);}bool operator!=(const BigInteger& b) const{return b < *this || *this < b;}bool operator==(const BigInteger& b) const{return !(b < *this) && !(b > *this);}
};ostream& operator << (ostream& out, const BigInteger& x) {out << x.s.back();for (int i = x.s.size()-2; i >= 0; i--) {char buf[20];sprintf(buf, "%08d", x.s[i]);for (int j = 0; j < strlen(buf); j++) out << buf[j];}return out;
}istream& operator >> (istream& in, BigInteger& x) {string s;if (!(in >> s)) return in;x = s;return in;
}
int main()
{int n;BigInteger ans[350];ans[1]=1;ans[2]=2;for(int i=3;i<=200;++i)ans[i]=ans[i-1]*i;while(scanf("%d",&n)!=EOF){BigInteger p;p=ans[2*n]/(ans[n]*ans[n+1]);cout<<p<<endl;}
}

wustoj 1506 药丸 卡特兰数相关推荐

  1. Catalan数——卡特兰数

    今天阿里淘宝笔试中碰到两道组合数学题,感觉非常亲切,但是笔试中失踪推导不出来 后来查了下,原来是Catalan数.悲剧啊,现在整理一下 Catalan数--卡特兰数] 一.Catalan数的定义令h( ...

  2. 【COGS】2287:[HZOI 2015]疯狂的机器人 FFT+卡特兰数+排列组合

    [题意][COGS 2287][HZOI 2015]疯狂的机器人 [算法]FFT+卡特兰数+排列组合 [题解]先考虑一维的情况,支持+1和-1,前缀和不能为负数,就是卡特兰数的形式. 设C(n)表示第 ...

  3. 关于卡特兰数及典型例题

    关于卡特兰数: f[0] = 1, f[1] = 1; for(int i = 2; i <= n; i++)for(int j = 0; j < i; j++)f[i] += f[j] ...

  4. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  5. 先序序列为a、b、c、d的不同二叉树的个数是多少(卡特兰数)

    除了逻辑清晰的挨个画出来之外,还有一种方法需要大家牢记! 因为前序序列和中序序列可以唯一地确定一棵二叉树,并且题目已经给出了先序序列,所以我们只需要知道由该先序序列可以确定多少个中序序列即可,确定多少 ...

  6. zcmu-1934(卡特兰数大数取模(逆元))

    1934: ly的二叉树 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 42  Solved: 9 [Submit][Status][Web Boar ...

  7. 洛谷 P1044 栈 [卡特兰数]

    题目背景 栈是计算机中经典的数据结构,简单的说,栈就是限制在一端进行插入删除操作的线性表. 栈有两种最重要的操作,即 poppop (从栈顶弹出一个元素)和 pushpush (将一个元素进栈). 栈 ...

  8. BZOJ4001[TJOI2015]概率论——卡特兰数

    题目描述 输入 输入一个正整数N,代表有根树的结点数 输出 输出这棵树期望的叶子节点数.要求误差小于1e-9 样例输入 1 样例输出 1.000000000 提示 1<=N<=10^9 设 ...

  9. Catalan Numbers 卡特兰数

    卡特兰数源于组合数学,递推式为 H[1] = 1:H[n] = H[n-1]*(4*n-2)/(n+1){n>=2}: 卡塔兰数的渐近增长为 下面给出几个求卡特兰数的公式,用h(n)表示卡特兰数 ...

最新文章

  1. Windows 11 再惹“众怒”!网友:微软就是逼我去买新电脑!
  2. 黄埔大学,选址定了!
  3. ATSS EfficientDet
  4. C++ 指针 vs 数组
  5. java五子棋悔棋代码_小猿圈前端编写JS五子棋游戏
  6. CentOS7 安装redis-5.0.5/注册为系统服务及单机启动多个redis服务
  7. 《Hadoop权威指南》第三章 Hadoop分布式文件系统
  8. SQL Server 2008 高可用性视频(四)-- 故障转移群集
  9. python中import sys_python import sys出错怎么办
  10. python代码性能分析_Python 性能分析入门指南
  11. 2021年KOL市场研究报告
  12. PL/pgSQL学习笔记之六
  13. 如何保证redis数据都是热点数据
  14. linux 读取 ntfs硬盘,嵌入式linux下ntfs格式的硬盘读写方法
  15. PAT (Basic Level) Practice 1006 换个格式输出整数
  16. 如何实现大批量UKEY远程连接管控?
  17. Navicat安装与破解
  18. 20+案例教你可视化图表的设计方法
  19. Oriented R-CNN完整复现HRSC2016以及训练自己的HBB数据集(DIOR)
  20. divmod在python中是什么意思_python中divmod是什么

热门文章

  1. Led智慧照明系统功能
  2. python起笔落笔_书法讲究的是起笔和落笔落是什么意思
  3. java实现注册登录版五子棋对战平台(超详细注释,内含人机实现)
  4. java application_运行java application时,总是报错
  5. 项目经理辞职后,可以干嘛?
  6. jupyther_python基础系列 09 第九章 有益的探索
  7. 102-并发编程详解(中篇)
  8. Nacos 极简入门
  9. 阿里巴巴矢量图标库(网页)
  10. kong mysql_konga 安装