Common recursive, special counting mathematical template

(1) Catalan number (1,1,2,3,5,14,421324291430486216796)

f(n)=f(2)*f(n-1)+f(3)*f(n-2)+...+f(n-1)*f(2)   The boundary is f[2]=f[3]=1;
void Catalan(int n)        
{
    long long count=1;
    for(int i=1,j=2*n;i<=n;i++,j--)
      count=count*j/i;
    cout<<count/(n+1)<<endl;
} 

(2) The second kind of Stirling number and Bell number (result b[n] modulus c) (1,2,5,15,52203877140147975, when n=100, b[n]=751)

#define c 1000
long long a[2005][2005]={1};
long long b[2005];
void Bell(int n,int c)           
{
    for(int i=1;i<=2000;i++)
    {
        a[i][0]=0;
        a[i][i]=1;
        for(int j=1;j<i;j++)
          a[i][j]=(a[i-1][j-1]+j*a[i-1][j])%c;
    }
    for(int i=1;i<=2000;i++)
    {
        b[i]=0;
        for(int j=0;j<=i;j++)
          b[i]=(b[i]+a[i][j])%c;
    } 
    cout<<b[n]<<endl;
}

(3) Stagger count (give n, find out the percentage of 1~n's stagger number in n's total permutation)

Staggered formula: Dn=(1-1+1/2!-...+[(-1)^n]*1/n!)*n!)
void Cuopai(int n)       
{
    double sum,sign=1.0;
    if(n==1)
      sum=0;
    else
    {
        sum=0;
        long long k=1;
        for(int i=2;i<=n;i++)
        {
            k*=i;
            sum=sum+sign/(k*1.0);
            sign=-sign;
        }
        sum*=100;
    }
    printf(".2%%lf",sum);
}

(4) Full Permutation (Full Permutation output of string S letters)

void Pailie(char s[])         
{
    int len=strlen(s);
    sort(s,s+len);
    do
    {
        cout<<s<<endl;
    }while(next_permutation(s,s+len));
}

(5) Triangle count

(from 1 to N, three numbers are arbitrarily suspended, and the length of three sides can form a triangle)

(0,1,3,7,13,22,34,50,70,95,125,161,203,252,308,372,444)

f(n)=f(n-1)+((n-1)*(n-2)/2-(n-1)/2)/2  The boundary is f[3]=0;
void TC(int n)
{
    long long a[maxn];
    a[3]=0;
    for(i=4;i<maxn;i++)
      a[i]=a[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2;
    cout<<a[n]<<endl;
}

(6) Split plane

(0,5,14,27,44,65,90,119,152,189,230,275,324,377,434)

f(n)=f(n-1)+4*(n-1))+1   boundary f[1]=0;
void ZX(int n)       
{
    long long a[maxn];
    a[1]=0;
    for(int i=2;i<10005;i++)
      a[i]=a[i-1]+4*(i-1)+1;
    cout<<a[n]<<endl;
} 

(7) Tricolor problem

(3,6,6,18,50,66,126,258,510,1026,2046,4098,8190,16386,32766)

f(n)=f(n-1)+f(n-2)*2   boundary f[0]=0;
void RPG(int n)        
{
    long long s[55]; 
    s[0]=0;s[1]=3,s[2]=6;s[3]=6;
    for(int i=4;i<=50;i++)
      s[i]=s[i-1]+s[i-2]*2;
    cout<<s[n]<<endl;
}

(8) Bone plate square I (two layers)

(1,2,3,5,8,13,21,34,55,89,

144,233,377,610,987)

 f(n)=f(n-1)+f(n-2)   boundary f[0]=0;
void GPI(int n)          
{
    long long s[50];
    s[0]=0,s[1]=1,s[2]=2;
    for(int i=3;i<50;i++)
      s[i]=s[i-1]+s[i-2];
    cout<<s[n]<<endl;
} 

(9) Bone plate square II (three layers)

(3,11,41,153,571,2131,7953,29681,110771)

F (n) = 3 * f (n-2) + 2 * (f (n-4) +... + F (2)); (n is even, and f(0)=1,f[2]=3)
void GPII(int n)       
{
    int a[17],sum=0;
    a[0]=0,a[1]=1;
    for(int i=2;i<=16;i++)
    {
        sum=sum+a[i-2];
        a[i]=3*a[i-1]+2*sum;
    }
    if(n%2==1)
      cout<<0<<endl;
    else
      cout<<a[n/2+1]<<endl; 
}

(10) Watermelon cutting (2,3,5,8,12,17,23,30,38,47,57,68)

F(n)=(n*n-n+4)/2;
int QXG(int n)    
{
    return (n*n-n+4)/2;
} 

 

Posted by aaronlzw_21 on Sat, 04 Jan 2020 18:21:42 -0800