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 ...
随机推荐
- javaee加密部署,tomcat使用自己的classloader解密【正解】
[起因] 公司需要对一个web项目进行加密之后出售, 大家都知道的,class很好反编译, 所以需要对class文件先进行加密, 然后使用自己的classloader进行解密并加载. [步骤] 大概分 ...
- JDK 环境变量配置(Mac)
Mac JDK 安装过后 修改 ~/. bash_profile 配置环境变量 修改内容: JAVA_HOME=$(/usr/libexec/java_home) export JAVA_HOME P ...
- Delphi下用API代码创建Form
program PMyWindowClass; uses Windows, Messages, SysUtils; type TMyWindow = class(TObject) priva ...
- 性能测试之LoardRunner 手动关联二
概述: 1.如果寻找左右边界值 2.关联函数详解 以下是详细介绍 1.如果寻找左右边界值 <以login 为例> Step1.录制两份相同的业务流程的的脚本,输入的数据要相同 Step2. ...
- SQL SERVER之数据查询
本篇主要解说查询语句,全部的演示样例都会依照以下这张表进行. stuID stuName age sex 11090241031 王小虎 21 男 11090241032 王小六 22 男 11 ...
- Effective C++_笔记_条款07_为多态基类声明virtual析构函数
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 这个规则只适用于polymorphic(带多态性质的)base ...
- JSP的学习(1)——基本知识与底层原理
通过之前的学习,我们已经对Servlet有所了解,现在我们先来学习JSP,当能使用JSP进行友好的页面显示之后,再回去学习Servlet的其他高级特性会将整个学习很好的融入进来. JSP,即Java ...
- Tomcat详细用法学习(二)
本篇接上一篇<Tomcat详细用法学习(一)>,主要讲解服务器的虚拟目录映射的几种方式. 先来看几个概念: web应用的概念:一个web应用包含了许多我们做好的web资源,里面或许包括了多 ...
- 重载(overload),覆盖/重写(override),隐藏(hide)
写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖/重写(override),隐藏(hide).在早期的C++书籍中,常常把重载(overload)和覆盖(override)搞错 ...
- mysql dump 参数
mysql dump 参数: -R, --routines Dump stored routines (functions and procedures). 备份 函数和存储过程: -E, --eve ...