CF1178D.Prime Graph

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but couldn't think of anything inventive. Hence, he will be giving her a graph. How original, Bob! Alice will surely be thrilled!

When building the graph, he needs four conditions to be satisfied:

  • It must be a simple undirected graph, i.e. without multiple (parallel) edges and self-loops.
  • The number of vertices must be exactly nn — a number he selected. This number is not necessarily prime.
  • The total number of edges must be prime.
  • The degree (i.e. the number of edges connected to the vertex) of each vertex must be prime.

Below is an example for n=4n = 4. The first graph (left one) is invalid as the degree of vertex 22 (and 44) equals to 11, which is not prime. The second graph (middle one) is invalid as the total number of edges is 44, which is not a prime number. The third graph (right one) is a valid answer for n=4n = 4.

Note that the graph can be disconnected.

Please help Bob to find any such graph!

每个人都喜欢质数。Alice 是一个人,因此她也同样热爱质数。Bob 想送她一份充满爱意的礼物,却想不出什么有创意的点子。于是,他决定送她一张图。多么别出心裁啊,Bob!Alice 一定会欣喜若狂!

在构造这张图时,他需满足以下四个条件:

  • 它必须是一张简单无向图,即不含多重(平行)边和自环;
  • 顶点数必须恰好为 nn —— 这是他选定的一个数。该数本身不一定是质数;
  • 边的总数必须是质数;
  • 每个顶点的度数(即与该顶点相连的边的数量)必须是质数。

下图是一个 n=4n = 4 的示例:第一张图(左侧)无效,因为顶点 22(以及顶点 44)的度数为 11,而 11 不是质数;第二张图(中间)无效,因为总边数为 44,而 44 不是质数;第三张图(右侧)是 n=4n = 4 的一个合法解。

注意:该图可以是非连通的。

请帮 Bob 构造出任意一张满足上述条件的图!

输入格式

The input consists of a single integer nn (3≤n≤1 0003 \leq n \leq 1\,000) — the number of vertices.

输入包含一个整数 nn(3≤n≤1 0003 \leq n \leq 1\,000)——顶点的数量。

输出格式

If there is no graph satisfying the conditions, print a single line containing the integer −1-1.

Otherwise, first print a line containing a prime number mm (2≤m≤n(n−1)22 \leq m \leq \frac{n(n-1)}{2}) — the number of edges in the graph. Then, print mm lines, the ii-th of which containing two integers uiu_i, viv_i (1≤ui,vi≤n1 \leq u_i, v_i \leq n) — meaning that there is an edge between vertices uiu_i and viv_i. The degree of each vertex must be prime. There must be no multiple (parallel) edges or self-loops.

If there are multiple solutions, you may print any of them.

Note that the graph can be disconnected.

如果不存在满足条件的图,则输出一行,包含整数 −1-1。

否则,首先输出一行,包含一个质数 mm(2≤m≤n(n−1)22 \leq m \leq \frac{n(n-1)}{2})——表示图中的边数。然后输出 mm 行,其中第 ii 行包含两个整数 uiu_i、viv_i(1≤ui,vi≤n1 \leq u_i, v_i \leq n),表示顶点 uiu_i 与 viv_i 之间存在一条边。每个顶点的度数必须为质数。图中不能存在重边(平行边)或自环。

若存在多个解,可输出任意一个。

注意:该图可以是非连通的。

输入输出样例

  • 输入#1

    4

    输出#1

    5
    1 2
    1 3
    2 3
    2 4
    3 4
  • 输入#2

    8

    输出#2

    13
    1 2
    1 3
    2 3
    1 4
    2 4
    1 5
    2 5
    1 6
    2 6
    1 7
    1 8
    5 8
    7 8

说明/提示

The first example was described in the statement.

In the second example, the degrees of vertices are [7,5,2,2,3,2,2,3][7, 5, 2, 2, 3, 2, 2, 3]. Each of these numbers is prime. Additionally, the number of edges, 1313, is also a prime number, hence both conditions are satisfied.

第一个示例已在题目描述中给出。

在第二个示例中,各顶点的度数为 [7,5,2,2,3,2,2,3][7, 5, 2, 2, 3, 2, 2, 3]。其中每个数均为素数。此外,边的数量 1313 也是一个素数,因此两个条件均满足。

输入解题思路,AI测评打分。不知道怎么写?

首页