TopcoderArchive
TCO13 Dec 2018
TCO19 SRM 744

ModularQuadrant

Problem statement, definition, constraints, and public examples.

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

  1. 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 1201
  2. 2
    2
    0
    7
    Returns: 10
    Now we are computing the sum of a single row that looks as follows: 22201201
  3. 4
    8
    0
    5
    Returns: 37
    222222 111111 000000 222222 111112
Back to all problems