%MOD兼容产生二项式系数的方法系数、方法、MOD、二项式

2023-09-11 23:07:39 作者:莪づ繼續一個人孤單

我要优化我的节目里,我计算二项式系数总和高达K.即

的一部分

  C(N,0)+ C(N,1)+ ... + C(N,K)
 

由于值超出了数据类型(久长)可以支持,我计算出的值模 M ,并一直在寻找的过程来做到这一点。 目前,我已经与杨辉三角做,但它似乎采取位负载。所以,我在想,如果有任何其他有效的方法来做到这一点。我认为卢卡斯的定理,但MI有大,已足以让C(N,K)出去的手!

任何指针,我怎么能做到这一点不同,或许计算整笔款项共与德之和其他一些整齐的EX pression。如果没有,我会离开它与杨辉三角方法本身。 谢谢

下面是我迄今为止 O(N ^ 2)

 的#define MAX 1000000007
长长NChooseK_Sum(INT N,INT K){
    矢量<长长> preVV,V;
    prevV.push_back(1); prevV.push_back(1);
    的for(int i = 2; I< = N; ++ I){
            V.clear();
            V.push_back(1);
            为(诠释J = 0; J&其中;第(i-1); ++ j)条{
                    长长的VAL = preVV [J] + preVV [J + 1];
                    如果(VAL> = MAX)
                            VAL%= MAX;
                    V.push_back(VAL);
            }
            V.push_back(1);
            preVV = V;
    }
    长长的解析度= 0;
    的for(int i = 0; I< = K ++ I){
            RES + = V [I]
            如果(RES> = MAX)
                    RES%= MAX;
    }
    返回水库;
}
 
怎么计算二项式系数

解决方案

这是算法执行算术BIGNUM操作的线性数为

 高清binom(N):
    NCK = 1
    对于k的范围(N + 1):#0到n
        产量NCK
        NCK =(NC *(N  -  K))/(K + 1)
 

本使用分裂,但模素 P ,您可以通过该解决方案我等式 I *(K + 1)= 1模p 。该值可以在算术OPS的对数数通过扩展欧几里德算法。

I would like to optimize a part of my program where I'm calculating the sum of Binomial Coefficients up to K. i.e.

C(N,0) + C(N,1) + ... + C(N,K)

Since the values go beyond the data type (long long) can support, I'm to calculate values mod M and was looking for procedures to do that. Currently, I've done it with Pascal's Triangle but it seems to be taking a bit of load. so, I was wondering if there's any other efficient way to do this. I've considered Lucas' Theorem, although M I have is already large enough so that C(N,k) goes out of hand!

Any pointers as how can I do this differently, maybe calculate the whole sum altogether with some other neat expression of teh sum. If not I'll leave it with the Pascal's Triangle method itself. Thank you,

Here is what I have so far O(N^2) :

#define MAX 1000000007
long long NChooseK_Sum(int N, int K){
    vector<long long> prevV, V;
    prevV.push_back(1);     prevV.push_back(1);
    for(int i=2;i<=N;++i){
            V.clear();
            V.push_back(1);
            for(int j=0;j<(i-1);++j){
                    long long val = prevV[j] + prevV[j+1];
                    if(val >= MAX)
                            val %= MAX;
                    V.push_back(val);
            }
            V.push_back(1);
            prevV = V;
    }
    long long res=0;
    for(int i=0;i<=K;++i){
            res+=V[i];
            if(res >= MAX)
                    res %= MAX;
    }
    return res;
}

解决方案

An algorithm that performs a linear number of arithmetic bignum operations is

def binom(n):
    nck = 1
    for k in range(n + 1):  # 0..n
        yield nck
        nck = (nck * (n - k)) / (k + 1)

This uses division, but modulo a prime p, you can accomplish much the same thing by multiplying by the solution i to the equation i * (k + 1) = 1 mod p. The value i can be found in a logarithmic number of arithmetic ops via the extended Euclidean algorithm.

 
精彩推荐
图片推荐