Counting Binary Trees

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 493 Accepted Submission(s): 151

Problem Description
There are 5 distinct binary trees of 3 nodes:

Let T(n) be the number of distinct non-empty binary trees of no more than
n nodes, your task is to calculate T(n) mod
m.

 
Input
The input contains at most 10 test cases. Each case contains two integers n and m (1 <= n <= 100,000, 1 <= m <= 10
9) on a single line. The input ends with n = m = 0.
 
Output
For each test case, print T(n) mod m.
 
Sample Input
3 100
4 10
0 0
 
Sample Output
8
2
 
Source
 
Recommend
zhonglihua
乘法逆元,我们知道,卡特兰数可以由公式,h[i]=h[i-1]*(4*i-2)/(i+1)得出,但是,我们知道,由于,是取过模的,我们如果,不还是直接除的话,是不对的,所以,我们要用乘法逆元就可以了,但是,乘法逆元,要求是互质的数, 这里,我们,把m的质因子保存下来,互素的直接算就可以了 !
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. using namespace std;
  5. __int64 vec[40],num[40],m,index;
  6.  
  7. __int64 ectgcd(__int64 a,__int64 b,__int64 & x,__int64 & y)
  8. {
  9. if(b==0){x=1;y=0;return a;}
  10. __int64 d=ectgcd(b,a%b,x,y);
  11. __int64 t=x;x=y;y=(t-a/b*y);
  12. return d;
  13. }
  14. int main()
  15. {
  16. __int64 i,j,tempm,t,k,l;
  17. __int64 n;
  18. while(scanf("%I64d%I64d",&n,&m)!=EOF&&n+m)
  19. {
  20. memset(num,0,sizeof(num));
  21. index=0;
  22. tempm=m;
  23. for(i=2;i*i<=m;i++)
  24. {
  25. if(m%i==0)
  26. {
  27. vec[index++]=i;
  28. while(m%i==0)
  29. {
  30. m=m/i;
  31. }
  32. }
  33. }
  34. if(m!=1)
  35. vec[index++]=m;
  36. m=tempm;
  37. __int64 res=1,result=0;
  38. for(i=1;i<=n;i++)
  39. {
  40. k=4*i-2;
  41. for(j=0;j<index;j++)
  42. {
  43. if(k%vec[j]==0)
  44. {
  45. while(k%vec[j]==0)
  46. {
  47. k=k/vec[j];
  48. num[j]++;
  49. }
  50. }
  51. }
  52. res=res*k%m;
  53. k=i+1;
  54. for(j=0;j<index;j++)
  55. {
  56. if(k%vec[j]==0)
  57. {
  58. while(k%vec[j]==0)
  59. {
  60. k=k/vec[j];
  61. num[j]--;
  62. }
  63. }
  64. }
  65. if(k!=1)
  66. {
  67. __int64 x,y;
  68. ectgcd(k,m,x,y);
  69. x=x%m;
  70. if(x<0)
  71. x+=m;
  72. res=res*x%m;
  73. }
  74. l=res;
  75. for(j=0;j<index;j++)
  76. for(t=0;t<num[j];t++)
  77. l=l*vec[j]%m;
  78. result=(result+l)%m;
  79. }
  80. printf("%I64d\n",result);
  81. }
  82. return 0;
  83. }

hdu3240 Counting Binary Trees的更多相关文章

  1. [HDU3240]Counting Binary Trees(不互质同余除法)

    Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. Binary Trees

    1. Definiation What is Binary Trees? Collection of node (n>=0) and in which no node can have more ...

  3. [leetcode-617-Merge Two Binary Trees]

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  4. Merge Two Binary Trees

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  5. [LeetCode] Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  6. [Swift]LeetCode617. 合并二叉树 | Merge Two Binary Trees

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  7. [Swift]LeetCode823. 带因子的二叉树 | Binary Trees With Factors

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...

  8. [Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees

    A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...

  9. [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees

    For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...

随机推荐

  1. easyhadoop:failed to open stream:Permission denied in /var/www/html/index.php

    今天又重新部署了下easyhadoop,结果apache后台服务器报这个错误: [Fri Dec 13 10:32:41 2013] [notice] SIGHUP received. Attempt ...

  2. Windows通过远程桌面访问Ubuntu

    关于Windows通过远程桌面访问Ubuntu 问题及目标 Window环境通过远程桌面访问Ubuntu Ubuntu机器端   1.  安装所需软件包   sudoapt-get install x ...

  3. 三篇IMO的文章

    http://column.iresearch.cn/b/201411/687499.shtml?from=singlemessage&isappinstalled=1 http://colu ...

  4. GDI+: Curved Shapes

    原文 http://www.functionx.com/vcsharp2003/gdi/curves.htm Curves   Introduction to Curves   A curve is ...

  5. 浅析嵌入式Linux系统的构成和启动过程

    在我们的周围,大量的嵌入式设备都是基于Linux系统来构建的,嵌入式Linux与主机Linux相比有着自己的一些特点,本文就嵌入式Linux系统的构成和启动过程做一些总结. 一.嵌入式Linux系统构 ...

  6. UVA11552------FEWEST FLOPS------区间型的DP

    题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. Android程序检测网络是否可用

    在做Android应用程序中,连接网络的时候,常常要用到检测网络状态是否可用,在这里分享一个比较好用的方法. 本人参考:http://blog.csdn.net/sunboy_2050/article ...

  8. Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue

    1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...

  9. winform基础——实现简易赈灾物资发放登记系统

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  10. 查看 ios 真机调试log,导出log

    使用Xcode 在模拟器李敏运行的时候,可以直接通过xcode 查看log,但是真机测试的时候,xcode 却无法获取到,对于日志输出,可以先保存到真机上,之后通过iTunes 导出即可 修改源码 此 ...