排列组合的一些练习

#define Mod 1000000007
class CombineByMistake {
public:
    int countWays(int n)
    {
        if(n==0||n==1)
            return 0;
        if(n==2)
            return 1;
        int pre=0,last=1,temp=0;
        for(int i=3,temp=0;i<=n;i++)
        {
            temp=(long long)(i-1)*(long long)(last+pre)%Mod;
            pre=last;
            last=temp;
        }
        return last;
    }
};

 

class TreeCount 
{
public:
    int countWays(int n) 
    {
        return zuhe(2*n,n)/(n+1);
    }
    int zuhe(int m,int n)
        //n<=m
    {
        int A=1,B=1,i=0;
        while(i<n)
        {
            A*=(m-i);
            i++;
        }
        while(n)
        {
            B*=n--;
        }
        return (A/B);
    }
};

using namespace std;
class Robot {
public:
    int countWays(int x, int y) 
    {
        return zuhe(x+y-2,x-1);

    }
    int zuhe(int m,int n)
    {
        int A=1,B=1,i=0;
        while(i<n)
        {
            A*=(m-i);
            i++;
        }
        while(n)
        {
            B*=n--;
        }
        return (A/B);

    }
};

//看做这几个糖果中放隔板,几天吃完放几个隔板
class Distribution {
public:
    int getWays(int n, int m) 
    {
        return zuhe(n-1,m-1);
    }
    int zuhe(int m,int n)
    //n<=m
    {
        int A=1,B=1,i=0;
        while(i<n)
        {
            A*=(m-i);
            i++;
        }
        while(n)
        {
            B*=n--;
        }
        return (A/B);
    }
};

class HighAndShort 
{
public:
    int countWays(int n) 
    {
        return zuhe(n,n/2)/(n/2+1);
    }
    int zuhe(int m,int n)
        //n<=m
    {
        int A=1,B=1,i=0;
        while(i<n)
        {
            A*=(m-i);
            i++;
        }
        while(n)
        {
            B*=n--;
        }
        return (A/B);
    }
};

class LonelyA {
public:
    int getWays(int n, int A, int b, int c)
    {
        int s1=pailie(n),s2=pailie(n-1),s3=pailie(n-2);
        return (s1-4*s2+2*s3);

    }
    int pailie(int m)
    {
        int res=1;
        while(m)
        {
            res*=m;
            m--;
        }
        return res;
    }
};

class LonelyA {
public:
    int getWays(int n, int A, int b, int c)
    {
        int s1=pailie(n),s2=pailie(n-1),s3=pailie(n-2);
        return (s1-4*s2+2*s3);

    }
    int pailie(int m)
    {
        int res=1;
        while(m)
        {
            res*=m;
            m--;
        }
        return res;
    }
};

class Stack {
public:
    int countWays(int n) 
    {
        return zuhe(2*n,n)/(n+1);
    }
    int zuhe(int m,int n)
        //n<=m
    {
        int A=1,B=1,i=0;
        while(i<n)
        {
            A*=(m-i);
            i++;
        }
        while(n)
        {
            B*=n--;
        }
        return (A/B);
    }
};

class Parenthesis {
public:
    int countLegalWays(int n) 
    {
        return zuhe(2*n,n)/(n+1);
    }
    int zuhe(int m,int n)
        //n<=m
    {
        int A=1,B=1,i=0;
        while(i<n)
        {
            A*=(m-i);
            i++;
        }
        while(n)
        {
            B*=n--;
        }
        return (A/B);
    }
};

class BuyTickets {
public:
    int countWays(int n) 
    {
        return zuhe(2*n,n)/(n+1);
    }
    int zuhe(int m,int n)
        //n<=m
    {
        int A=1,B=1,i=0;
        while(i<n)
        {
            A*=(m-i);
            i++;
        }
        while(n)
        {
            B*=n--;
        }
        return (A/B);
    }
};

class StandInLine {
public:
    vector<int> getWays(int n, int a, int b) 
    {
        vector<int> res;
        res.push_back(pailie(n)/2);
        res.push_back(pailie(n-1));
        return res;

    }
    int pailie(int m)
    {
        int res=1;
        while(m)
        {
            res*=m;
            m--;
        }
        return res;
    }
};

 

posted on 2019-08-01 22:13  tianzeng  阅读(238)  评论(0编辑  收藏  举报

导航