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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:07:17+00:00 2026-06-09T21:07:17+00:00

The problem: Larry is very bad at math – he usually uses a calculator,

  • 0

The problem:

Larry is very bad at math – he usually uses a calculator, which worked well throughout college. Unforunately, he is now struck in a deserted island with his good buddy Ryan after a snowboarding accident. They’re now trying to spend some time figuring out some good problems, and Ryan will eat Larry if he cannot answer, so his fate is up to you!

It’s a very simple problem – given a number N, how many ways can K numbers less than N add up to N?

For example, for N = 20 and K = 2, there are 21 ways:

0+20
1+19
2+18
3+17
4+16
5+15
...
18+2
19+1
20+0

Input
Each line will contain a pair of numbers N and K. N and K will both be an integer from 1 to 100, inclusive. The input will terminate on 2 0’s.
Output
Since Larry is only interested in the last few digits of the answer, for each pair of numbers N and K, print a single number mod 1,000,000 on a single line.

Sample Input

20 2
20 2
0 0

Sample Output

21
21

The solution code:

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
#define maxn 100

typedef long ss;
ss T[maxn+2][maxn+2];

void Gen() {
    ss i, j;
    for(i = 0; i<= maxn; i++)
        T[1][i] = 1;
    for(i = 2; i<= 100; i++) {
        T[i][0] = 1;
        for(j = 1; j <= 100; j++)
            T[i][j] = (T[i][j-1] + T[i-1][j]) % 1000000;
    }
}

int main() {
    //freopen("in.txt", "r", stdin);
    ss n, m;
    Gen();
    while(cin>>n>>m) {
        if(!n && !m) break;
        cout<<T[m][n]<<endl;
    }
    return 0;
}

How has this calculation been derived?
How has it come T[i][j] = (T[i][j-1] + T[i-1][j]) ?

  • 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-09T21:07:18+00:00Added an answer on June 9, 2026 at 9:07 pm

    Note: I only use n and k (lower case) to refer to some anonymous variable. I will always use N and K (upper case) to refer to N and K as defined in the question (sum and the number of portions).

    Let C(n, k) be the result of n choose k, then the solution to the problem is C(N + K – 1, K – 1), with the assumption that those K numbers are non-negative (or there will be infinitely many solution even for N = 0 and K = 2).

    Since the K numbers are non-negative, and the sum N is fixed, we can think of the problem as: how many ways to divide candy among K people. We can divide the candies, by lying them into a line, and put (K – 1) separator between the candies. The (K – 1) separators will divide the candies up to K portions of candies. Looking at another perspective, it is also like choosing (K – 1) positions among (N + K – 1) positions to put in the separators, then the rest of the positions are candies. So, this explains why the number of ways is N + (K – 1) choose (K – 1).

    Then the problem reduce to how to find the least significant digits of C(n, k). (Since maximum of N and K is 100 as defined in maxn, we don’t have to worry if the algorithm goes up to O(n3)).


    The calculation uses this combinatorial identity C(n, k) = C(n – 1, k) + C(n, k – 1) (Pascal’s rule). The clever thing about the implementation is that it doesn’t store C(n, k) (table of result of combination, which is a jagged array), but it stores C(N, K) instead. The identity is actually present in the T[i][j] = (T[i][j-1] + T[i-1][j]):

    • The first dimension is actually K, the number of portions. And the second dimension is the sum N. T[K][N] will directly store the result, and according to the mathematical result derived above, is (least significant digits of) C(N + K – 1, K – 1).
    • Re-writing the T[i][j] = (T[i][j-1] + T[i-1][j]) back to equivalent mathematical result:

      C(i + j – 1, i – 1) = C(i + j – 2, i – 1) + C(i + j – 2, i – 2), which is correct according to the identity.

    The program will fill the array row by row:

    • The row K = 0 is already initialized to 0, using the fact that static array is initialized to 0.
    • It fills the row K = 1 with 1 (there is only 1 way to divide N into 1 portion).
    • For the rest of the rows, it sets the case N = 0 to 1 (there is only 1 way to divide 0 into K parts – all parts are 0).
    • Then the rest are filled with the expression T[i][j] = (T[i][j-1] + T[i-1][j]), which will refer to the previous row, and the previous element of the same row, both of which has been filled up in earlier iterations.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem Description: We have a service which has applications for main mobile OS’s. We
PROBLEM: Here's the code: import matplotlib.pyplot as plt plt.bar([1,2],[5,4]) plt.title('this is a very long
I was reading Larry Osterman's latest blog post about debugging a flickering problem in
What I have is basically a problem which is easily solved with multiple tables,
Problem: I have an address field from an Access database which has been converted
Problem Statement is:- Each thread uses unique ID between 1 and 1000 and program
Problem: Been struggling to get my code to load external shaders and it is
Problem: I've written State Machine for my android application. It is separate class, extension
Problem: I have a table that prints out vertical but I would like it
Problem Given a boolean expression consisting of the symbols 0, 1, &, |, ^

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.