D. Symmetric Projections

Codeforces
IDCF889D
Time2000ms
Memory256MB
Difficulty
geometry
English · Original
Chinese · Translation
Formal · Original
You are given a set of _n_ points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of good lines. Multiset is a set where equal elements are allowed. Multiset is called symmetric, if there is a point _P_ on the plane such that the multiset is [centrally symmetric](https://en.wikipedia.org/wiki/Point_reflection) in respect of point _P_. ## Input The first line contains a single integer _n_ (1 ≤ _n_ ≤ 2000) — the number of points in the set. Each of the next _n_ lines contains two integers _x__i_ and _y__i_ ( - 106  ≤  _x__i_,  _y__i_  ≤  106) — the coordinates of the points. It is guaranteed that no two points coincide. ## Output If there are infinitely many good lines, print _\-1_. Otherwise, print single integer — the number of good lines. [samples] ## Note Picture to the first sample test: ![image](https://espresso.codeforces.com/eedc60313be8684bd6169b8b23f0f0afd92479a8.png) In the second sample, any line containing the origin is good.
给定平面上的一组 #cf_span[n] 个点。一条经过原点的直线被称为“好线”,当且仅当该点集在该直线上的投影形成一个对称的多重集。 多重集是指允许重复元素的集合。 一个多重集被称为对称的,如果存在平面上的一个点 #cf_span[P],使得该多重集关于点 #cf_span[P] 中心对称。 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 2000]) —— 点集中的点数。 接下来的 #cf_span[n] 行,每行包含两个整数 #cf_span[xi] 和 #cf_span[yi] (#cf_span[ - 106  ≤  xi,  yi  ≤  106]) —— 点的坐标。保证没有两个点重合。 如果存在无穷多条好线,请输出 _-1_。 否则,输出一个整数 —— 好线的总数。 第一个样例的图示: 在第二个样例中,任何经过原点的直线都是好线。 ## Input 第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 2000]) —— 点集中的点数。接下来的 #cf_span[n] 行,每行包含两个整数 #cf_span[xi] 和 #cf_span[yi] (#cf_span[ - 106  ≤  xi,  yi  ≤  106]) —— 点的坐标。保证没有两个点重合。 ## Output 如果存在无穷多条好线,请输出 _-1_。否则,输出一个整数 —— 好线的总数。 [samples] ## Note 第一个样例的图示:在第二个样例中,任何经过原点的直线都是好线。
Let $ S = \{ \mathbf{p}_1, \mathbf{p}_2, \dots, \mathbf{p}_n \} \subset \mathbb{R}^2 $ be a set of $ n $ distinct points in the plane, with $ \mathbf{p}_i = (x_i, y_i) $. For a line $ \ell $ passing through the origin, let $ \pi_\ell : \mathbb{R}^2 \to \ell $ denote the orthogonal projection onto $ \ell $. Define the projected multiset: $$ P_\ell = \{ \pi_\ell(\mathbf{p}_i) \mid \mathbf{p}_i \in S \}. $$ A line $ \ell $ is **good** if $ P_\ell $ is **centrally symmetric** about some point $ \mathbf{P} \in \ell $, i.e., for every point $ \mathbf{q} \in P_\ell $, the point $ 2\mathbf{P} - \mathbf{q} $ is also in $ P_\ell $ with the same multiplicity. Let $ \theta \in [0, \pi) $ parametrize the direction of $ \ell $, so that $ \ell = \{ t (\cos\theta, \sin\theta) \mid t \in \mathbb{R} \} $. We are to compute the number of distinct directions $ \theta \in [0, \pi) $ for which $ P_\ell $ is centrally symmetric. **Key observations:** 1. The projection $ \pi_\ell(\mathbf{p}_i) $ is the scalar projection along the unit vector $ \mathbf{u}_\theta = (\cos\theta, \sin\theta) $: $$ \pi_\ell(\mathbf{p}_i) = (\mathbf{p}_i \cdot \mathbf{u}_\theta) \cdot \mathbf{u}_\theta. $$ The **scalar coordinate** of the projection along $ \ell $ is $ s_i = \mathbf{p}_i \cdot \mathbf{u}_\theta = x_i \cos\theta + y_i \sin\theta $. 2. The multiset $ P_\ell $ is centrally symmetric about some point $ \mathbf{P} \in \ell $ **if and only if** the multiset of scalar projections $ \{ s_1, s_2, \dots, s_n \} $ is centrally symmetric about some $ c \in \mathbb{R} $, i.e., for every $ s_i $, $ 2c - s_i $ is also in the multiset with the same multiplicity. 3. A multiset of real numbers is centrally symmetric about $ c $ if and only if: $$ \{ s_1, \dots, s_n \} = \{ 2c - s_1, \dots, 2c - s_n \}. $$ This implies that the multiset is symmetric about its **mean** (if $ n $ is odd, the center must be the median, which must equal the mean). Thus, the only possible center is $ c = \frac{1}{n} \sum_{i=1}^n s_i $, and the condition becomes: $$ \forall i, \exists j \text{ such that } s_j = 2c - s_i. $$ 4. Since $ c = \frac{1}{n} \sum_{i=1}^n \mathbf{p}_i \cdot \mathbf{u}_\theta = \left( \frac{1}{n} \sum_{i=1}^n \mathbf{p}_i \right) \cdot \mathbf{u}_\theta = \bar{\mathbf{p}} \cdot \mathbf{u}_\theta $, where $ \bar{\mathbf{p}} = \frac{1}{n} \sum \mathbf{p}_i $, the condition becomes: $$ \forall i, \exists j \text{ such that } \mathbf{p}_i \cdot \mathbf{u}_\theta + \mathbf{p}_j \cdot \mathbf{u}_\theta = 2 \bar{\mathbf{p}} \cdot \mathbf{u}_\theta, $$ or equivalently, $$ (\mathbf{p}_i + \mathbf{p}_j - 2\bar{\mathbf{p}}) \cdot \mathbf{u}_\theta = 0. $$ 5. Define the **centered points**: $ \mathbf{q}_i = \mathbf{p}_i - \bar{\mathbf{p}} $. Then the condition becomes: $$ \forall i, \exists j \text{ such that } (\mathbf{q}_i + \mathbf{q}_j) \cdot \mathbf{u}_\theta = 0. $$ That is, $ \mathbf{q}_i + \mathbf{q}_j \perp \mathbf{u}_\theta $. 6. For the multiset of projections to be symmetric, the set $ \{ \mathbf{q}_1, \dots, \mathbf{q}_n \} $ must be **centrally symmetric about the origin** in the plane — **but only along the direction $ \mathbf{u}_\theta $**. That is, for the projection to be symmetric, the set $ \{ \mathbf{q}_i \} $ must be symmetric under reflection through the origin **in the direction of $ \ell $**. However, a necessary and sufficient condition is: > The multiset $ \{ \mathbf{p}_i \} $ is such that the set of vectors $ \{ \mathbf{p}_i - \bar{\mathbf{p}} \} $ is centrally symmetric **as a whole** — then **every** line through the origin is good. Otherwise, only lines $ \ell $ for which the projection of the centered set is symmetric (i.e., the direction $ \mathbf{u}_\theta $ is orthogonal to some $ \mathbf{q}_i + \mathbf{q}_j $) are candidates. 7. **Critical insight**: The set of projections $ \{ s_i \} $ is centrally symmetric **if and only if** the multiset $ \{ \mathbf{q}_i \} $ is centrally symmetric **in the direction of $ \ell $**. That is, for every $ \mathbf{q}_i $, there exists $ \mathbf{q}_j $ such that $ \mathbf{q}_i + \mathbf{q}_j \perp \mathbf{u}_\theta $. But this must hold for **all** $ i $. So, the set $ \{ \mathbf{q}_i \} $ must be centrally symmetric **as a whole** (i.e., $ \{ \mathbf{q}_i \} = \{ -\mathbf{q}_i \} $), or else only specific directions $ \mathbf{u}_\theta $ satisfy the condition. 8. **Final characterization**: - If the entire set $ S $ is centrally symmetric about $ \bar{\mathbf{p}} $, i.e., $ \{ \mathbf{p}_i \} = \{ 2\bar{\mathbf{p}} - \mathbf{p}_i \} $, then **every** line through the origin is good → output $ -1 $. - Otherwise, the good lines correspond to directions $ \mathbf{u}_\theta $ such that $ \mathbf{u}_\theta \perp (\mathbf{q}_i + \mathbf{q}_j) $ for some pair $ (i,j) $, and the multiset of projections is symmetric. But since the set is not centrally symmetric globally, the only possible good lines are those perpendicular to $ \mathbf{q}_i + \mathbf{q}_j $ for some pair $ i < j $, **and** for which the projection symmetry condition holds. However, a simpler and known approach from competitive programming problems (e.g., CodeForces) is: > The number of good lines equals the number of distinct directions $ \theta \in [0, \pi) $ such that the multiset $ \{ \mathbf{p}_i \cdot \mathbf{u}_\theta \} $ is symmetric about its mean. This happens if and only if the multiset $ \{ \mathbf{q}_i \} $ is symmetric under reflection through the origin **along $ \ell $**, i.e., the set of vectors $ \{ \mathbf{q}_i \} $ is symmetric under the operation of negating components **along $ \ell $**. The only directions $ \ell $ that can work are those for which the set $ \{ \mathbf{q}_i \} $ is symmetric with respect to the line perpendicular to $ \ell $ — i.e., $ \ell $ is an axis of symmetry of the multiset $ \{ \mathbf{q}_i \} $ **through the origin**. But since we are projecting onto $ \ell $, and requiring the **projection** to be centrally symmetric, the correct known solution is: > Good lines correspond to directions $ \mathbf{u} $ such that the multiset $ \{ \mathbf{q}_i \} $ is symmetric under the transformation $ \mathbf{v} \mapsto -\mathbf{v} $ **when projected onto $ \mathbf{u}^\perp $** — i.e., $ \mathbf{u} $ is perpendicular to a vector $ \mathbf{q}_i + \mathbf{q}_j $ for some pair $ i, j $, and the pairing covers all points. Actually, the known solution (from CodeForces problems like "Good Line") is: - Compute $ \bar{\mathbf{p}} = \frac{1}{n} \sum \mathbf{p}_i $. - Compute $ \mathbf{q}_i = \mathbf{p}_i - \bar{\mathbf{p}} $. - If all $ \mathbf{q}_i = \mathbf{0} $, then all points coincide — contradiction (guaranteed distinct), so skip. - If $ \{ \mathbf{q}_i \} = \{ -\mathbf{q}_i \} $ as multisets → then every line is good → output $ -1 $. - Otherwise, for each unordered pair $ (i, j) $ with $ i < j $, compute $ \mathbf{v}_{ij} = \mathbf{q}_i + \mathbf{q}_j $. If $ \mathbf{v}_{ij} \ne \mathbf{0} $, then the direction $ \ell $ perpendicular to $ \mathbf{v}_{ij} $ is a candidate. - For each such candidate direction (i.e., normalized vector $ \mathbf{u} \perp \mathbf{v}_{ij} $), check whether the projection of $ \{ \mathbf{q}_i \} $ onto $ \ell $ is centrally symmetric (i.e., the multiset of dot products $ \{ \mathbf{q}_i \cdot \mathbf{u} \} $ is symmetric about 0). - Count distinct such directions $ \mathbf{u} \in [0, \pi) $. But note: since $ \mathbf{u} \perp \mathbf{v}_{ij} $, then $ \mathbf{u} \cdot (\mathbf{q}_i + \mathbf{q}_j) = 0 \Rightarrow \mathbf{q}_i \cdot \mathbf{u} = - \mathbf{q}_j \cdot \mathbf{u} $, so at least this pair is symmetric. But we need **all** points to pair up. So the algorithm: - If the multiset $ \{ \mathbf{q}_i \} $ is centrally symmetric about origin → output $ -1 $. - Else: - Initialize a set $ D $ of directions. - For each pair $ (i, j) $, $ i < j $: - Let $ \mathbf{v} = \mathbf{q}_i + \mathbf{q}_j $. - If $ \mathbf{v} \ne \mathbf{0} $, compute a unit vector $ \mathbf{u} $ perpendicular to $ \mathbf{v} $, normalized to direction in $ [0, \pi) $. - Check if the multiset $ \{ \mathbf{q}_k \cdot \mathbf{u} \} $ is symmetric about 0. - If yes, add $ \mathbf{u} $ to $ D $ (as a normalized direction, avoiding duplicates). - Output $ |D| $. However, note that if $ \mathbf{v} = \mathbf{0} $, then $ \mathbf{q}_j = -\mathbf{q}_i $, so this pair is already symmetric — but we still need to check global symmetry. So the **formal statement** is: --- **Definitions:** - Let $ \mathbf{p}_1, \dots, \mathbf{p}_n \in \mathbb{R}^2 $ be distinct points. - Let $ \bar{\mathbf{p}} = \frac{1}{n} \sum_{i=1}^n \mathbf{p}_i $. - Let $ \mathbf{q}_i = \mathbf{p}_i - \bar{\mathbf{p}} $ for $ i = 1, \dots, n $. **Given:** - $ \mathbf{q}_i \ne \mathbf{0} $ for at least one $ i $ (since points are distinct). - The multiset $ \{ \mathbf{q}_1, \dots, \mathbf{q}_n \} $ is **not necessarily** centrally symmetric about $ \mathbf{0} $. **Objective:** Find the number of distinct unit vectors $ \mathbf{u} \in \mathbb{R}^2 $ with $ \|\mathbf{u}\| = 1 $, and $ \mathbf{u} $ representing a direction in $ [0, \pi) $, such that the multiset $ \{ \mathbf{q}_i \cdot \mathbf{u} \mid i = 1, \dots, n \} $ is symmetric about 0. That is: $$ \{ \mathbf{q}_1 \cdot \mathbf{u}, \dots, \mathbf{q}_n \cdot \mathbf{u} \} = \{ -(\mathbf{q}_1 \cdot \mathbf{u}), \dots, -(\mathbf{q}_n \cdot \mathbf{u}) \} $$ as multisets. **Equivalently:** The multiset $ \{ \mathbf{q}_i \cdot \mathbf{u} \} $ is symmetric about 0 if and only if for every $ i $, there exists $ j $ such that $ \mathbf{q}_i \cdot \mathbf{u} = - \mathbf{q}_j \cdot \mathbf{u} $, and multiplicities match. **Constraint:** If $ \{ \mathbf{q}_i \} = \{ -\mathbf{q}_i \} $ as multisets, then **every** $ \mathbf{u} $ satisfies the condition → output $ -1 $. Otherwise, count the number of distinct directions $ \mathbf{u} \in S^1 / \sim $ (where $ \mathbf{u} \sim -\mathbf{u} $) for which $ \{ \mathbf{q}_i \cdot \mathbf{u} \} $ is symmetric about 0. --- **Final Formal Statement:** Let $ \mathbf{q}_i = \mathbf{p}_i - \frac{1}{n} \sum_{j=1}^n \mathbf{p}_j $ for $ i = 1, \dots, n $. - If $ \{ \mathbf{q}_1, \dots, \mathbf{q}_n \} = \{ -\mathbf{q}_1, \dots, -\mathbf{q}_n \} $ as multisets, output $ -1 $. - Else, let $ \mathcal{U} = \left\{ \mathbf{u} \in \mathbb{R}^2 \mid \|\mathbf{u}\| = 1, \{ \mathbf{q}_i \cdot \mathbf{u} \}_{i=1}^n \text{ is symmetric about } 0 \right\} $. - Output $ \left| \left\{ \mathbf{u} \in \mathcal{U} \mid \mathbf{u} \in [0, \pi) \text{ direction} \right\} \right| $. Where "direction in $ [0, \pi) $" means we identify $ \mathbf{u} $ and $ -\mathbf{u} $, and represent each line by its unique unit vector with angle $ \theta \in [0, \pi) $. ---
Samples
Input #1
3
1 2
2 1
3 3
Output #1
3
Input #2
2
4 3
1 2
Output #2
\-1
API Response (JSON)
{
  "problem": {
    "name": "D. Symmetric Projections",
    "description": {
      "content": "You are given a set of _n_ points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF889D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a set of _n_ points on the plane. A line containing the origin is called good, if projection of the given set to this line forms a symmetric multiset of points. Find the total number of ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "给定平面上的一组 #cf_span[n] 个点。一条经过原点的直线被称为“好线”,当且仅当该点集在该直线上的投影形成一个对称的多重集。\n\n多重集是指允许重复元素的集合。\n\n一个多重集被称为对称的,如果存在平面上的一个点 #cf_span[P],使得该多重集关于点 #cf_span[P] 中心对称。\n\n第一行包含一个整数 #cf_span[n] (#cf_span[1 ≤ n ≤ 2000]) ——...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ S = \\{ \\mathbf{p}_1, \\mathbf{p}_2, \\dots, \\mathbf{p}_n \\} \\subset \\mathbb{R}^2 $ be a set of $ n $ distinct points in the plane, with $ \\mathbf{p}_i = (x_i, y_i) $.\n\nFor a line $ \\ell $ passing ...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments