HDU 3240 counting Binary Trees)

Source: Internet
Author: User

Counting Binary Trees

Problem descriptionthere are 5 distinct Binary Trees of 3 nodes:

Let T (n) be the number of distinct non-empty Binary Trees of no more NNodes, your task is to calculate T (n) mod M.
Inputthe 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.
Outputfor each test case, print T (n) mod m.
Sample Input
3 1004 100 0
 
Sample output
82
 
Source2009 "nit Cup" national invitational contest
Recommendzhonglihua | we have carefully selected several similar problems for you: 3249 3241 3242 3243

Question:

Ask you the number of Binary Tree solutions with no more than N nodes, and the result requires M remainder.


Solution:

The number of schemes can be divided between the left and right sides, that is, H (n) = H (0) * H (n-1) + H (1) * (H-2) + .................. + H (n-1) * H (0), which is the number of catlands,

The number of catlands must be about the efficiency of NLG ^ n.

Baidu:

H (0) = 1, H (1) = 1,

The Catalan number meets the following requirements:

Recursive Formula [1]: H (n) = H (0) * H (n-1) + H (1) * H (n-2) +... + H (n-1) H (0) (N> = 2)
Recursive Formula [2]: H (n) = H (n-1) * (4 * N-2)/(n + 1 );
Recursive Formula [3]: H (n) = C (2n, N)/(n + 1) (n =, 2 ,...)
Recursive Formula [4]: H (n) = C (2n, n)-C (2n, n + 1) (n =, 2 ,...)

We choose, recursive formula [2]: H (n) = H (n-1) * (4 * N-2)/(n + 1 );

However, this recursion requires the remainder, and the multiplication requires the remainder. Division requires the inverse element. The necessary and sufficient condition for the Division is gcd (A, MoD) = 1, that is, the mass of the divisor and the number to be modeled.

So, for MOD decomposition factors, for factors in denominator

(1) Calculate the inverse element directly for the denominator of each other.

(2) numbers without mutual quality cannot reverse the element, and only molecules can be used for elimination.


Solution code:

#include <iostream>#include <cstdio>#include <map>#include <vector>#include <cstring>using namespace std;typedef long long ll;const int maxn=50;int tol,prime[maxn],cnt[maxn],n,mod;void getPrime(){    tol=0;    int x=mod;    for(int i=2;i*i<=x;i++){        if(x%i==0){            prime[tol++]=i;            while(x%i==0) x/=i;        }    }    if(x>1) prime[tol++]=x;}void extend_gcd(int a,int b,int &x,int &y){     if(b==0){        x=1;        y=0;     }else{        extend_gcd(b,a%b,x,y);        int tmp=x;        x=y;        y=tmp-(a/b)*y;     }}int inv(int a,int mod){    int x,y;    extend_gcd(a,mod,x,y);    return (x%mod+mod)%mod;}void deal1(ll &ret,int x){    for(int i=0;i<tol;i++){        while(x%prime[i]==0){            x/=prime[i];            cnt[i]++;        }    }    ret=(ret*x)%mod;}void deal2(ll &ret,int x){    for(int i=0;i<tol;i++){        while(x%prime[i]==0){            x/=prime[i];            cnt[i]--;        }    }    if(x>1){        int tmp=inv(x,mod);        ret=(ret*tmp)%mod;    }}ll pow_mod(ll a,ll b,ll p){    ll sum=1;    while(b>0){        if(b&1) sum=(sum*a)%p;        a=(a*a)%p;        b/=2;    }    return sum%p;}void solve(){    ll ans=1,ret=1;    memset(cnt,0,sizeof(cnt));    getPrime();    for(int i=2;i<=n;i++){        deal1(ret,4*i-2);        deal2(ret,i+1);        ll tmp=ret;        for(int t=0;t<tol;t++){            tmp=( tmp*pow_mod(prime[t],cnt[t],mod) )%mod;        }        ans=(ans+tmp)%mod;    }    cout<<ans<<endl;}int main(){    while(cin>>n>>mod && (n||mod) ){        solve();    }    return 0;}







HDU 3240 counting Binary Trees)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.