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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:37:02+00:00 2026-06-01T13:37:02+00:00

The following is the problem description: let c[n] be the catalan number for n

  • 0

The following is the problem description:

let c[n] be the catalan number for n and p be a large prime eg.1000000007

I need to calculate c[n] % p where n ranges from {1,2,3,...,1000}

The problem which I am having is that on a 32 bit machine you get overflow when you calculate catalan number for such large integer. I am familiar with modulo arithmetic. Also

(a.b) % p = ((a % p)(b % p)) % p 

this formula helps me to get away with the overflow in numerator separately but I have no idea how to deal with denominators.

  • 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-06-01T13:37:03+00:00Added an answer on June 1, 2026 at 1:37 pm

    For a modulus of 1000000007, avoiding overflow with only 32-bit integers is cumbersome. But any decent C implementation provides 64-bit integers (and any decent C++ implementation does too), so that shouldn’t be necessary.

    Then to deal with the denominators, one possibility is, as KerrekSB said in his comment, to calculate the modular inverse of the denominators modulo the prime p = 1000000007. You can calculate the modular inverse with the extended Euclidean algorithm or, equivalently, the continued fraction expansion of k/p. Then instead of dividing by k in the calculation, you multiply by its modular inverse.

    Another option is to use Segner’s recurrence relation for the Catalan numbers, which gives a calculation without divisions:

    C(0) = 1
             n
    C(n+1) = ∑ C(i)*C(n-i)
             0
    

    Since you only need the Catalan numbers C(k) for k <= 1000, you can precalculate them, or quickly calculate them at program startup and store them in a lookup table.


    If contrary to expectation no 64-bit integer type is available, you can calculate the modular product by splitting the factors into low and high 16 bits,

    a = a1 + (a2 << 16)    // 0 <= a1, a2 < (1 << 16)
    b = b1 + (b2 << 16)    // 0 <= b1, b2 < (1 << 16)
    a*b = a1*b1 + (a1*b2 << 16) + (a2*b1 << 16) + (a2*b2 << 32)
    

    To calculate a*b (mod m) with m <= (1 << 31), reduce each of the four products modulo m,

    p1 = (a1*b1) % m;
    p2 = (a1*b2) % m;
    p3 = (a2*b1) % m;
    p4 = (a2*b2) % m;
    

    and the simplest way to incorporate the shifts is

    for(i = 0; i < 16; ++i) {
        p2 *= 2;
        if (p2 >= m) p2 -= m;
    }
    

    the same for p3 and with 32 iterations for p4. Then

    s = p1+p2;
    if (s >= m) s -= m;
    s += p3;
    if (s >= m) s -= m;
    s += p4;
    if (s >= m) s -= m;
    return s;
    

    That way is not very fast, but for the few multiplications needed here, it’s fast enough. A small speedup should be obtained by reducing the number of shifts; first calculate (p4 << 16) % m,

    for(i = 0; i < 16; ++i) {
        p4 *= 2;
        if (p4 >= m) p4 -= m;
    }
    

    then all of p2, p3 and the current value of p4 need to be multiplied with 216 modulo m,

    p4 += p3;
    if (p4 >= m) p4 -= m;
    p4 += p2;
    if (p4 >= m) p4 -= m;
    for(i = 0; i < 16; ++i) {
        p4 *= 2;
        if (p4 >= m) p4 -= m;
    }
    s = p4+p1;
    if (s >= m) s -= m;
    return s;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing the following problem: in the controller I select the data I need
I've got following problem: (c#) There is some class (IRC bot), which has method,
Problem description: The setup is executing Ant build script with TestNG target, which then
Problem description I have employed the convolution theorem to calculate convolutions efficiently. Suppose there
Given the following xml fragment: <Problems> <Problem> <File>file1</File> <Description>desc1</Description> </Problem> <Problem> <File>file1</File> <Description>desc2</Description> </Problem>
Following problem: I want to render a news stream of short messages based on
The following problem happens on both Safari and Chrome, so probably a WebKit issue.
I'm having the following problem and wondered whether anyone could see why this is
I face the following problem function book($memberid, $classid){ if (!book){ // update the db
I have the following problem: I have a form in site/banen (currently local running

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.