Manasa and Combinatorics
题意:
给定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的更多相关文章
- HackerRank "Manasa and Prime game"
Intuitive one to learn about Grundy basic :) Now every pile becomes a game, so we need to use Spragu ...
- Codeforces 382E Ksenia and Combinatorics 【组合计数】*
Codeforces 382E Ksenia and Combinatorics Ksenia has her winter exams. Today she is learning combinat ...
- 【HackerRank】Manasa and Stones
Change language : Manasa 和 她的朋友出去徒步旅行.她发现一条小河里边顺序排列着带有数值的石头.她开始沿河而走,发现相邻两个石头上的数值增加 a 或者 b. 这条小河的尽头有一 ...
- 抄书 Richard P. Stanley Enumerative Combinatorics Chapter 2 Sieve Methods
2.1 Inclusion-Exclusion Roughly speaking, a "sieve method" in enumerative combinatorics is ...
- drawer principle in Combinatorics
Problem 1: Given an array of real number with length (n2 + 1) A: a1, a2, ... , an2+1. Prove that th ...
- [hackerrank]Manasa and Stones
https://www.hackerrank.com/contests/w2/challenges/manasa-and-stones 简单题. #include<iostream> us ...
- Manasa and Stones
from __future__ import print_function def main(): t = int(raw_input()) for _ in range(t): n = int(ra ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- 【转】Artificial Neurons and Single-Layer Neural Networks
原文:written by Sebastian Raschka on March 14, 2015 中文版译文:伯乐在线 - atmanic 翻译,toolate 校稿 This article of ...
随机推荐
- C#自定义类型数组排序
在数组或者集合中对自定义类型进行排序分为两种方法. 1.如果这个自定义类型是自己定义编写的,那么我可以使它继承ICompareable<T>接口,实现其中的CompareTo(Object ...
- Java依照List内存储的对象的某个字段进行排序
关键点:将List内存储的对象实现Comparable类.重写它的compareTo()方法就可以 Bean: package chc; public class StuVo implements C ...
- 【C语言】求两个数中不同的位的个数
//求两个数中不同的位的个数 #include <stdio.h> int count_different(int a, int b) { int count = 0; int c = a ...
- 【转载】FAT32文件系统详解
硬盘是用来存储数据的,为了使用和管理方便,这些数据以文件的形式存储在硬盘上.任何操作系统都有自己的文件管理系统,不同的文件系统又有各自不同的逻辑组织方式.例如:常见的文件系统有FAT,NTFS,EXT ...
- linux 块设备驱动 (三)块设备驱动开发
一: 块设备驱动注册与注销 块设备驱动中的第1个工作通常是注册它们自己到内核,完成这个任务的函数是 register_blkdev(),其原型为:int register_blkdev(unsigne ...
- sql中in/not in 和exists/not exists的使用方法差别
1:首先来说in/not in的使用方法 in/not in是确定单个属性的值是否和给定的值或子查询的值相匹配: select * from Student s where s.id in(1,2,3 ...
- 08 comet反向ajax
一:HTTP协议与技久链接+分块传输---->反向ajax 反向ajax又叫comet, server push,服务器推技术. 应用范围: 网页聊天服务器,, 新浪微博在线聊天,google ...
- iOS 优化方案浅析
本文转载至 http://mobile.51cto.com/iphone-413256.htm Windows独特的注册表机制以及复杂的进程.内存管理,给了很多PC“优化”类软件极大的机遇,比如奇虎3 ...
- lua 定义类 就是这么简单
在网上看到这样一段代码,真是误人子弟呀,具体就是: lua类的定义 代码如下: local clsNames = {} local __setmetatable = setmetatable loca ...
- Red Black Tree java.util.TreeSet
https://docs.oracle.com/javase/9/docs/api/java/util/SortedMap.html public interface SortedMap<K,V ...