题意:

给定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. VueJS样式绑定:v-bind

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  2. Java反射及其在Android中的应用学习总结

    一. Java反射机制 Reflection 是Java被视为动态(或准动态)语言的一个关键性质.这个机制同意程序在执行时透过Reflection APIs取得不论什么一个已知名称的class的内部信 ...

  3. 在eclipse中查找指定文件 [多种方法]

    在eclipse中查找指定文件   1.ctrl+h打开搜索界面 File Search: containing text填*,File name patterns填写hello.*,可以找到hell ...

  4. SEO(搜索引擎优化)

    高级搜索指令 搜索命令中的字符均为英文 用户除了可以通过搜索引擎搜索普通的查询词外,还可以使用一些特殊的高级搜索指令.这些指令普通用户很少会用到,但是对于SEO人员进行竞争对手研究和寻找外部链接资源却 ...

  5. PE添加Style

       1. <style id="NumberStyle"> <setting> <param name="option"> ...

  6. ecshop忘记管理员密码

    直接修改数据表 ecs_admin_user, 找到对应的管理员, 同时修改 password 为 2fc3ec4c91d51bee94f4a8ccbdbe5383 和 ec_salt 为1819, ...

  7. 【BZOJ4293】[PA2015]Siano 线段树

    [BZOJ4293][PA2015]Siano Description 农夫Byteasar买了一片n亩的土地,他要在这上面种草. 他在每一亩土地上都种植了一种独一无二的草,其中,第i亩土地的草每天会 ...

  8. EasyDarwin云存储方案调研:海康萤石云采用的是MPEG-PS打包的方式进行的存储

    EasyDarwin开源流媒体服务器项目在直播功能稳定和完善之后,开始涉及服务器端存储与回放功能的调研与开发,当然,这里就要研究一下行业标杆萤石云是怎么来做的,我们通过非常复杂的流程将萤石存储的录像文 ...

  9. java的取出map里所有元素的两种方式

    /* * 取出map元素的两种方式 */package com.map.test; import java.util.HashMap;import java.util.Iterator;import ...

  10. 关于<context:annotation-config/>配置

    对于spring项目的一些配置,一直感到有些混乱,今天看到一前辈总结的特别好,把自己的理解贴在这里,有不当的地方,后续继续学习: 当我们使用@Autowired.@Required等这些注解时,就要在 ...