問題一覧 > 通常問題

No.461 How Many Triangles?

レベル : / AI推定難易度 : 1850 / 実行時間制限 : 1ケース 5.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 40
作問者 : りあん / テスター : kimiyuki
お気に入りにしたユーザー ProblemId : 1096 / 自分の提出
問題文最終更新日: 2026-06-25 11:47:58
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2016 (順位表) の他の問題:

Apology

Python3 got TLE with the intended solution.

Problem Statement

Inside an equilateral triangle, we add several line segments, each parallel to one of its three sides.

The $i$-th segment to be added is described using parameters $p_i, \ a_i, \ b_i$ as follows.

  • $p_i$ is $0$, $1$, or $2$, and these correspond to the vertices of the equilateral triangle in counterclockwise order.
  • Consider the points that internally divide each of the two sides extending from vertex $p_i$ in the ratio $a_i : b_i$, and call them $A$ and $B$, respectively.
  • Then the segment $AB$ is the $i$-th segment to be added.

(See also the samples.)

After all the segments have been added, output the number of equilateral triangles formed by the existing line segments.

Input

$N$
$p_1 \ a_1 \ b_1$
:
$p_N \ a_N \ b_N$

The first line contains the number $N$ of segments to add.

If $N$ is at least 1, the following $N$ lines, starting from the second line, contain the information $p_i, \ a_i, \ b_i$ of the segments to add, separated by spaces.

All input values are integers and satisfy the following constraints.

  • $0 \leq N \leq 4000$
  • $0 \leq p_i \leq 2$
  • $1 \leq a_i, b_i$
  • $a_i + b_i \leq 4 \times 10^5$
  • $(i \neq j)$ ならば, $(p_i \neq p_j)$ または $(a_i : b_i \neq a_j : b_j)$

Output

Output the number of equilateral triangles in the final figure.

Print a newline at the end.

Sample

Sample 1
Input
3
0 1 1
1 1 1
2 1 1
Output
5

The segments are added as shown below, and the final figure contains 5 equilateral triangles.

→ →

Sample 2
Input
4
2 1 1
1 3 1
2 9 3
0 6 2
Output
11

Note that $a_i$ and $b_i$ are not necessarily coprime.

Sample 3
Input
3
0 12 12
1 21 212
2 12 1212
Output
4

Sample 4
Input
0
Output
1

Note that there may be no segments drawn at all.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。