• 快速阶乘


    可以在log2n内计算阶乘。

    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    #define MAXSIZE 100
    unsigned long answer[MAXSIZE];
    
    unsigned long long int power(unsigned long base, unsigned long exp)
    {
        unsigned long long int result = 1;
        while (exp)
        {
            if (exp & 0x01)
                result *= base;
            base *= base;
            exp >>= 1;
        }
    
        return result;
    }
    
    unsigned long cn2(unsigned long n)
    {
        unsigned long x = (1 << n) + 1;
        unsigned long mask = (1 << n) - 1;
    
        return (power(x, n) >> ((n >> 1) * n)) & mask;
    }
    
    unsigned long factorial(unsigned long n)
    {
        unsigned long temp;
    
        if (n == 1)
            return 1;
        else if (n & 0x01 == 1)
            return n * factorial(n - 1);
        else
        {
            temp = factorial(n >> 1);
            return cn2(n) * temp * temp;
        }
    }
    
    void main()
    {
        int number = 4;
        unsigned long result = factorial(number);
        cout << "result = " << result << endl;
    }

    还可以logn内完成,但需要把C(N,N/2),C(N/2,N/4),C(N/4,N/8).....打出来。

    #include <iostream>
    #include <iterator>
    #include <algorithm>
    
    using namespace std;
    
    #define MAXSIZE 100
    unsigned long mask;
    unsigned cnrs[MAXSIZE];
    int number;
    int p_size;
    
    unsigned long power(unsigned long n, unsigned long m)
    {
        unsigned long temp;
    
        if (m == 1)
            temp = n;
        else if (m & 0x01 != 0)
            temp = n * power(n, m - 1);
        else
        {
            temp = power(n, m >> 1);
            temp *= temp;
            cnrs[number++] = (temp >> ((m >> 1) * p_size)) & mask;
        }
    
        return temp;
    }
    
    unsigned factor(unsigned long n)
    {
        unsigned long temp;
    
        if (n == 1)
            return 1;
        else if (n & 0x01 == 1)
            return n * factor(n - 1);
        else
        {
            temp = factor(n >> 1);
            return cnrs[number++] * temp * temp;
        }
    }
    
    unsigned long factorial(unsigned long n)
    {
        unsigned long x = (1 << n) + 1;
    
        number = 0;
        mask = (1 << n) - 1;
        p_size = n;
        power(x, n);
        number = 0;
    
        return factor(n); 
    }
    
    void main()
    {
        int number = 6;
        unsigned long result = factorial(number);
        cout << "result = " << result << endl;
        copy(cnrs, cnrs + 10, ostream_iterator<int>(cout, " "));
    }
  • 相关阅读:
    Visual C#使用DirectX实现视频播放
    sql server中新增一条数据后返回该数据的ID
    JQuery上传插件Uploadify使用详解
    .NET JS escape加密的后台解密
    Redis Sentinel(哨兵模式)
    Redis集群入门
    Redis 4.0.X版本reshard出现错误的解决办法
    首层nginx 传递 二级代理,三级代理......多级代理nginx 客户端真实IP的方法
    Centos6 Ruby 1.8.7升级至Ruby 2.3.1的方法
    SQL Server 2008示例数据库安装问题解决
  • 原文地址:https://www.cnblogs.com/shuaihui520/p/9661794.html
Copyright © 2020-2023  润新知