HDU 3240 Counting Binary Trees
来源:互联网 发布:微信小程序个人淘宝客 编辑:程序博客网 时间:2024/05/16 08:04
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.
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 <= 109) on a single line. The input ends with n = m = 0.
Output
For each test case, print T(n) mod m.
Sample Input
3 1004 100 0
Sample Output
82
卡特兰数+逆元
#include<cstdio>#include<iostream>#include<algorithm>using namespace std;const long long maxn = 100005;long long f[maxn], p[maxn], P = 0, n, m, a[maxn], b[maxn], tot, sum[maxn], ans;void prime(){memset(f, 0, sizeof(f));for (long long i = 2; i < maxn; i++){if (!f[i]) p[P++] = i;for (long long j = 0; j < P&&i * p[j] < maxn; j++){f[i*p[j]] = 1;if (i%p[j] == 0) break;}}}void break_down(long long x){tot = 0;for (long long i = 0; i < P; i++){if (x%p[i] == 0) a[tot++] = p[i];while (x % p[i] == 0) x /= p[i];}}/*long long inv(long long x, long long mod){if (mod % x == 0) return x;return inv(mod % x, mod)*(mod - mod / x) % mod;}*/void egcd(long long a, long long b, long long &x, long long &y){if (b == 0){x = 1, y = 0;return;}egcd(b, a%b, x, y);long long t = x;x = y, y = t - a / b*y;return;}long long insert(long long x, long long y){for (long long i = 0; i < tot; i++){while (x % a[i] == 0) b[i]++, x /= a[i];while (y % a[i] == 0) b[i]--, y /= a[i];}long long i, j;egcd(y, m, i, j);return x * i % m;}long long get(){long long ans = 1;for (long long i = 0; i < tot; i++)for (long long j = 1; j <= b[i]; j++) (ans *= a[i]) %= m;return ans;}int main(){prime();while (cin >> n >> m, n + m){break_down(m);memset(b, 0, sizeof(b));memset(sum, 0, sizeof(sum));ans = 0;for (long long i = sum[0] = 1; i <= n; i++){sum[i] = sum[i - 1] * insert(i * 4 - 2, i + 1) % m;(ans += sum[i] * get()) %= m;}cout << (ans + m) % m << endl;}return 0;}
0 0
- HDU 3240 Counting Binary Trees
- 扩展的欧几里得算法求逆元 hdu-3240-Counting Binary Trees
- HDU 3240 Counting Binary Trees 数论-卡特兰数
- Counting Binary Trees - HDU 3240 卡特兰数+逆元
- HDOJ-3240 Counting Binary Trees(数论)
- HDU 3240 Counting Binary Trees(卡特兰数+分解素数+扩展欧拉求逆元)
- HDU 3240 Counting Binary Trees [卡特兰数] 【数论+组合数学】
- HDU 3240 Counting Binary Trees(组合数学-卡特兰数,数论-整数快速幂,数论-求逆元)
- fzu 1775 Counting Binary Trees 卡特兰数取模求和
- uestc 1554 Counting Binary Trees (catalan 数)
- hdu3240 Counting Binary Trees 卡特兰数 乘法逆元
- HDU3240-Counting Binary Trees(Catalan数+求逆元(非互质))
- HDU3240 Counting Binary Trees (卡特兰数,拓欧求逆元)
- FZU 1775 Counting Binary Trees(卡特兰数前n项和模m)
- FZU 1775 Counting Binary Trees 卡特兰数前n项和%m(m可为非素数
- Counting Factor Trees zoj 3405
- (zju3405)Counting Factor Trees
- zoj 4013 Counting Factor Trees
- HDU 2196 树形DP经典题
- 例题6-2 铁轨(栈)
- 《鸟哥的Linux私房菜》读书笔记:登录文件(log)及其分析
- 计算磁盘IOPS
- JAVA基础笔记三——几种排序算法
- HDU 3240 Counting Binary Trees
- 黑马程序员---Objective-C 三大特性---多态
- 3791 DP
- CSS 用法
- Makefile的应用——进度条实现
- hdu 4734 数位DP
- [AC自动机+dp] whu oj 1572 I - Cyy and Fzz
- The progress of abstraction (抽象过程)
- poj 2342 树形DP