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 ...
随机推荐
- BMP文件结构
1. 位图文件头 位图文件头包含有关于文件类型.文件大小.存放位置等信息,在Windows 3.0以上版本的位图文件中用BITMAPFILEHEADER结构来定义: typedef struct ta ...
- JavaScript 进阶(二)变量作用域
局部变量陷阱 先看一段代码: function foo() { var a = "hello" b = "world" return a + b; } 这个函数 ...
- C和指针 (pointers on C)——第十章:结构体和联合(上)
第十章 结构和联合 这个部分先介绍了.运算符,能够供直接訪问,还介绍了->运算符,它取代结构体指针的间接訪问操作(*struct).xxx 这一章新手理解起来不算太难,没有学过操作系统的话理解位 ...
- Android 高仿 频道管理----网易、今日头条、腾讯视频 (可以拖动的GridView)附源码DEMO
距离上次发布(android高仿系列)今日头条 --新闻阅读器 (二) 相关的内容已经半个月了,最近利用空闲时间,把今日头条客户端完善了下.完善的功能一个一个全部实现后,就放整个源码.开发的进度就是按 ...
- Linear Regression(线性回归)(三)—代价函数J(θ)选择的概率解释
(整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 在遇到线性回归问题时,我们总是令.可是我们为什么这样选择代价函数呢 ...
- 【Java数据结构】Java数据结构之链表反转
我们都知道用C可以很简单的实现单链表反转,今天来学习下,在Java中如何实现链表反转. 思路很简单,定义一个类,这个类分成2块,一块是表示自身的标志,另外一个存储指向下一个元素的引用.通过互换相邻两个 ...
- uva 140
思路:暴力+剪枝 uva140 wa了好多次……数组开小了……!!! #include <iostream> #include <cstdio> #include <cm ...
- 今天就这么乱糟糟的过啦~刚刚接触html,就稍微写了一下,明天加油,今天直接贴图
2015-01-01 <!DOCTYPE HTML> <html> <head> <meta charset = "gb2312"> ...
- 快速排序的时间复杂度nlogn是如何推导的??
本文以快速排序为例,推导了快排的时间复杂度nlogn是如何得来的,其它算法与其类似. 对数据Data = { x1, x2... xn }: T(n)是QuickSort(n)消耗的时间: P(n)是 ...
- Swift - 文本输入框内容改变时响应,并获取最新内容
1,问题描述 有时我们开发的时候需要先把“确认”按钮初始设置为不可用,当文本框中输入文字以后,再将输入按钮变为可用. 2,实现原理 (1)要检测文本框内容的变化,我们需要让新界面的Controller ...