题意:

给定n,求问由2n个字母B,n个字母A构成的字符串中

任意前缀B的个数大于A的个数且任意后缀B的个数大于A的个数的 字符串个数。

解法:

注意到答案不易于直接计算,所以我们考虑应用容斥原理。

注意到本题非常类似卡特兰数。

卡特兰数等价于从棋盘上$(1,1)$走到$(n,n)$且不穿过对角线的方案数。

1.先考虑求存在前缀B的个数<A的个数的方案数。

等价于从棋盘上$(1,1)$上走到$(2n,n)$ 且 穿过从$(1,1)$开始,以$(1,1)$为方向向量的直线$L$ 的 方案数。

当第一次穿过$L$时,必然是向右走了t步,向上走了t+1步,将从$(t,t+1)$开始的折线以L未为称轴翻折,

得到一个在棋盘上从$(1,1)$到$(n-1,2n+1)$的路径,

这样对于任意一个穿过$L$的从$(1,1)$到$(2n,n)$的行走方案 对应 一个 从$(1,1)$到$(n-1,2n+1)$的行走方案。

注意到任意一个从$(1,1)$到$(n-1,2n+1)$的路径也必然对应着一个从$(1,1)$到$(2n,n)$的穿过L的方案。

这样证明了两者一一对应,个数相同为 $C_{3n}^{n-1}$。

2.对于存在后缀B的个数<A的个数的方案数,同1得个数为 $C_{3n}^{n-1}$。

3.对于同时满足1,2的方案数,考虑对于原问题做1的等价之后,

问题转化为求 从$(1,1)$到$(n-1,2n+1)$ 且 经过 过终点的与L平行的直线 的路径数。

类比1中的方法进行再次翻折得到其个数为 $C_{3n}^{n-2}$

综上:答案为$C_{3n}^n - 2*C_{3n}^{n-1} + C_{3n}^{n-2}$

应用Lucas定理,计算总效率$O(P + logn)$

 #include <iostream>
#include <cstdio>
#include <cstring> #define P 99991
#define LL long long using namespace std; LL fac[P]; LL qpow(LL x,int n)
{
LL ans=;
for(;n;n>>=,x=x*x%P)
if(n&) ans=ans*x%P;
return ans;
} LL C(int n,int m)
{
if(n<m) return ;
return fac[n]*qpow(fac[m],P-)%P*qpow(fac[n-m],P-)%P;
} LL Lucas(LL n,LL m)
{
if(m<) return ;
if(!m || !n) return 1LL;
return Lucas(n/P,m/P) * C(n%P,m%P)%P;
} int main()
{
fac[]=;
for(int i=;i<P;i++) fac[i]=fac[i-]*i%P;
LL n;
int T;
scanf("%d",&T);
while(T--)
{
cin>>n;
LL ans=Lucas(*n,n)-2LL*Lucas(*n,n-)+Lucas(*n,n-);
cout << (ans%P+P) %P << endl;
}
}

Manasa and Combinatorics的更多相关文章

  1. HackerRank "Manasa and Prime game"

    Intuitive one to learn about Grundy basic :) Now every pile becomes a game, so we need to use Spragu ...

  2. Codeforces 382E Ksenia and Combinatorics 【组合计数】*

    Codeforces 382E Ksenia and Combinatorics Ksenia has her winter exams. Today she is learning combinat ...

  3. 【HackerRank】Manasa and Stones

    Change language : Manasa 和 她的朋友出去徒步旅行.她发现一条小河里边顺序排列着带有数值的石头.她开始沿河而走,发现相邻两个石头上的数值增加 a 或者 b. 这条小河的尽头有一 ...

  4. 抄书 Richard P. Stanley Enumerative Combinatorics Chapter 2 Sieve Methods

    2.1 Inclusion-Exclusion Roughly speaking, a "sieve method" in enumerative combinatorics is ...

  5. drawer principle in Combinatorics

    Problem 1: Given an array of real number with length (n2 + 1) A: a1,  a2, ... , an2+1. Prove that th ...

  6. [hackerrank]Manasa and Stones

    https://www.hackerrank.com/contests/w2/challenges/manasa-and-stones 简单题. #include<iostream> us ...

  7. Manasa and Stones

    from __future__ import print_function def main(): t = int(raw_input()) for _ in range(t): n = int(ra ...

  8. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  9. 【转】Artificial Neurons and Single-Layer Neural Networks

    原文:written by Sebastian Raschka on March 14, 2015 中文版译文:伯乐在线 - atmanic 翻译,toolate 校稿 This article of ...

随机推荐

  1. grep man 有删减 百科

    NAME grep, egrep, fgrep, rgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FIL ...

  2. class中的私有属性的访问

    在类中的私有属性设置: class Name(): __init__(self): self.__name = 'arnol'` 如何查看: 1,在类中定义一个方法: def getname(self ...

  3. libraries_v140_x64_py35_1.0.1.tar.bz2 libraries_v120_x64_py27_1.1.0.tar 下载链接以及百度云下载链接

    下载链接 wget  -c  https://github.com/willyd/caffe-builder/releases/download/v1.0.1/libraries_v140_x64_p ...

  4. ffmpeg编码常见问题排查方法

    播放问题排查: 一旦我们遇到视频播放不了,第一件事,就是要找几个别的播放器也播放看看,做一下对比测试,或者对码流做一些基础分析,以便更好的定位问题的源头,而各个平台比较常见的播放/分析工具有如下几个: ...

  5. CentOS安装配置

    1.准备安装 1.1 系统简介 CentOS 是什么? CentOS是一个基于Red Hat 企业级 Linux 提供的可自由使用的源代码企业级的 Linux 发行版本.每个版本的 CentOS 都会 ...

  6. Hadoop集群_HDFS初探之旅

    1.HDFS简介 HDFS(Hadoop Distributed File System)是Hadoop项目的核心子项目,是分布式计算中数据存储管理的基础,是基于流数据模式访问和处理超大文件的需求而开 ...

  7. MFC学习之对话框---对话框之间的domodle 数据传递

    if(IDOK==data.DoModal()){tx->m_gao=data.m_he;tx->m_kuan=data.m_wi;tx->m_jiao=data.m_jiaoju; ...

  8. Android-基本控件和详解四种布局方式

    转自:https://www.cnblogs.com/ludashi/p/4883915.html 一.常用基本控件 1.TextView 看到Android中的TextView, 我不禁的想到了iO ...

  9. POJ 1195 Mobile phones (二维树状数组)

    Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...

  10. HDU 6074 Phone Call LCA + 并查集

    Phone Call Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pro ...