hdu3240 Counting Binary Trees
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
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.
9) on a single line. The input ends with n = m = 0.
4 10
0 0
2
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
__int64 vec[40],num[40],m,index; __int64 ectgcd(__int64 a,__int64 b,__int64 & x,__int64 & y)
{
if(b==0){x=1;y=0;return a;}
__int64 d=ectgcd(b,a%b,x,y);
__int64 t=x;x=y;y=(t-a/b*y);
return d;
}
int main()
{
__int64 i,j,tempm,t,k,l;
__int64 n;
while(scanf("%I64d%I64d",&n,&m)!=EOF&&n+m)
{
memset(num,0,sizeof(num));
index=0;
tempm=m;
for(i=2;i*i<=m;i++)
{
if(m%i==0)
{
vec[index++]=i;
while(m%i==0)
{
m=m/i;
}
}
}
if(m!=1)
vec[index++]=m;
m=tempm;
__int64 res=1,result=0;
for(i=1;i<=n;i++)
{
k=4*i-2;
for(j=0;j<index;j++)
{
if(k%vec[j]==0)
{
while(k%vec[j]==0)
{
k=k/vec[j];
num[j]++;
}
}
}
res=res*k%m;
k=i+1;
for(j=0;j<index;j++)
{
if(k%vec[j]==0)
{
while(k%vec[j]==0)
{
k=k/vec[j];
num[j]--;
}
}
}
if(k!=1)
{
__int64 x,y;
ectgcd(k,m,x,y);
x=x%m;
if(x<0)
x+=m;
res=res*x%m;
}
l=res;
for(j=0;j<index;j++)
for(t=0;t<num[j];t++)
l=l*vec[j]%m;
result=(result+l)%m;
}
printf("%I64d\n",result);
}
return 0;
}
hdu3240 Counting Binary Trees的更多相关文章
- [HDU3240]Counting Binary Trees(不互质同余除法)
Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- Binary Trees
1. Definiation What is Binary Trees? Collection of node (n>=0) and in which no node can have more ...
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- 如何关闭IE浏览器在生成原型时候的安全警告
在上一节中,我们学习了如何生成网页原型的三种方法,当时我们采用的默认浏览器,搜狗浏览器,没有弹出安全警告,一般情况下,如果你的浏览器是IE的话,在每次生成网页原型的时候都会弹出如下安全警告,如图: 暂 ...
- 基于visual Studio2013解决C语言竞赛题之1074八皇后
题目 解决代码及点评 /************************************************************************/ /* ...
- ubuntu 10.04安装qtcreator并汉化
最近最的项目中需要做出来一个带有界面的demo,所以想到了用qt做个简单的demo! 于是在ubuntu上安装了qt,很简单apt-get apt-get install qtcreator 大概几百 ...
- ASP.NET - TreeView
设置节点图片 : Windows资源管理器左侧的树型资源结构图中,各节点都有图片连接,例如磁盘的图片.光盘的图片和文件夹的图片等,使资源的表现更加形象.IEWebControls的TreeView控件 ...
- [置顶] 手把手教你iOS消息推送证书生成以及Push消息
iOS推送消息是许多iOS应用都具备的功能,今天在给应用加推送功能,在生成证书的过程中,发生了各种令人蛋痛的事.下面就把步骤拿出来分享下: iOS消息推送的工作机制可以简单的用下图来概括: Provi ...
- js验证日期
寻寻觅觅,Web开发里,对日期的验证太多了,网上好多是用正则表达式来验证,但是这种验证也只能验证格式,没办法验证有效性,比如平年(2月28天)和闰年(2月29天).平时用得多,以前经常用一次写一次,腻 ...
- 移动无边框窗体(设置标志位更流畅,或者发送WM_SYSCOMMAND和SC_MOVE + HTCAPTION消息)
移动无边框窗体的代码网上很多,其原理都是一样的,但是是有问题的,我这里只是对其修正一下 网上的代码仅仅实现了两个事件 void EditDialog::mousePressEvent(QMouseEv ...
- HDU 4981 Goffi and Median(水)
HDU 4981 Goffi and Median 思路:排序就能够得到中间数.然后总和和中间数*n比較一下就可以 代码: #include <cstdio> #include <c ...
- Swift - 告警提示框(UIAlertController)的用法
自iOS8起,苹果就建议告警框使用UIAlertController来代替UIAlertView.下面总结了一些常见的用法: 1,简单的应用(同时按钮响应Handler使用闭包函数) 1 2 3 ...
- 如何获取ul 中li选中的值点击button按钮跳转链接
<ul id="parent"> <li></li> <li></li> <li></li> & ...