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 ...
随机推荐
- 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 ...
- Windows通过远程桌面访问Ubuntu
关于Windows通过远程桌面访问Ubuntu 问题及目标 Window环境通过远程桌面访问Ubuntu Ubuntu机器端 1. 安装所需软件包 sudoapt-get install x ...
- 三篇IMO的文章
http://column.iresearch.cn/b/201411/687499.shtml?from=singlemessage&isappinstalled=1 http://colu ...
- GDI+: Curved Shapes
原文 http://www.functionx.com/vcsharp2003/gdi/curves.htm Curves Introduction to Curves A curve is ...
- 浅析嵌入式Linux系统的构成和启动过程
在我们的周围,大量的嵌入式设备都是基于Linux系统来构建的,嵌入式Linux与主机Linux相比有着自己的一些特点,本文就嵌入式Linux系统的构成和启动过程做一些总结. 一.嵌入式Linux系统构 ...
- UVA11552------FEWEST FLOPS------区间型的DP
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- Android程序检测网络是否可用
在做Android应用程序中,连接网络的时候,常常要用到检测网络状态是否可用,在这里分享一个比较好用的方法. 本人参考:http://blog.csdn.net/sunboy_2050/article ...
- Swift - 多线程实现方式(2) - NSOperation和NSOperationQueue
1,Swift继续使用Object-C原有的一套线程,包括三种多线程编程技术: (1)NSThread (2)Cocoa NSOperation(NSOperation和NSOperationQueu ...
- winform基础——实现简易赈灾物资发放登记系统
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- 查看 ios 真机调试log,导出log
使用Xcode 在模拟器李敏运行的时候,可以直接通过xcode 查看log,但是真机测试的时候,xcode 却无法获取到,对于日志输出,可以先保存到真机上,之后通过iTunes 导出即可 修改源码 此 ...