Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC2 (Handbook of Crazy Constructions) and looks for the right chapter:
How to build a wall:
Take a set of bricks.
Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader.
Place bricks on top of each other, according to the chosen design.
This seems easy enough. But Heidi is a Coding Cow, not a Constructing Cow. Her mind keeps coming back to point 2b. Despite the imminent danger of a zombie onslaught, she wonders just how many possible walls she could build with up to n bricks.
A wall is a set of wall segments as defined in the easy version. How many different walls can be constructed such that the wall consists of at least 1 and at most n bricks? Two walls are different if there exist a column c and a row r such that one wall has a brick in this spot, and the other does not.
Along with n, you will be given C, the width of the wall (as defined in the easy version). Return the number of different walls modulo 106 + 3.
Input
The first line contains two space-separated integers n and C, 1 ≤ n ≤ 500000, 1 ≤ C ≤ 200000.
Output
Print the number of different walls that Heidi could build, modulo 106 + 3.
Examples
Input
5 1
Output
5
Input
2 2
Output
5
Input
3 2
Output
9
Input
11 5
Output
4367
Input
37 63
Output
230574
Note
The number 106 + 3 is prime.
In the second sample case, the five walls are:
code
B B
B., .B, BB, B., and .B
In the third sample case, the nine walls are the five as in the second sample case and in addition the following four:
code
B B
B B B B
B., .B, BB, and BB
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to understand the problem. There is an "easy version" defining "wall segments". The problem statement is cryptic. Likely from some contest; we need to interpret based on given examples.
We have: "A wall is a set of wall segments as defined in the easy version." So we need to know what a wall segment is. Probably from an earlier problem "Building Walls" maybe.
Let me search memory: There's a known problem "Building Walls" where you have bricks of size 1x1 and you build a wall of width C, and you place bricks on top of each other, but bricks are placed with some rules? The easy version might have defined that a wall segment is a configuration of bricks in a single column? Or perhaps it's about building a wall using bricks that are 1x1 and you can place them in rows and columns, with the condition that bricks cannot be placed on top of empty space? Not sure.
Alternatively, the problem might be about compositions: "wall" built from "segments" that are basically columns of bricks? The note: "In the second sample case, the five walls are: B., .B, BB, B., and .B" Wait they show something: "B., .B, BB, B., and .B" That seems inconsistent. Actually the listing:
code
B B
B., .B, BB, B., and .B
That is confusing. It seems they are depicting two rows? Possibly each wall is a 2x2 grid? Because C=2, and n is up to 2 bricks. They show five walls.
Let's try to parse: For C=2, the five walls are:
B. (maybe first column has brick, second column none)
.B (first column none, second column brick)
BB (both columns have bricks)
B. (again? That seems duplicate) Actually they wrote: "B., .B, BB, B., and .B" Wait it's "B., .B, BB, B., and .B". That would be two duplicates? That seems like a mistake. Maybe they meant:
B., .B, BB,
B
.
and
.
B ? Not sure.
But in the third sample, for n=3, C=2, output 9. They say the nine walls are the five as in second sample case and in addition the following four:
code
B B
B B B B
B., .B, BB, and BB
Again cryptic: "B B" maybe indicating two rows? It might be that a wall can have multiple rows. So a wall is a set of bricks placed in a grid of columns (width C) and rows (some height). Possibly bricks are 1x1 units, and they are placed on a grid such that each brick rests on either the ground or on top of another brick, and no brick can be placed without support? Something like that.
But the definition: "A wall is a set of wall segments as defined in the easy version." And "How to build a wall: 1. Take a set of bricks. 2. Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader. 3. Place bricks on top of each other, according to the chosen design." That seems like a humorous step.
Maybe the "easy version" was about counting number of possible walls for a given number of bricks? Possibly it's a combinatorial structure: "wall segments" could be columns with some height, and a wall is a multiset of such columns placed side by side? No, it says "wall consists of at least 1 and at most n bricks". And difference: Two walls are different if there exist a column c and a row r such that one wall has a brick in this spot, and the other does not. So essentially walls are subsets of cells of an infinite grid? But with constraints: a brick can only be placed if the cell below is also a brick (like gravity). Usually in such brick stacking problems, you can't have a floating brick; bricks must be placed on the ground or on top of another brick, and they must be contiguous? But not necessarily contiguous horizontally.
Also the width C is given; that likely means the wall spans exactly C columns? Or at most C? Usually width means the wall occupies columns 1 through C. So the grid is of width C, infinite height. Cells are integer coordinates (col, row) with row starting at 1 maybe. Bricks are 1x1. You can place bricks in some cells such that there is no brick placed above an empty cell? Actually typical "wall" building: you place bricks so that each brick rests on either the ground (row 1) or on a brick directly below it. That ensures the structure is stable. Also possibly bricks cannot overhang: they must have full support from below. So the wall must be a Young diagram shape: a left-justified shape? Wait, if we have multiple columns, bricks in higher rows require bricks directly below them. That means that if there's a brick at (c, r), then there must be a brick at (c, r-1). Also ground row is considered always present? The ground is not a brick but it's the foundation, so row 0 maybe considered always present. So the condition is: For any brick at (c, r), r >=1, there must be a brick at (c, r-1) (unless r=1, which is ground). So the configuration corresponds to choosing a set of cells such that if a cell is present, all cells below it in the same column are present. That is equivalent to each column having a certain height (number of bricks stacked in that column). Then the wall is described by a tuple (h1, h2, ..., hC) where hi >= 0 are heights of columns, and the total number of bricks = sum hi.
Now, is that all? Usually such a configuration is a "wall". However, the problem mentions "wall segments". In easy version maybe they defined a wall segment as a contiguous block of bricks? Or maybe there is also a condition about "no holes": bricks must be placed in columns from 1 to C, but they can have different heights; it's just independent columns.
If that's the case, then the number of different walls with exactly k bricks is the number of ways to distribute k indistinguishable bricks into C columns where order matters (columns are distinct), i.e., number of integer vectors (h1,...,hC) with sum = k, hi >= 0. That number is C(k+C-1, C-1). Then total number of walls with at most n bricks (and at least 1) is sum_{k=1}^{n} C(k+C-1, C-1). That sum might have closed form: sum_{k=0}^{n} C(k+C-1, C-1) - 1 = C(n+C, C) - 1? Check identity: Sum_{k=0}^{n} C(k+r, r) = C(n+r+1, r+1). Indeed sum_{k=0}^{n} binom(k+r, r) = binom(n+r+1, r+1). Here r = C-1. So sum_{k=0}^{n} binom(k+C-1, C-1) = binom(n+C, C). So total with at least 1 brick is binom(n+C, C) - 1.
Sample 5: n=37, C=63. binom(37+63,63)-1 = binom(100,63)-1 mod 1e6+3. binom(100,63) = binom(100,37). Need compute modulo prime 1e6+3 = 1000003. Let's check if output matches 230574. Compute binom(100,37) mod 1000003? Let's verify quickly to ensure it matches. We'll compute later. But likely matches.
Thus the problem is indeed simply compute (C(n+C, C) - 1) mod (10^6+3). Great!
But wait: The problem statement includes "A wall is a set of wall segments as defined in the easy version." and "Two walls are different if there exist a column c and a row r such that one wall has a brick in this spot, and the other does not." So indeed it matches column heights representation.
Also note: "Compute the number of possible designs is left as an exercise to the reader." That's a joke: It's just binomial coefficient.
Thus the solution is straightforward: given n and C, compute binom(n+C, C) mod p where p=1000003, then subtract 1 mod p (if result 0, add p? Actually need positive mod). But careful: we need at least 1 brick, so subtract 1.
But we have constraints: n up to 500,000, C up to 200,000. p is 1,000,003 which is a prime (as stated). But note that n and C can be large, but n+C can be up to 700,000? Actually n max 500k, C max 200k, so n+C max 700k, which is less than p=1,000,003. So n+C < p? 500k+200k = 700k < 1e6+3. So n+C <= 700,000 < 1,000,003. So the binomial coefficient binom(n+C, C) mod p can be computed using standard factorial modulo p, because n+C < p, so factorials will be less than p (but factorial can exceed p; but since n+C < p, the factorials themselves are not multiples of p because p is prime larger than the numbers, so factorial mod p is non-zero and we can compute inverse using Fermat. Actually careful: factorial modulo p for numbers up to p-1 are non-zero because p is prime and none of the numbers 1..p-1 are divisible by p. So we can compute factorials up to n+C and then compute binom directly.
But note that n+C could be as high as 700,000, which is less than 1,000,003, so indeed we can precompute factorials up to 700,000. That's feasible.
Alternatively, we could use formula binom(n+C, C) = (n+C)! / (n! C!). Since p is prime > n+C, all factorials are invertible mod p. So answer = (fact[n+C] * inv_fact[n] * inv_fact[C] % p - 1 + p) % p.
We must ensure we handle the case where n or C might be zero? But constraints: n ≥ 1, C ≥ 1, so at least 1 brick, C>=1, so fine.
But note: There's also possibility that n is large but still less than p. So we can precompute factorial up to maxN = n+C, which is at most 700,000. That's fine.
Time complexity O(maxN) precomputation, then O(1) per query (only one query). Memory O(maxN).
Edge case: if we compute factorial up to 700,000 and store, that's okay.
Alternatively, we can compute directly using Lucas theorem? Not needed.
Thus solution is trivial: read n, C; compute L = n+C; precompute factorials up to L; compute binom; subtract 1.
But wait: The problem might be more subtle? The note says "The number 10^6+3 is prime." This is often used when n and C might be larger than p, requiring Lucas theorem. But given constraints n ≤ 500,000, C ≤ 200,000, they guarantee that n+C ≤ 700,000 < p = 1,000,003, so we are safe. However, perhaps there is another nuance: The sum from k=1 to n of binom(k+C-1, C-1) equals binom(n+C, C) - 1, which we already used.
Thus answer is as above.
But we should verify sample 5: n=37, C=63 => n+C=100. binom(100,63) mod 1000003? Compute binom(100,63)= binom(100,37). Let's compute exact value: binom(100,37) is huge, but mod 1000003? Since 100 < p, we can compute exactly mod. Let's compute quickly to ensure matches 230574.
We'll compute using Python mental? Let's approximate: binom(100,37) mod 1,000,003. Could be 230575? Actually output is 230574, so binom(100,63) -1 = 230574 => binom(100,63) = 230575 mod p? Wait output is 230574, which is answer after subtract 1. So binom(100,63) mod p should be 230575? Because answer = binom -1 mod p. So we expect binom(100,63) ≡ 230575 (mod p). Let's test roughly: binom(100,37) actual value? 100 choose 37 is about 1.37e27, not known mod. We can compute quickly using known values: But it's plausible. Let's just trust.
Thus solution is straightforward.
But wait: There is also "The number of possible designs is left as an exercise to the reader." That is a joke: it's the binomial coefficient. So problem is basically compute (C(n+C, C) - 1) mod (10^6+3).
Thus the only trick is to ensure we do modulo correctly and subtract 1.
Implementation details:
p = 1000003 (a prime)
Input: n, C
L = n + C
Precompute fact array of size L+1
fact[0] = 1; for i in 1..L: fact[i] = fact[i-1] * i % p
Then for i from L down to 1: inv_fact[i-1] = inv_fact[i] * i % p (or compute all via pow)
Then binom = fact[L] * inv_fact[n] % p * inv_fact[C] % p
ans = (binom - 1) % p (add p if negative)
Print ans.
But note that we need inv_fact[n] and inv_fact[C] separately. Since n and C are <= L, fine.
Memory: L up to 700,000, storing two arrays of ints (fact and inv_fact) ~ 5.6 MB, fine.
Alternatively, we could compute binom using multiplication and division without precomputing all factorials? But easier to precompute.
Edge case: Could L be 0? No.
Also, note that n can be up to 500k, C up to 200k, L up to 700k, but p is 1,000,003. So factorials computed mod p, but because L < p, factorial values are non-zero mod p, good.
Thus solution is straightforward.
But wait, the problem states "Return the number of different walls modulo 106 + 3." That's 10^6+3, prime.
Now, before writing final answer, we should double-check the derivation: Is the wall just columns of bricks stacked? Could there be any other constraints like no overhangs across columns? Usually in a wall built by stacking bricks, bricks might be placed in a staggered pattern, like in a brick wall, where bricks in successive rows are offset. But the problem statement is very minimal. However, the sample matches the simple column heights model perfectly. So it's confirmed.
Also note: In the sample, for C=2, the list of walls with up to 2 bricks: They list 5 walls: (1) B. (column1 has brick, column2 empty) (2) .B (column2 has brick) (3) BB (both columns have bricks) (4) B. (??) Actually they might have meant two rows? Let's interpret: For walls with up to 2 bricks, there are indeed 5 configurations if we consider column heights: heights (1,0), (0,1), (1,1), (2,0), (0,2). That's 5 configurations. But the representation they gave: "B., .B, BB, B., and .B" seems like they wrote the same (1,0) and (0,1) twice? Could be that they intended to list: B., .B, BB, B (with a newline) .? Actually the formatting is messed up. In the note they show:
In the second sample case, the five walls are:
code
B B
B., .B, BB, B., and .B
That looks like a text-art representation of walls with two rows? Possibly they are showing each wall as a 2-row representation? For (2,0): column1 has height 2, column2 height 0 -> that would be two bricks stacked in column1. In ASCII they might show something like:
B
B.
That could be "B\n."? Hard to parse. But anyway.
Thus our solution is correct.
Now, we must implement. Since it's a single test case, we can just compute directly.
But the problem might be from some contest with multiple test cases? The input is only one line. So it's a single case.
Time complexity: O(L) which is up to 700k, fine.
Potential pitfalls: The modulus is 1000003, which is not a huge prime but still prime. We need to use pow for modular inverse.
Implementation in C++ or Python? The answer expects likely a compiled language but Python should be okay with 700k operations, but careful: Python's modulo operations are fine. However, input size small. Python can handle 700k precomputation quickly.
But typical online judge might have tight time for Python? Usually Python is fine for 700k loops. But we need to be careful about recursion? Not needed.
Alternatively, we can compute binom using Lucas if needed but not.
Thus final solution:
Pseudo-code:
read n, C
p = 1000003
maxN = n + C
fact = [1](maxN+1)
for i in range(1, maxN+1):
fact[i] = fact[i-1] * i % p
inv_fact = [1](maxN+1)
inv_fact[maxN] = pow(fact[maxN], p-2, p)
for i in range(maxN, 0, -1):
inv_fact[i-1] = inv_fact[i] * i % p
binom = fact[maxN] * inv_fact[n] % p * inv_fact[C] % p
ans = (binom - 1) % p
print(ans)
Double-check: binom(n+C, C) uses fact[n+C] / (n! * C!). Yes.
But note: When computing inv_fact for i from maxN down to 1, we need to compute inv_fact[i-1] = inv_fact[i] * i % p. This works because inv_fact[i] = (i!)^{-1} mod p, so inv_fact[i] * i = (i!)^{-1} * i = ((i-1)!)^{-1} mod p.
Alternatively, compute inv_fact via pow for each of n and C separately: inv_n! = pow(fact[n], p-2, p) etc. But that would be three pow calls, still okay. But precomputing all is efficient.
Edge Cases: If n+C = 0? Not possible. Also, if n+C >= p? Not as per constraints. But what if they extended? Actually constraints ensure n+C <= 700k < p, so fine.
Thus answer is straightforward.
But wait: Could there be a scenario where the answer is negative after subtraction? We mod correctly.
Now, let's compute sample 5 manually to double-check.
n=37, C=63 => n+C=100. So compute binom(100,63) mod 1000003.
We can compute binom(100,63) = binom(100,37). Let's compute using Python mental? Could compute using known values modulo 1e6+3. Let's attempt:
We want to confirm that binom(100,37) mod 1000003 = 230575. Then answer = 230574.
Compute binom(100,37) mod 1000003:
We can compute manually but maybe trust.
But let's compute quickly using small script mental? Not needed; sample matches.
Thus solution accepted.
Now, we should note that the problem might require using long ints and modulo operations. In C++, use long long.
But there's a potential twist: The number of bricks is at most n, but the wall can have any number of bricks from 1 to n inclusive. Our sum formula works.
Double-check: For n=1, C=anything: binom(1+C, C)-1 = binom(C+1, C)-1 = (C+1)-1 = C. Is that correct? With C columns, if we have at most 1 brick, the possibilities: each column can have either 0 or 1 brick, but total bricks <=1, so exactly one column can have 1 brick (or all zero? but at least 1 brick so exactly one column has 1 brick). There are C possibilities. So binom(1+C, C)-1 = (C+1 choose C) = C+1, minus 1 = C. Correct.
For n=0? Not allowed.
Thus correct.
Now, is there any other interpretation that would affect the answer? Suppose the wall is built from bricks of size 1x1 but they must be placed in a "staggered" pattern? That would give different counts. But given sample matches binomial, it's clear.
Thus final answer: compute binomial.
I'll now write the solution.
But we need to be careful with modulo: p=1000003, which is 1000003. When computing factorial, we multiply modulo p.
Also note that n and C up to 200k and 500k, but maxN = 700k, which is less than p. So factorial values will never be 0 mod p, because p is prime and 700k! does not contain factor p (since p > 700k). So no division by zero issues.
Now, the code.
Implementation in C++:
cpp
#include <iostream>
using namespace std;
const int MOD = 1000003;
const int MAX = 700000; // but we can compute dynamic
int main() {
int n, C;
cin >> n >> C;
int L = n + C;
// allocate arrays size L+1
long long* fact = new long long[L+1];
long long* inv_fact = new long long[L+1];
fact[0] = 1;
for (int i = 1; i <= L; ++i) {
fact[i] = fact[i-1] * i % MOD;
}
// compute inverse factorial using Fermat
inv_fact[L] = 1;
// compute pow(fact[L], MOD-2)
long long exp = MOD - 2;
long long base = fact[L];
long long res = 1;
while (exp > 0) {
if (exp & 1) res = res * base % MOD;
base = base * base % MOD;
exp >>= 1;
}
inv_fact[L] = res;
for (int i = L; i > 0; --i) {
inv_fact[i-1] = inv_fact[i] * i % MOD;
}
long long binom = fact[L] * inv_fact[n] % MOD * inv_fact[C] % MOD;
long long ans = (binom - 1 + MOD) % MOD;
cout << ans << endl;
delete[] fact;
delete[] inv_fact;
return 0;
}
But we can simplify: compute inv_fact[L] using pow from cmath? Better use loop.
Alternatively, we could compute inv_fact using precomputed inverse array: but above fine.
But L up to 700k, dynamic allocation on stack may be large; better use vector or global array. Since 700k, we can use static array of size 700001? That's safe. But to be safe, we can allocate with vector or dynamic.
I'll use vector.
Also we can compute binom without full inv_fact: compute inv_n! and inv_C! using pow, then multiply. But that would require two pow calls, still fine. However, computing inv_fact for all i is not necessary but fine.
Given constraints, we can precompute up to 700k.
Edge: if L=0? Not possible.
Thus answer.
But note: In C++ we must be careful about integer overflow; use long long.
Alternatively, we can compute using Lucas if needed but not.
Thus final solution.
But I'd like to double-check sample 5 with code mental? Let's quickly compute using Python mental? Eh not necessary.
Now, can we derive a closed form without precomputing factorials? Since n+C up to 700k, we can compute binom via multiplicative formula:
binom = product_{i=1}^{C} (n+i)/i mod p. Since p is prime and all numbers are less than p, we can compute product and multiply by modular inverses of i. That's essentially the same as computing factorials. But maybe we can compute directly in O(C) which is up to 200k, which is fine too. But O(L) 700k also fine. Either way.
But we need to handle inverse of i mod p. Since p is prime, we can precompute inv[i] using Fermat's little theorem: inv(i) = pow(i, p-2) mod p, but doing that for each i would be heavy. Better to compute factorial.
But we could compute binom iteratively: ans = 1; for i=1 to C: ans = ans * (n+i) % MOD * inv(i) % MOD. If we precompute inv[1..C] using formula inv[i] = MOD - MOD/i * inv[MOD%i] % MOD (since MOD is prime, that works for i < MOD). That's O(C). Then compute product. That might be more memory efficient? Actually we need array of inv up to C (200k). That's fine. But factorial approach is simpler.
Since we need factorial and inv_fact, we already compute up to L. It's okay.
Potential memory: fact array of 700001 long long: each 8 bytes = 5.6 MB, inv_fact same = 5.6 MB, total ~11.2 MB, fine.
But we could reduce memory by computing inv_fact only for needed indices? Not needed.
Alternatively, we can compute binom using:
binom = 1;
for (int i=1; i<=C; i++) {
binom = binom * (n + i) % MOD * inv(i) % MOD;
}
But we need inv(i). We can compute all inverses up to C using linear method: inv[1]=1; for i=2 to C: inv[i] = MOD - MOD/i * inv[MOD%i] % MOD. This works because MOD is prime. That's O(C). Then we just loop. That uses only O(C) memory and O(C) time, which is up to 200k, even faster. Also we need to compute product of (n+1)(n+2)...*(n+C) and divide by C!. But we can compute numerator as product (n+i) and multiply by inv(i) sequentially. That yields binom correctly.
But we must be careful that (n+i) might be 0 mod MOD? Since n+i < MOD (n+i <= 700k < 1e6+3), so none zero. So fine.
That method is efficient and simpler? Let's check: we need inv up to C (max 200k). That's fine. Then compute:
binom = 1;
for i in 1..C:
binom = binom * (n + i) % MOD * inv[i] % MOD;
But wait, this computes binom(n+C, C) because binom(n+C, C) = product_{i=1}^{C} (n+i)/i. Indeed.
So we can compute answer = (binom - 1 + MOD) % MOD.
This avoids computing factorials up to 700k (though 700k is not large). But either is fine.
But note that this product method might be more efficient? Both are O(L). But product method uses O(C) memory instead of O(L). But anyway.
We'll implement whichever.
Potential overflow: use long long.
Let's implement using factorial approach for clarity.
However, there is a nuance: The binom(n+C, C) formula holds for any n and C. But we derived sum_{k=1}^n binom(k+C-1, C-1) = binom(n+C, C). That is standard.
Thus answer.
Now, we should consider if there are any hidden constraints: n up to 500000, C up to 200000. So L up to 700000. That's fine.
But could there be multiple test cases? The input format shows only one line, so single case.
But I'll also consider that they might want the answer for up to n bricks inclusive, which we gave.
Now, final solution: compute binom(n+C, C) mod 1000003 and subtract 1.
Now, I'll produce final answer in C++.
I'll write:
code
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000003;
long long mod_pow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
int main() {
int n, C;
cin >> n >> C;
int L = n + C;
vector<long long> fact(L+1), inv_fact(L+1);
fact[0] = 1;
for (int i = 1; i <= L; i++) {
fact[i] = fact[i-1] * i % MOD;
}
inv_fact[L] = mod_pow(fact[L], MOD-2);
for (int i = L; i > 0; i--) {
inv_fact[i-1] = inv_fact[i] * i % MOD;
}
long long binom = fact[L] * inv_fact[n] % MOD * inv_fact[C] % MOD;
long long ans = (binom - 1 + MOD) % MOD;
cout << ans << endl;
return 0;
}
But we could also compute using product method:
code
int main() {
int n, C;
cin >> n >> C;
const int MOD = 1000003;
vector<long long> inv(C+1);
inv[1] = 1;
for (int i = 2; i <= C; i++) {
inv[i] = MOD - MOD/i * inv[MOD % i] % MOD;
}
long long binom = 1;
for (int i = 1; i <= C; i++) {
binom = binom * (n + i) % MOD * inv[i] % MOD;
}
long long ans = (binom - 1 + MOD) % MOD;
cout << ans << endl;
return 0;
}
That's even simpler and uses O(C) time, C <= 200k.
But careful: inv[i] formula: inv[i] = MOD - MOD/i * inv[MOD%i] % MOD; works for prime MOD.
We must ensure that i divides MOD? MOD is prime, so MOD/i is integer division? Actually we need to compute MOD - (MOD / i) * inv[MOD % i] % MOD. Where MOD/i is integer division (floor). For prime MOD, MOD % i is non-zero for i>1, but i might not divide MOD. The formula for modular inverse using dynamic programming: inv[i] = MOD - MOD/i * inv[MOD%i] % MOD; indeed works for prime MOD. So we can use that.
But we need to use long long to avoid overflow.
Alternatively, we could compute inv using pow for each i? That would be O(C log MOD) which is slower but still okay for 200k? 200k * log(1e6) ~ 200k * 20 = 4M operations, maybe okay but safer use linear.
Thus I'll implement linear inverse method.
Also note: n can be up to 500k, C up to 200k, so (n+i) up to 700k, still < MOD, no overflow if we multiply mod.
Now, verify with sample:
n=5, C=1: inv[1]=1; loop i=1: binom = 1 * (5+1) % MOD * 1 % MOD = 6; ans = 5. OK.
n=2, C=2: inv[1]=1; inv[2]=? MOD=1000003, MOD%2=1, MOD/2=500001, so inv[2] = MOD - 500001inv[1] % MOD = 1000003-500001=500002? Actually compute: 5000011=500001, MOD-500001=500002, mod MOD = 500002. That's the inverse of 2 mod 1000003 (since 2*500002=1000004 ≡1). Good.
Edge Cases: C=0? Not allowed. But if C=0, then width zero? Not allowed since C>=1.
Now, check for n large: For n=500000, C=200000, L=700000. In product method, we compute inv up to C=200000 and loop 200000 times, fine. In factorial method we'd compute up to 700000, also fine. But product method is slightly faster.
Thus I'll present solution with product method.
But careful: The product method requires computing inv[i] for i up to C. But the formula uses MOD%i. Since MOD is 1000003, which is prime, and i up to 200000, MOD%i is in range, fine.
Implementation details:
Use long long for all.
Initialize inv[1] = 1.
For i=2..C: inv[i] = MOD - MOD / i * inv[MOD % i] % MOD.
But careful: n+i may be up to 700000 < MOD, but multiplication may exceed 64-bit? In C++, long long can hold up to ~9e18, 700000*700000 ~ 5e11, safe. But accumulate multiplications mod after each step.
We need to use modulo after each multiplication to avoid overflow.
Now, verify with sample 5 quickly using code? Not needed.
Thus final solution.
Now, I'll write the final answer in C++.
Potential pitfalls: The input might be large, but cin is fine.
Now, produce final answer.
However, note: The problem might be to compute for up to n bricks, not exactly n bricks. But we already accounted.
Now, ensure that we output result modulo 1000003.
Now, for completeness, I'll include both possible methods.
Given that the editorial might expect the binomial formula.
Thus final answer.
Now, I will produce the final solution in code. ```cpp
#include
#include
using namespace std;
const int MOD = 1000003; // 10^6 + 3, prime
int main() {
int n, C;
cin >> n >> C;
code
// Precompute modular inverses of 1..C using linear method
vector<long long> inv(C + 1);
inv[1] = 1;
for (int i = 2; i <= C; ++i) {
inv[i] = MOD - (MOD / i) * inv[MOD % i] % MOD;
}
// Compute binom(n + C, C) = product_{i=1}^{C} (n + i) / i (mod MOD)
long long binom = 1;
for (int i = 1; i <= C; ++i) {
binom = binom * (n + i) % MOD;
binom = binom * inv[i] % MOD;
}
long long ans = (binom - 1 + MOD) % MOD;
cout << ans << endl;
return 0;