CF1731E.Graph Cost

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an initially empty undirected graph with nn nodes, numbered from 11 to nn (i. e. nn nodes and 00 edges). You want to add mm edges to the graph, so the graph won't contain any self-loop or multiple edges.

If an edge connecting two nodes uu and vv is added, its weight must be equal to the greatest common divisor of uu and vv, i. e. gcd(u,v)\gcd(u, v).

In order to add edges to the graph, you can repeat the following process any number of times (possibly zero):

  • choose an integer k1k \ge 1;
  • add exactly kk edges to the graph, each having a weight equal to k+1k + 1. Adding these kk edges costs k+1k + 1 in total.

Note that you can't create self-loops or multiple edges. Also, if you can't add kk edges of weight k+1k + 1, you can't choose such kk.

For example, if you can add 55 more edges to the graph of weight 66, you may add them, and it will cost 66 for the whole pack of 55 edges. But if you can only add 44 edges of weight 66 to the graph, you can't perform this operation for k=5k = 5.

Given two integers nn and mm, find the minimum total cost to form a graph of nn vertices and exactly mm edges using the operation above. If such a graph can't be constructed, output 1-1.

Note that the final graph may consist of several connected components.

你被给定一个初始为空的无向图,该图包含 nn 个节点,编号从 11nn(即 nn 个节点,00 条边)。你需要向图中添加 mm 条边,使得图中不包含任何自环或重边。

若添加一条连接节点 uuvv 的边,则其权重必须等于 uuvv 的最大公约数,即 gcd(u,v)\gcd(u, v)

为了向图中添加边,你可以重复执行以下操作任意次数(包括零次):

  • 选择一个整数 k1k \ge 1
  • 向图中恰好添加 kk 条边,每条边的权重均为 k+1k + 1。执行该操作的总代价为 k+1k + 1

注意:你不允许创建自环或重边。此外,若无法添加 kk 条权重为 k+1k + 1 的边,则不允许选择该 kk

例如,若图中尚可添加 55 条权重为 66 的边,则你可以执行该操作(即选择 k=5k = 5),并为此付出总代价 66。但若图中最多只能添加 44 条权重为 66 的边,则你不能为 k=5k = 5 执行该操作。

给定两个整数 nnmm,求使用上述操作构造一个含 nn 个顶点、恰好 mm 条边的图所需的最小总代价。若无法构造这样的图,则输出 1-1

注意:最终图可能由若干个连通分量组成。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \leq t \leq 10^4). Description of the test cases follows.

The first line of each test case contains two integers nn and mm (2n1062 \leq n \leq 10^6; 1mn(n1)21 \leq m \leq \frac{n(n-1)}{2}).

It is guaranteed that the sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt1t1041 \leq t \leq 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nnmm2n1062 \leq n \leq 10^61mn(n1)21 \leq m \leq \frac{n(n-1)}{2})。

保证所有测试用例的 nn 值之和不超过 10610^6

输出格式

For each test case, print the minimum cost to build the graph, or 1-1 if you can't build such a graph.

对于每个测试用例,输出构建该图的最小代价;如果无法构建这样的图,则输出 1-1

输入输出样例

  • 输入#1

    4
    4 1
    6 10
    9 4
    10 11

    输出#1

    2
    -1
    7
    21

说明/提示

In the first test case, we can add an edge between the vertices 22 and 44 with gcd=2\gcd = 2. This is the only possible way to add 11 edge that will cost 22.

In the second test case, there is no way to add 1010 edges, so the answer is 1-1.

In the third test case, we can add the following edges:

  • k=1k = 1: edge of weight 22 between vertices 22 and 44 (gcd(2,4)=2\gcd(2, 4) = 2). Cost: 22;
  • k=1k = 1: edge of weight 22 between vertices 44 and 66 (gcd(4,6)=2\gcd(4, 6) = 2). Cost: 22;
  • k=2k = 2: edges of weight 33: (3,6)(3, 6) and (3,9)(3, 9) (gcd(3,6)=gcd(3,9)=3\gcd(3, 6) = \gcd(3, 9) = 3). Cost: 33.

As a result, we added 1+1+2=41 + 1 + 2 = 4 edges with total cost 2+2+3=72 + 2 + 3 = 7, which is the minimal possible cost.

在第一个测试用例中,我们可以在顶点 2244 之间添加一条边,其权值为 gcd=2\gcd = 2。这是唯一一种添加 11 条边且总代价为 22 的方案。

在第二个测试用例中,不存在添加 1010 条边的方法,因此答案为 1-1

在第三个测试用例中,我们可以添加如下边:

  • k=1k = 1:在顶点 2244 之间添加一条权值为 22 的边(gcd(2,4)=2\gcd(2, 4) = 2),代价为 22
  • k=1k = 1:在顶点 4466 之间添加一条权值为 22 的边(gcd(4,6)=2\gcd(4, 6) = 2),代价为 22
  • k=2k = 2:添加两条权值为 33 的边:(3,6)(3, 6)(3,9)(3, 9)gcd(3,6)=gcd(3,9)=3\gcd(3, 6) = \gcd(3, 9) = 3),代价为 33

最终,我们共添加了 1+1+2=41 + 1 + 2 = 4 条边,总代价为 2+2+3=72 + 2 + 3 = 7,这是可能的最小总代价。

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

首页