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 1033311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:12:47+00:00 2026-05-16T14:12:47+00:00

I just saw this question and have no idea how to solve it. can

  • 0

I just saw this question and have no idea how to solve it. can you please provide me with algorithms , C++ codes or ideas?

This is a very simple problem. Given the value of N and K, you need to tell us the value of the binomial coefficient C(N,K). You may rest assured that K <= N and the maximum value of N is 1,000,000,000,000,000. Since the value may be very large, you need to compute the result modulo 1009.
Input

The first line of the input contains the number of test cases T, at most 1000. Each of the next T lines consists of two space separated integers N and K, where 0 <= K <= N and 1 <= N <= 1,000,000,000,000,000.
Output

For each test case, print on a new line, the value of the binomial coefficient C(N,K) modulo 1009.
Example

Input:
3
3 1
5 2
10 3

Output:
3
10
120

  • 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-16T14:12:48+00:00Added an answer on May 16, 2026 at 2:12 pm

    Notice that 1009 is a prime.

    Now you can use Lucas’ Theorem.

    Which states:

    Let p be a prime.
    If n  = a1a2...ar when written in base p and
    if k  = b1b2...br when written in base p
    
    (pad with zeroes if required)
    
    Then
    
    (n choose k) modulo p = (a1 choose b1) * (a2 choose  b2) * ... * (ar choose br) modulo p.
    
    i.e. remainder of n choose k when divided by p is same as the remainder of
    the product (a1 choose b1) * .... * (ar choose br) when divided by p.
    Note: if bi > ai then ai choose bi is 0.
    

    Thus your problem is reduced to finding the product modulo 1009 of at most log N/log 1009 numbers (number of digits of N in base 1009) of the form a choose b where a <= 1009 and b <= 1009.

    This should make it easier even when N is close to 10^15.

    Note:

    For N=10^15, N choose N/2 is more than
    2^(100000000000000) which is way
    beyond an unsigned long long.

    Also, the algorithm suggested by
    Lucas’ theorem is O(log N) which is
    exponentially faster than trying to
    compute the binomial coefficient
    directly (even if you did a mod 1009
    to take care of the overflow issue).

    Here is some code for Binomial I had written long back, all you need to do is to modify it to do the operations modulo 1009 (there might be bugs and not necessarily recommended coding style):

    class Binomial
    {
    public:
        Binomial(int Max)
        {
            max = Max+1;
            table = new unsigned int * [max]();
            for (int i=0; i < max; i++)
            {
                table[i] = new unsigned int[max]();
    
                for (int j = 0; j < max; j++)
                {
                    table[i][j] = 0;
                }
            }
        }
    
        ~Binomial()
        {
            for (int i =0; i < max; i++)
            {
                delete table[i];
            }
            delete table;
        }
    
        unsigned int Choose(unsigned int n, unsigned int k);
    
    private:
        bool Contains(unsigned int n, unsigned int k);
    
        int max;
        unsigned int **table;
    };
    
    unsigned int Binomial::Choose(unsigned int n, unsigned int k)
    {
        if (n < k) return 0;
        if (k == 0 || n==1 ) return 1;
        if (n==2 && k==1) return 2;
        if (n==2 && k==2) return 1;
        if (n==k) return 1;
    
    
        if (Contains(n,k))
        {
            return table[n][k];
        }
        table[n][k] = Choose(n-1,k) + Choose(n-1,k-1);
        return table[n][k];
    }
    
    bool Binomial::Contains(unsigned int n, unsigned int k)
    {
        if (table[n][k] == 0) 
        {
            return false;
        }
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just saw this dating site on builtwithbootstrap and have a question about table
I just saw this question: Understanding .NET’s SecurityAction parameter for permissions And I have
I just saw this question and one of the answers looks really appealing to
I know this may seem like a math question but i just saw this
I just saw this topic: Datatable vs Dataset but it didn't solve my doubt
I just saw this code while studying the wordpress source code (PHP), You can
I saw this post: Typos… Just use option strict and explicit please.. during one
I just saw a very odd thing, I have a co-worker who has their
So in a comment from another question , I just saw this example for
Just saw this question relating to a segmentation fault issue in a C++ class

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.