Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:45:30+00:00 2026-05-30T03:45:30+00:00

I would like to optimize a part of my program where I’m calculating the

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:45:32+00:00Added an answer on May 30, 2026 at 3:45 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to implement an B+ tree in Java and try to optimize
Would like to know the c# code to actually retrieve the IP type: Static
I am working with ASP.NET MVC 2 and would like to optimize my routing.
I would like to read how to use caching effectively, optimize my database schema
I would like to optimize my scp deployment which currently copies all files to
I would like to optimize my perl script because it is a bit slow
I would like to optimize this query using SQLite 3. SELECT id FROM Table
I have many complex SQL queries that I would like to optimize. The performance
I came across the following code recently and would like to optimize it: Public
I got this code, I would like to optimize. I basically can add new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.