zoukankan      html  css  js  c++  java
  • 概率的一些练习

    #define Mod 1000000007
    class Ants {
    public:
        vector<int> collision(int n) 
        {
            int down=pow(2.0,n),up=down-2;
            int temp=gcd(up,down);
            vector<int> res;
            res.push_back(up/temp);
            res.push_back(down/temp);
            return res;
        }
        int gcd(int x,int y)
        {
            return (!y)?x:gcd(y,x%y);
        }
    };

    class RandomP {
    public:
        static int f();
    };
    
    class Random01 {
    public:
        // 用RandomP::f()实现等概率的01返回
        int random01() 
        {
            int a,b;
            while(1)
            {
                a=RandomP::f();
                b=RandomP::f();
                if(a!=b)
                    return (a>b)?0:1;
            }
        }
    };

    // 以下内容请不要修改
    class Random5 {
    public:
        static int randomNumber();
    };
    
    class Random7 {
    public:
        int rand5() {
            return Random5::randomNumber();
        }
        // 以上内容请不要修改
    
    
        int randomNumber() 
        {
            int a=5*(rand5()-1)+rand5()-1;
            while(a>20)
                a=5*(rand5()-1)+rand5()-1;
            return a%7+1;
        }  
    };

    class RandomSeg {
    public:
        // 等概率返回[0,1)
        double f() {
            return rand() * 1.0 / RAND_MAX;
        }
        // 通过调用f()来实现
        double random(int k, double x) 
        {
            double res=-1;
            for(int i=0;i<k;i++)
            {
                double b=f();
                res=(res>b)?res:b;
            }
            return res;
        }
    };

    class RandomPrint {
    public:
        vector<int> print(vector<int> arr, int N, int M) 
        {
            vector<int> res;
            for(int i=0;i<M;i++)
            {
                int pos=rand()%(N-i);
                res.push_back(arr[pos]);
                swap(arr[pos],arr[N-i-1]);
            }
            return res;
        }
    };

    #define Mod 1000000007
    class Championship {
    public:
        vector<int> calc(int k) 
        {
            vector<int> res;
            int up=1,down=1,i=2*k-1;
            while(i>0)
            {
                down*=i;
                i=i-2;
            }
            i=k+1;
            while(i>2)
            {
                up*=i;
                i--;
            }
            int Com=gcd(down-up,down);
            res.push_back((down-up)/Com);
            res.push_back(down/Com);
            return res;
        }
        int gcd(int x, int y)
        {
            return (!y)?x:gcd(y,x%y);
        }
    };

  • 相关阅读:
    node.js+mysql接口入门
    input边写边验证?正则表达式写在属性里?小技巧
    创建vue,react项目
    jquery在网页中加载本地json文件
    OpenFeigin服务接口调用
    Ribbon负载均衡服务调用
    Consul服务注册与发现
    Eureka服务注册与发现
    springboot项目在idea实现热部署
    设计模式——单例模式
  • 原文地址:https://www.cnblogs.com/tianzeng/p/11285973.html
Copyright © 2011-2022 走看看