题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3240

卡特兰数递推公式h(i)=h(i-1)*(4*i-2)/(i+1)

如果直接算每一步,然后modm的话,有错误,因为h(i-1)%m后,h(i-1)*(4*i-2)不一定能整除(i+1),所以不行。
其实只需要把答案看做两部分的乘积:一部分是与m互素的,这一部分的乘法直接计算,除法改成乘逆元就行了;另一部分是若干个m的素因子的乘积,因为m<1,000,000,000,所以m的不同素因子不会太多,用一个数组记录每一个素因子的数量就行。这一部分的乘法就是把记录的素因子数量相加,除法就是把记录的素因子数量相减。最后计算这两部分的乘积对m的取模,也就是h(n)%m。

代码如下:

#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;const int N=10001;
typedef long long LL;int su[N],num[N];
int n,m,sui;void mul(LL &res,int k)
{for(int i=0;i<sui;i++){while(k%su[i]==0){k/=su[i];num[i]++;}}res=(res*k)%m;
}int ext_gcd(int a,int b,int &x,int &y)
{int t,ret;if(!b){x=1;y=0;return a;}ret=ext_gcd(b,a%b,y,x);y-=x*(a/b);return ret;
}void chu(LL &res,int k)
{for(int i=0;i<sui;i++){while(k%su[i]==0&&num[i]>0){k/=su[i];num[i]--;}}if(k!=1){int x,y,temp;temp=ext_gcd(k,m,x,y);x=(x%m+m)%m;res=(res*x)%m;}
}int main()
{int i,j,k,t;while(scanf("%d%d",&n,&m)&&(n+m)){sui=0,t=m;for(i=2;i*i<=t;i++)if(t%i==0){su[sui++]=i;while(t%i==0)t/=i;}if(t>1)su[sui++]=t;memset(num,0,sizeof(num));LL res=1,sum=1,l;for(i=2;i<=n;i++){mul(res,4*i-2);chu(res,i+1);l=res;for(j=0;j<sui;j++)for(k=0;k<num[j];k++)l=l*su[j]%m;sum=(sum+l)%m;}printf("%lld\n",sum);}return 0;
}

转载于:https://www.cnblogs.com/dyllove98/archive/2013/06/12/3132818.html

HDU 3240 Counting Binary Trees 数论-卡特兰数相关推荐

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

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

  2. J. Counting Trees (树,卡特兰数)

    题目 https://codeforces.com/gym/102501/problem/J 大意就是,规定一棵二叉树,每个节点的值都大于等于其父节点的值.给出一个中序遍历值序列,问有多少棵这样的树满 ...

  3. HDU 5673 Robot 卡特兰数

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5673 题目描述: 一个人从原点开始向右走, 要求N秒后回到原点, 且过程中不能到负半轴, 人有两种操 ...

  4. 数论二(hdoj 卡特兰数)

    卡特兰数: 1 通项公式:h(n)=C(n,2n)/(n+1)=(2n)!/((n!)*(n+1)!) 2递推公式:h(n)=((4*n-2)/(n+1))*h(n-1); h(n)=h(0)*h(n ...

  5. Rosalind第88题:Counting Rooted Binary Trees

    Problem As in the case of unrooted trees, say that we have a fixed collection of  taxa labeling the  ...

  6. hdu 1023 大数 卡特兰数

    卡特兰数 JAVA大数 import java.util.*; import java.math.*; public class Main {public static void main(Strin ...

  7. 【HDU - 1134 】Game of Connections(JAVA大数加法,卡特兰数)

    题干: This is a small but ancient game. You are supposed to write down the numbers 1, 2, 3, ... , 2n - ...

  8. hdu 1134 卡特兰数(大数模板)

    卡特兰数 递推公式: C(n)=C(2n,n)/(n+1)  即用数组表示为c[i]=c[i-1]*(4*i-2)/(i+1); 一般形式 直接 表达 c[1]=1; for(i=2;i<40; ...

  9. 【数论】——Catalan 卡特兰数

    Catalan 卡特兰数 文章目录 Catalan 卡特兰数 定义 计算 证明 推论 代码 定义 给定 n 个 0 和 n 个 ** 1**,它们将按照某种顺序排成长度为 **2n ** 的序列,求它 ...

最新文章

  1. oracle10个,OracleDBA新手经常碰到的10个Oracle错误
  2. mysql中先随机提取再排序d_mysql性能优化
  3. PDF搜索、转换与处理类网站
  4. mac下php mysql数据库文件怎么打开_Mac环境下php操作mysql数据库的方法分享
  5. 多继承-概念、语法和基本演练
  6. CodeForces - 1494E A-Z Graph(构造+思维)
  7. QPainter函数setClipRect
  8. 我们为什么会爱上一个人?
  9. 基于SSLStrip的HTTPS会话劫持
  10. 清除n天以前的日志文件以及mysql-bin文件
  11. linux ubuntu文件系统,Ubuntu Linux 文件系统的主要目录
  12. 不为环境所动就能成功——职场人士寓言(3)
  13. DQN-FlappyBird项目学习
  14. becon帧 wifi_beacon帧
  15. 科目二倒车入库学车技巧_学车必看_保过。
  16. 【zer0pts CTF 2022】 Anti-Fermat(p、q生成不当)
  17. 戴尔笔记本电脑重装win10系统详细记录
  18. 计算机数字怎么转换成文本,怎么把数字转换成文本
  19. macOS更新系统到12.6后git无法使用
  20. python安装matplotlib库三种失败情况

热门文章

  1. Ubuntu 系统进不去 左上角减号
  2. 有的歌声音大有的歌声音小_手机听筒声音小怎么办?别急,只需一把刷子
  3. android 宽度动画,android – ObjectAnimator对LinearLayout宽度进行动画处理
  4. c语言程序设计 江宝钏 实验九,c语言程序设计,江宝钏著,实验九
  5. mysql导入表error 1067_mysql 导入数据error 1067(42000) Invalid default value for 'update'
  6. 引发了未经处理的异常:读取访问权限冲突_从零开始学Python:23课-文件读写和异常处理
  7. c语言两个条件同时成立,为什么if的条件成立else内的条件成立两个程序同时执行...
  8. FPGA学习笔记---利用连续赋值语句延时功能实现按键消抖
  9. CentOS7.5安装MySql8.0
  10. linux内存管理(十二)-直接页面回收