Problem Statement
We are looking at the first quadrant of an infinite square grid. Its rows and columns are numbered starting from zero. Each cell (r,c) contains the number (max(r,c) mod 3).
Compute and return the sum of all cells (r,c) such that r1 <= r <= r2 and c1 <= c <= c2.
Definition
- Class:
- ModularQuadrant
- Method:
- sum
- Parameters:
- int, int, int, int
- Returns:
- long
- Method signature:
- long sum(int r1, int r2, int c1, int c2)
- (be sure your method is public)
Constraints
- 0 <= r1 <= r2 <= 10^9.
- 0 <= c1 <= c2 <= 10^9.
Examples
0
2
1
4
Returns: 13
We want rows 0 through 2 and columns 1 through 4. The corresponding part of the grid looks as follows: 2201 1201 12012
2
0
7
Returns: 10
Now we are computing the sum of a single row that looks as follows: 222012014
8
0
5
Returns: 37
222222 111111 000000 222222 111112