Count the Cows#11215

Count the Cows

1 Sekunde
128 Megabyte
Sehr schwer

Farmer John's cows have spread themselves out in his largest pasture, which can be represented as a large 2D grid of square cells (imagine a huge chessboard).

The pattern of cows across the pasture is quite intriguing. For every cell with x \ge 0 and y \ge 0, there is a cow at if, for all integers k \ge 0, the parity of the remainders when \left\lfloor \frac{x}{3^k} \right\rfloor and \left\lfloor \frac{y}{3^k} \right\rfloor are divided by is the same. In other words: both remainders are odd (equal to ), or both are even (equal to or ). For example, the cells among 0 \le x, y < 9 that contain cows are marked with ones in the diagram below:

        x
    012345678

  0 101000101
  1 010000010
  2 101000101
  3 000101000
y 4 000010000
  5 000101000
  6 101000101
  7 010000010
  8 101000101

Farmer John is curious how many cows are present in certain diagonal regions of his pasture. He asks queries, each consisting of three integers x_i, y_i, d_i. For each query, Farmer John wants to know how many cows are located at the cells along the diagonal from (x_i, y_i) to (x_i + d_i, y_i + d_i) (endpoints inclusive).

Eingabe

The first line contains an integer (1 \le q \le 10^4), the number of queries.

The next lines each contain three integers d_i, x_i, y_i (0 \le x_i, y_i, d_i \le 10^{18}), describing a query.

Ausgabe

Print lines, one for each query. Each line should contain a single integer — the answer to the corresponding query.

Beispiele

Beispiel 1

Loading
Loading