题意:

给定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. WWDC2014苹果的“软件”发布会

    WWDC 2014 苹果的"软件"发布会 在今年的 6 月 2 日到 6 日,苹果照例举行了一年一次的全球开发者大会(World Wide Developer Conference ...

  2. Android组件系列----ContentProvider内容提供者【4】

    (4)单元測试类: 这里须要涉及到另外一个知识:ContentResolver内容訪问者. 要想訪问ContentProvider.则必须使用ContentResolver. 能够通过ContentR ...

  3. Android与WebView的插件管理机制

    上一篇文章说到,当利用WebViewClient或者WebChromeClient来处理由html页面传过来的请求的时候,都会将相应的服务名称,操作方法和相应的參数数据传给一个叫PluginManag ...

  4. HDU 6076 Security Check DP递推优化

    Security Check Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) ...

  5. poi 处理excel 小数问题 整数不保留小数位 整数多.0

    读取的单元格为 hssfCell ,传入下面我提供的方法处理默认poi返回的为DOUBLE,所有先转为Long判断下,再进行返回: private String getValue(Cell hssfC ...

  6. Dubbo Spring Cloud Motan

    跨语言统一治理.Golang,谈谈另辟蹊径的开源RPC框架Motan_搜狐科技_搜狐网 https://www.sohu.com/a/207389967_467759

  7. 简单老式Java对象 横切关注点 最小侵入性编程 声明式编程 避免强迫类继承和接口实现

    Spring In Action data injection aspect-oriented programming Plain Old Java Object 依赖注入能让相互协作的软件组件保持松 ...

  8. UVA 11752 The Super Powers —— 数学与幂

    题目链接:https://vjudge.net/problem/UVA-11752 题解: 1.首先变量必须用unsig long long定义. 2.可以分析得到,当指数为合数的时候,该值合法. 3 ...

  9. 科目三靠边停车难度升级,超过50cm不合格怎么破?

    驾考新规实施了几天,考过的学员普遍反映科目三难度升级,其中靠边停车项目的扣分点细化之后,一不小心就会不合格.新规以前靠边停车不压线就好了,新规之后,车辆距离马路右侧边缘线超过50cm就要扣100分,对 ...

  10. discuz邮箱注册激活||腾讯企业邮箱免费注册及登录方法

    如何申请免费的企业邮箱,如果拥有了网站,还能有一个免费的域名邮箱,是不是很拉风呢?对于还没有注册企业的用户来说,优先使用企业邮箱,是非常好的事呢. 腾讯邮箱现在开放免费的企业邮箱注册,效果要比个人邮箱 ...