-----------------------------------------------------------------------------. Counting Binary Trees
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 739 Accepted Submission(s): 256
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.
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 100 4 10 0 0
Sample Output 8 2
Source 2009 “NIT Cup” National Invitational Contest
# include <stdio.h> # include <string.h> # include <iostream> # include <algorithm> # include <vector> # include <queue> # include <set> # include <map> # include <string> # include <math.h> # include <stdlib.h> # include <time.h> # include <queue> using namespace std;
# define LL long long int # define _LL __int64
const LL MOD = 1e9+7; const int MAX = 105; const int MIN = 1005; const double EPS=1e-6;
int prime[10005]; int num[10005]; int k=0;
LL qmod(LL a,LL b,LL c) { LL res=1; while(b) { if(b&1) res=(res*a)%c; b >>= 1; a=(a*a)%c; } return res; }
LL extendeuclid(LL a,LL b,LL &x,LL &y) { if(b==0) { x=1,y=0; return a; } else { LL r = extendeuclid(b,a%b,x,y); LL t = x; x = y; y = t-(a/b)*y; return r; } }