D. Relatively Prime Graph

Codeforces
IDCF1009D
Time2000ms
Memory256MB
Difficulty
brute forceconstructive algorithmsgraphsgreedymath
English · Original
Chinese · Translation
Formal · Original
Let's call an undirected graph $G = (V, E)$ _relatively prime_ if and only if for each edge $(v, u) \in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge between some pair of vertices $v$ and $u$ then the value of $GCD(v, u)$ doesn't matter. The vertices are numbered from $1$ to $|V|$. Construct a _relatively prime_ graph with $n$ vertices and $m$ edges such that it is connected and it contains neither self-loops nor multiple edges. If there exists no valid graph with the given number of vertices and edges then output _"Impossible"_. If there are multiple answers then print any of them. ## Input The only line contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$) — the number of vertices and the number of edges. ## Output If there exists no valid graph with the given number of vertices and edges then output _"Impossible"_. Otherwise print the answer in the following format: The first line should contain the word _"Possible"_. The $i$\-th of the next $m$ lines should contain the $i$\-th edge $(v_i, u_i)$ of the resulting graph ($1 \le v_i, u_i \le n, v_i \neq u_i$). For each pair $(v, u)$ there can be no more pairs $(v, u)$ or $(u, v)$. The vertices are numbered from $1$ to $n$. If there are multiple answers then print any of them. [samples] ## Note Here is the representation of the graph from the first example: ![image](https://espresso.codeforces.com/501a835f442401d928aaaee2ff94c95a4900a18e.png)
[{"iden":"statement","content":"我们称一个无向图 $G = (V, E)$ 为 _互质图_,当且仅当对于每条边 $(v, u) \\in E$,都有 $\\gcd(v, u) = 1$(即 $v$ 和 $u$ 的最大公约数为 1)。如果某对顶点 $v$ 和 $u$ 之间没有边,则 $\\gcd(v, u)$ 的值无关紧要。顶点编号从 $1$ 到 $|V|$。\n\n请构造一个包含 $n$ 个顶点和 $m$ 条边的 _互质图_,要求该图连通,且不包含自环或重边。\n\n如果不存在满足条件的图,则输出 _\"Impossible\"_。\n\n如果存在多个答案,输出任意一个即可。\n\n输入仅一行,包含两个整数 $n$ 和 $m$($1 \\leq n, m \\leq 10^5$)—— 分别表示顶点数和边数。\n\n如果不存在满足条件的图,则输出 _\"Impossible\"_。\n\n否则,请按以下格式输出答案:\n\n第一行输出单词 _\"Possible\"_。\n\n接下来的 $m$ 行中,第 $i$ 行输出第 $i$ 条边 $(v_i, u_i)$($1 \\leq v_i, u_i \\leq n$,且 $v_i \\neq u_i$)。对于每对 $(v, u)$,最多只能出现一次 $(v, u)$ 或 $(u, v)$。顶点编号从 $1$ 到 $n$。\n\n如果存在多个答案,输出任意一个即可。\n\n第一个示例的图表示如下:\n\n"},{"iden":"input","content":"输入仅一行,包含两个整数 $n$ 和 $m$($1 \\leq n, m \\leq 10^5$)—— 分别表示顶点数和边数。"},{"iden":"output","content":"如果不存在满足条件的图,则输出 _\"Impossible\"_。否则请按以下格式输出答案:第一行输出单词 _\"Possible\"_。接下来的 $m$ 行中,第 $i$ 行输出第 $i$ 条边 $(v_i, u_i)$($1 \\leq v_i, u_i \\leq n$,且 $v_i \\neq u_i$)。对于每对 $(v, u)$,最多只能出现一次 $(v, u)$ 或 $(u, v)$。顶点编号从 $1$ 到 $n$。如果存在多个答案,输出任意一个即可。"},{"iden":"examples","content":"输入\n5 6\n输出\nPossible\n2 5\n3 2\n5 1\n3 4\n4 1\n5 4\n\n输入\n6 12\n输出\nImpossible"},{"iden":"note","content":"第一个示例的图表示如下: "}] ```json [{"iden":"statement","content":"我们称一个无向图 $G = (V, E)$ 为 _互质图_,当且仅当对于每条边 $(v, u) \\in E$,都有 $\\gcd(v, u) = 1$(即 $v$ 和 $u$ 的最大公约数为 1)。如果某对顶点 $v$ 和 $u$ 之间没有边,则 $\\gcd(v, u)$ 的值无关紧要。顶点编号从 $1$ 到 $|V|$。\n\n请构造一个包含 $n$ 个顶点和 $m$ 条边的 _互质图_,要求该图连通,且不包含自环或重边。\n\n如果不存在满足条件的图,则输出 _\"Impossible\"_。\n\n如果存在多个答案,输出任意一个即可。\n\n输入仅一行,包含两个整数 $n$ 和 $m$($1 \\leq n, m \\leq 10^5$)—— 分别表示顶点数和边数。\n\n如果不存在满足条件的图,则输出 _\"Impossible\"_。\n\n否则,请按以下格式输出答案:\n\n第一行输出单词 _\"Possible\"_。\n\n接下来的 $m$ 行中,第 $i$ 行输出第 $i$ 条边 $(v_i, u_i)$($1 \\leq v_i, u_i \\leq n$,且 $v_i \\neq u_i$)。对于每对 $(v, u)$,最多只能出现一次 $(v, u)$ 或 $(u, v)$。顶点编号从 $1$ 到 $n$。\n\n如果存在多个答案,输出任意一个即可。\n\n第一个示例的图表示如下:\n\n"},{"iden":"input","content":"输入仅一行,包含两个整数 $n$ 和 $m$($1 \\leq n, m \\leq 10^5$)—— 分别表示顶点数和边数。"},{"iden":"output","content":"如果不存在满足条件的图,则输出 _\"Impossible\"_。否则请按以下格式输出答案:第一行输出单词 _\"Possible\"_。接下来的 $m$ 行中,第 $i$ 行输出第 $i$ 条边 $(v_i, u_i)$($1 \\leq v_i, u_i \\leq n$,且 $v_i \\neq u_i$)。对于每对 $(v, u)$,最多只能出现一次 $(v, u)$ 或 $(u, v)$。顶点编号从 $1$ 到 $n$。如果存在多个答案,输出任意一个即可。"},{"iden":"examples","content":"输入\n5 6\n输出\nPossible\n2 5\n3 2\n5 1\n3 4\n4 1\n5 4\n\n输入\n6 12\n输出\nImpossible"},{"iden":"note","content":"第一个示例的图表示如下: "}] ```
**Definitions** Let $ n, m \in \mathbb{Z}^+ $ with $ 1 \leq n, m \leq 10^5 $. Let $ V = \{1, 2, \dots, n\} $ be the set of vertices. Let $ E \subseteq \binom{V}{2} $ be a set of undirected edges (no self-loops, no multiple edges). A graph $ G = (V, E) $ is *relatively prime* if for every edge $ (u, v) \in E $, $ \gcd(u, v) = 1 $. The graph is *connected* if there exists a path between every pair of vertices in $ V $. **Constraints** 1. $ |E| = m $ 2. $ G = (V, E) $ is connected 3. For all $ (u, v) \in E $, $ \gcd(u, v) = 1 $ 4. $ u \ne v $ for all $ (u, v) \in E $ **Objective** Determine whether there exists a set $ E $ satisfying the above constraints. - If yes, output "Possible" followed by $ m $ distinct edges $ (u_i, v_i) \in E $. - If no, output "Impossible".
Samples
Input #1
5 6
Output #1
Possible
2 5
3 2
5 1
3 4
4 1
5 4
Input #2
6 12
Output #2
Impossible
API Response (JSON)
{
  "problem": {
    "name": "D. Relatively Prime Graph",
    "description": {
      "content": "Let's call an undirected graph $G = (V, E)$ _relatively prime_ if and only if for each edge $(v, u) \\in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge betw",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF1009D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Let's call an undirected graph $G = (V, E)$ _relatively prime_ if and only if for each edge $(v, u) \\in E$ $GCD(v, u) = 1$ (the greatest common divisor of $v$ and $u$ is $1$). If there is no edge betw...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "[{\"iden\":\"statement\",\"content\":\"我们称一个无向图 $G = (V, E)$ 为 _互质图_,当且仅当对于每条边 $(v, u) \\\\in E$,都有 $\\\\gcd(v, u) = 1$(即 $v$ 和 $u$ 的最大公约数为 1)。如果某对顶点 $v$ 和 $u$ 之间没有边,则 $\\\\gcd(v, u)$ 的值无关紧要。顶点编号从 $1$ 到 $|V|$。\\n\\n...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ with $ 1 \\leq n, m \\leq 10^5 $.  \nLet $ V = \\{1, 2, \\dots, n\\} $ be the set of vertices.  \nLet $ E \\subseteq \\binom{V}{2} $ be a set of undirected edges...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments