CF1798C.Candy Store

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

The store sells nn types of candies with numbers from 11 to nn. One candy of type ii costs bib_i coins. In total, there are aia_i candies of type ii in the store.

You need to pack all available candies in packs, each pack should contain only one type of candies. Formally, for each type of candy ii you need to choose the integer did_i, denoting the number of type ii candies in one pack, so that aia_i is divided without remainder by did_i.

Then the cost of one pack of candies of type ii will be equal to bi⋅dib_i \cdot d_i. Let's denote this cost by cic_i, that is, ci=bi⋅dic_i = b_i \cdot d_i.

After packaging, packs will be placed on the shelf. Consider the cost of the packs placed on the shelf, in order c1,c2,…,cnc_1, c_2, \ldots, c_n. Price tags will be used to describe costs of the packs. One price tag can describe the cost of all packs from ll to rr inclusive if cl=cl+1=…=crc_l = c_{l+1} = \ldots = c_r. Each of the packs from 11 to nn must be described by at least one price tag. For example, if c1,…,cn=[4,4,2,4,4]c_1, \ldots, c_n = [4, 4, 2, 4, 4], to describe all the packs, a 33 price tags will be enough, the first price tag describes the packs 1,21, 2, the second: 33, the third: 4,54, 5.

You are given the integers a1,b1,a2,b2,…,an,bna_1, b_1, a_2, b_2, \ldots, a_n, b_n. Your task is to choose integers did_i so that aia_i is divisible by did_i for all ii, and the required number of price tags to describe the values of c1,c2,…,cnc_1, c_2, \ldots, c_n is the minimum possible.

For a better understanding of the statement, look at the illustration of the first test case of the first test:

Let's repeat the meaning of the notation used in the problem:

aia_i — the number of candies of type ii available in the store.

bib_i — the cost of one candy of type ii.

did_i — the number of candies of type ii in one pack.

cic_i — the cost of one pack of candies of type ii is expressed by the formula ci=bi⋅dic_i = b_i \cdot d_i.

商店出售 nn 种糖果,编号从 11 到 nn。第 ii 种糖果的单价为 bib_i 枚金币。商店中第 ii 种糖果共有 aia_i 颗。

你需要将所有现有糖果装入若干包装盒中,每个包装盒仅包含同一种类的糖果。形式化地,对每种糖果 ii,你需要选定一个整数 did_i,表示每个包装盒中第 ii 种糖果的数量,且需满足 did_i 整除 aia_i(即 aia_i 能被 did_i 整除)。

此时,第 ii 种糖果的一个包装盒的价格为 bi⋅dib_i \cdot d_i。我们记该价格为 cic_i,即 ci=bi⋅dic_i = b_i \cdot d_i。

包装完成后,这些包装盒将按顺序摆放在货架上,对应的价格序列为 c1,c2,…,cnc_1, c_2, \ldots, c_n。我们将使用价签来标示这些包装盒的价格:若一段连续子序列 cl=cl+1=…=crc_l = c_{l+1} = \ldots = c_r 的值全部相等,则一个价签即可标示从第 ll 个到第 rr 个(含端点)的所有包装盒。每个从 11 到 nn 的包装盒都必须至少被一个价签所标示。例如,若 c1,…,cn=[4,4,2,4,4]c_1, \ldots, c_n = [4, 4, 2, 4, 4],则只需 33 个价签即可完成标示:第一个价签标示第 11 和第 22 个包装盒,第二个价签标示第 33 个包装盒,第三个价签标示第 44 和第 55 个包装盒。

你将获得整数 a1,b1,a2,b2,…,an,bna_1, b_1, a_2, b_2, \ldots, a_n, b_n。你的任务是为每个 ii 选择整数 did_i,使得对所有 ii 均有 did_i 整除 aia_i,并使得描述序列 c1,c2,…,cnc_1, c_2, \ldots, c_n 所需的价签数量最小化。

为更清晰理解题意,请参见第一个测试用例的图示:

我们再次明确本题中所用符号的含义:

aia_i — 商店中第 ii 种糖果的数量;
bib_i — 第 ii 种糖果的单价;
did_i — 每个包装盒中第 ii 种糖果的数量;
cic_i — 第 ii 种糖果的一个包装盒的价格,由公式 ci=bi⋅dic_i = b_i \cdot d_i 给出。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤100 0001 \le t \le 100\,000). Description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤200 0002 \le n \le 200\,000) — the number of types of candies.

Each of the next nn lines of each test case contains two integers aia_i and bib_i (1≤ai≤1091 \le a_i \le 10^9, 1≤bi≤10 0001 \le b_i \le 10\,000) — the number of candies and the cost of one candy of type ii, respectively.

It is guaranteed that the sum of nn over all test cases does not exceed 200 000200\,000.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤100 0001 \le t \le 100\,000)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(2≤n≤200 0002 \le n \le 200\,000)—— 表示糖果类型的数量。

每个测试用例接下来的 nn 行中,每行包含两个整数 aia_i 和 bib_i(1≤ai≤1091 \le a_i \le 10^9,1≤bi≤10 0001 \le b_i \le 10\,000)—— 分别表示第 ii 种糖果的数量及该种糖果的单价。

保证所有测试用例的 nn 值之和不超过 200 000200\,000。

输出格式

For each test case, output the minimum number of price tags required to describe the costs of all packs of candies in the store.

对于每个测试用例,输出描述商店中所有糖果包价格所需的最少价格标签数量。

输入输出样例

  • 输入#1

    5
    4
    20 3
    6 2
    14 5
    20 7
    3
    444 5
    2002 10
    2020 2
    5
    7 7
    6 5
    15 2
    10 3
    7 7
    5
    10 1
    11 5
    5 1
    2 2
    8 2
    6
    7 12
    12 3
    5 3
    9 12
    9 3
    1000000000 10000

    输出#1

    2
    1
    3
    2
    5

说明/提示

In the first test case, you can choose d1=4d_1 = 4, d2=6d_2 = 6, d3=7d_3 = 7, d4=5d_4 = 5. Then the cost of packs will be equal to [12,12,35,35][12, 12, 35, 35]. 22 price tags are enough to describe them, the first price tag for c1,c2c_1, c_2 and the second price tag for c3,c4c_3, c_4. It can be shown that with any correct choice of did_i, at least 22 of the price tag will be needed to describe all the packs. Also note that this example is illustrated by a picture in the statement.

In the second test case, with d1=4d_1 = 4, d2=2d_2 = 2, d3=10d_3 = 10, the costs of all packs will be equal to 2020. Thus, 11 price tag is enough to describe all the packs. Note that aia_i is divisible by did_i for all ii, which is necessary condition.

In the third test case, it is not difficult to understand that one price tag can be used to describe 22nd, 33rd and 44th packs. And additionally a price tag for pack 11 and pack 55. Total: 33 price tags.

在第一个测试用例中,你可以选择 d1=4d_1 = 4、d2=6d_2 = 6、d3=7d_3 = 7、d4=5d_4 = 5。此时各包装的成本分别为 [12,12,35,35][12, 12, 35, 35]。仅需 22 个价格标签即可描述全部包装:第一个价格标签用于 c1,c2c_1, c_2,第二个价格标签用于 c3,c4c_3, c_4。可以证明,对于任意满足条件的 did_i 的取值,至少需要 22 个价格标签来描述所有包装。此外,请注意该示例已在题目陈述中的图片中进行了图示。

在第二个测试用例中,若取 d1=4d_1 = 4、d2=2d_2 = 2、d3=10d_3 = 10,则所有包装的成本均为 2020。因此,仅需 11 个价格标签即可描述全部包装。注意,对所有 ii,均有 aia_i 被 did_i 整除,这是必要条件。

在第三个测试用例中,不难看出,一个价格标签即可描述第 22、第 33 和第 44 个包装;此外还需分别为第 11 个和第 55 个包装各分配一个价格标签。总计:33 个价格标签。

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

首页