Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema.

However, one day, the No. 68 Cinema runs out of changes (they don't have 50-yuan notes currently), but Nephren still wants to start their business. (Assume that yuan is a kind of currency in Regulu Ere.)

There are three types of customers: some of them bring exactly a 50-yuan note; some of them bring a 100-yuan note and Nephren needs to give a 50-yuan note back to him/her; some of them bring VIP cards so that they don't need to pay for the ticket.

Now n customers are waiting outside in queue. Nephren wants to know how many possible queues are there that they are able to run smoothly (i.e. every customer can receive his/her change), and that the number of 50-yuan notes they have after selling tickets to all these customers is between l and r, inclusive. Two queues are considered different if there exists a customer whose type is different in two queues. As the number can be large, please output the answer modulo p.

Input

One line containing four integers n (1 ≤ n ≤ 105), p (1 ≤ p ≤ 2·109), l and r (0 ≤ l ≤ r ≤ n).

Output

One line indicating the answer modulo p.

Examples

Input
  1. 4 97 2 3
Output
  1. 13
Input
  1. 4 100 0 4
Output
  1. 35

Note

We use A, B and C to indicate customers with 50-yuan notes, customers with 100-yuan notes and customers with VIP cards respectively.

For the first sample, the different possible queues that there are 2 50-yuan notes left are AAAB, AABA, ABAA, AACC, ACAC, ACCA, CAAC, CACA and CCAA, and the different possible queues that there are 3 50-yuan notes left are AAAC, AACA, ACAA and CAAA. So there are 13 different queues satisfying the first sample. Similarly, there are 35 different queues satisfying the second sample.

题意:现在有N个人排队看电影,最开始收费处没有50元额钞票。买票的人可以付100元、50元或者刷卡,但是付100元的时候需要保证收费处能补50元。

问最后收费处还剩L到R张50元的方案数。

思路:如果不考虑刷卡,那就是一个卡特兰数,有刷卡的情况,我们枚举刷卡个数即可。所以我们现在假设不存在刷卡的情况:

首先我们需要知道卡特兰数的扩展--->从(0,0)出发到点(a,b),方案数为C(a+b,a);其中经过x=y分界线的方案数等效于从(-1,1)到(a,b)的方案数C(a+b,a-1);所以从(0,0)到(a,b),而且不经过x=y的方案数为C(a+b,a)-C(a+b,a-1);

我们注意到这里的a+b为定值,a和a-1相差1,而[L,R]又是连续区间,所以我们利用前缀和来解决这个问题,最终可以推出:

ans=(C(N,(N-L)/2)-C(N,(N-L)/2-1))  +   (C(N,(N-L)/2-1)-C(N,(N-L)/2-2))  +...+  (C(N,(N-R)/2)-C(N,(N-R-1)/2)) =C(N,(N-L)/2)-C(N,(N-R-1)/2);

然后需要解决的问题是除法问题,因为P不一定是质数,但是鉴于N比较小,我们可以手动把所有数拆为不含P因子的数和含P因子的数,然后前者做正常的乘法,除法。后者做加,减法,最后统一快速幂乘。

(也可以用找循环节的方法:把P拆分为多个素数幂的形式,最后用中国剩余定理合并,可以见CQZhangYu的代码,但是这里N比较小,没必要。

  1. #include<bits/stdc++.h>
  2. #define rep(i,a,b) for(int i=a;i<=b;i++)
  3. #define ll long long
  4. using namespace std;
  5. const int maxn=;
  6. int f[maxn],rev[maxn],P,N,p[],cnt,num[maxn][];
  7. int qpow(int a,int x){ int res=;while(x){if(x&)res=(ll)res*a%P;a=(ll)a*a%P;x>>=;}return res; }
  8. void solve()
  9. {
  10. int phi=P,tp=P; f[]=rev[]=;
  11. for(int i=;i<=tp/i;i++){
  12. if(tp%i==) {
  13. p[++cnt]=i;
  14. phi=phi/i*(i-);
  15. while(tp%i==) tp/=i;
  16. }
  17. }
  18. if(tp>) phi=phi/tp*(tp-),p[++cnt]=tp;
  19. rep(i,,N) {
  20. int x=i; rep(j,,cnt){
  21. num[i][j]=num[i-][j];
  22. while(x%p[j]==) num[i][j]++,x/=p[j];
  23. }
  24. f[i]=(ll)f[i-]*x%P;
  25. rev[i]=qpow(f[i],phi-);
  26. }
  27. }
  28. int C(int x,int y)
  29. {
  30. if(y<) return ; int res=;
  31. res=(ll)f[x]*rev[y]%P*rev[x-y]%P;
  32. rep(i,,cnt) res=(ll)res*qpow(p[i],num[x][i]-num[y][i]-num[x-y][i])%P;
  33. return res;
  34. }
  35. int main()
  36. {
  37. int L,R,ans=;
  38. scanf("%d%d%d%d",&N,&P,&L,&R);
  39. R=min(N,R); solve();
  40. rep(i,,N-L) ans=(ans+1LL*(C(N-i,(N-i-L)>>)-C(N-i,(N-i-R-)>>))*C(N,i))%P;
  41. printf("%d\n",ans);
  42. return ;
  43. }

CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&组合数学---比较综合的一道题)的更多相关文章

  1. 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT

    [CF896D]Nephren Runs a Cinema 题意:一个序列中有n格数,每个数可能是0,1,-1,如果一个序列的所有前缀和都>=0且总和$\in [L,R]$,那么我们称这个序列是 ...

  2. CF896D Nephren Runs a Cinema

    CF896D Nephren Runs a Cinema 题意 售票员最开始没有纸币,每次来一个顾客可以给她一张.拿走她一张或不操作.求出不出现中途没钱给的情况 \(n\) 名顾客后剩余钱数在 \(l ...

  3. Train Problem II(卡特兰数 组合数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1023 Train Problem II Time Limit: 2000/1000 MS (Java/ ...

  4. uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

    option=com_onlinejudge&Itemid=8&category=471&page=show_problem&problem=4224" st ...

  5. hdu5673 Robot 卡特兰数+组合数学+线性筛逆元

    Robot Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  6. BZOJ1856:[SCOI2010]字符串(卡特兰数,组合数学)

    Description lxhgww最近接到了一个生成字符串的任务,任务需要他把n个1和m个0组成字符串,但是任务还要求在组成的字符串中,在任意的前k个字符中,1的个数不能少于0的个数.现在lxhgw ...

  7. CodeForces - 1204E Natasha, Sasha and the Prefix Sums (组合数学,卡特兰数扩展)

    题意:求n个1,m个-1组成的所有序列中,最大前缀之和. 首先引出这样一个问题:使用n个左括号和m个右括号,组成的合法的括号匹配(每个右括号都有对应的左括号和它匹配)的数目是多少? 1.当n=m时,显 ...

  8. HDOJ/HDU 1133 Buy the Ticket(数论~卡特兰数~大数~)

    Problem Description The "Harry Potter and the Goblet of Fire" will be on show in the next ...

  9. 【高精度练习+卡特兰数】【Uva1133】Buy the Ticket

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

随机推荐

  1. Python基础(10)_内置函数、匿名函数、递归

    一.内置函数 1.数学运算类 abs:求数值的绝对值 divmod:返回两个数值的商和余数,可用于计算页面数 >>> divmod(5,2) (2, 1) max:返回可迭代对象中的 ...

  2. LeetCode:杨辉三角【118】

    LeetCode:杨辉三角[118] 题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: ...

  3. Python 1 数据类型的操作

    一.数字(Number) 1.数学函数: 函数 返回值 ( 描述 ) abs(x) 返回数字的绝对值,如abs(-10) 返回 10 ceil(x) 返回数字的上入整数,如math.ceil(4.1) ...

  4. formatblock 块及

    有标签,执行标签替换,只是替换标签,属性不改变. 在无标签外部添加标签

  5. Bootstrap主题库

    主题 https://startbootstrap.com/template-categories/all/ https://bootstrapmade.com/ http://www.jqueryf ...

  6. [原创]spring及springmvc精简版--继承数据源,声明式事物

    1.前期:导入c3p0 jar包,相关数据库连接jar包,我用的是mysql 2.关注事物管理器的配置和AOP配置 代码: 核心关注bean配置文件 application.xml <?xml ...

  7. JDK源码 - ArrayList (基于1.7)

    前言   推荐一位大牛的博客: https://blog.csdn.net/eson_15/article/details/51121833 我基本都是看的他的源码分析,刚开始如果直接看jdk源码可能 ...

  8. Golang 连接Kafka

    Kafka介绍 Kafka是Apache软件基金会开发的一个开源流处理平台,由Java和Scala编写:Kafka是一种高吞吐.分布式.基于订阅发布的消息系统. Kafka名称解释 Producer: ...

  9. 简单web作业---书籍介绍的相关网页编写

    老师布置的web作业,我做了3个页面,其中有利用老师的css代码! 我有添加背景音乐,下面的是主界面的代码. <!DOCTYPE html> <html> <head&g ...

  10. 安装mysql5.7后无法启动,/var/run/mysqld 目录每次重启后都需要手动去创建--终极解决方案

    鉴于很多童鞋反应,mysql5.7安装后出现无法启动,建立/var/run/mysqld 并赋权mysql用户解决了启动的问题,但是重启系统后又出现无法启动的问题,导致/var/run/mysqld ...