CF1654D.Potion Brewing Class

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Alice's potion making professor gave the following assignment to his students: brew a potion using nn ingredients, such that the proportion of ingredient ii in the final potion is ri>0r_i \gt 0 (and r1+r2+⋯+rn=1r_1 + r_2 + \cdots + r_n = 1).

He forgot the recipe, and now all he remembers is a set of n−1n-1 facts of the form, "ingredients ii and jj should have a ratio of xx to yy" (i.e., if aia_i and aja_j are the amounts of ingredient ii and jj in the potion respectively, then it must hold ai/aj=x/ya_i/a_j = x/y), where xx and yy are positive integers. However, it is guaranteed that the set of facts he remembers is sufficient to uniquely determine the original values rir_i.

He decided that he will allow the students to pass the class as long as they submit a potion which satisfies all of the n−1n-1 requirements (there may be many such satisfactory potions), and contains a positive integer amount of each ingredient.

Find the minimum total amount of ingredients needed to make a potion which passes the class. As the result can be very large, you should print the answer modulo 998 244 353998\,244\,353.

爱丽丝的魔药学教授给学生们布置了如下作业:使用 nn 种原料配制一种魔药,使得第 ii 种原料在最终魔药中所占比例为 ri>0r_i \gt 0(且满足 r1+r2+⋯+rn=1r_1 + r_2 + \cdots + r_n = 1)。

他遗忘了原始配方,如今仅记得一组共 n−1n-1 条事实,每条形如:“原料 ii 与原料 jj 的用量之比应为 x:yx : y”(即若魔药中原料 ii 与原料 jj 的用量分别为 aia_i 和 aja_j,则必须满足 ai/aj=x/ya_i/a_j = x/y),其中 xx 和 yy 均为正整数。但题目保证他所记住的这组事实足以唯一确定原始比例值 rir_i。

他决定:只要学生提交的魔药满足全部 n−1n-1 条约束条件(满足条件的魔药可能有多个),且每种原料的用量均为正整数,则视为通过课程。

请找出满足上述要求的魔药所需的最小总原料用量。由于答案可能非常大,请将结果对 998 244 353998\,244\,353 取模后输出。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

Each of the next n−1n-1 lines contains four integers i,j,x,yi, j, x, y (1≤i,j≤n1 \le i, j \le n, i≠ji\not=j, 1≤x,y≤n1\le x, y \le n) — ingredients ii and jj should have a ratio of xx to yy. It is guaranteed that the set of facts is sufficient to uniquely determine the original values rir_i.

It is also guaranteed that the sum of nn for all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。

接下来的 n−1n-1 行中,每行包含四个整数 i,j,x,yi, j, x, y(1≤i,j≤n1 \le i, j \le n,i≠ji \neq j,1≤x,y≤n1 \le x, y \le n)—— 表示原料 ii 与原料 jj 的比例应为 x:yx : y。题目保证所给条件足以唯一确定原始值 rir_i。

同时保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, print the minimum total amount of ingredients needed to make a potion which passes the class, modulo 998 244 353998\,244\,353.

对于每个测试用例,输出制作一剂能通过课程的药水所需的最少总原料量,对 998 244 353998\,244\,353 取模。

输入输出样例

  • 输入#1

    3
    4
    3 2 3 4
    1 2 4 3
    1 4 2 4
    8
    5 4 2 3
    6 4 5 4
    1 3 5 2
    6 8 2 1
    3 5 3 4
    3 2 2 5
    6 7 4 3
    17
    8 7 4 16
    9 17 4 5
    5 14 13 12
    11 1 17 14
    6 13 8 9
    2 11 3 11
    4 17 7 2
    17 16 8 6
    15 5 1 14
    16 7 1 10
    12 17 13 10
    11 16 7 2
    10 11 6 4
    13 17 14 6
    3 11 15 8
    15 6 12 8

    输出#1

    69
    359
    573672453

说明/提示

In the first test case, the minimum total amount of ingredients is 6969. In fact, the amounts of ingredients 1,2,3,41, 2, 3, 4 of a valid potion are 16,12,9,3216, 12, 9, 32, respectively. The potion is valid because

  • Ingredients 33 and 22 have a ratio of 9:12=3:49 : 12 = 3 : 4;
  • Ingredients 11 and 22 have a ratio of 16:12=4:316 : 12 = 4 : 3;
  • Ingredients 11 and 44 have a ratio of 16:32=2:416 : 32 = 2 : 4.

In the second test case, the amounts of ingredients 1,2,3,4,5,6,7,81, 2, 3, 4, 5, 6, 7, 8 in the potion that minimizes the total amount of ingredients are 60,60,24,48,32,60,45,3060, 60, 24, 48, 32, 60, 45, 30.

在第一个测试用例中,所需原料的最小总量为 6969。事实上,一种合法药水所需的原料 1,2,3,41, 2, 3, 4 的量分别为 16,12,9,3216, 12, 9, 32。该药水是合法的,因为:

  • 原料 33 与原料 22 的比例为 9:12=3:49 : 12 = 3 : 4;
  • 原料 11 与原料 22 的比例为 16:12=4:316 : 12 = 4 : 3;
  • 原料 11 与原料 44 的比例为 16:32=2:416 : 32 = 2 : 4。

在第二个测试用例中,使原料总用量最小的药水中,原料 1,2,3,4,5,6,7,81, 2, 3, 4, 5, 6, 7, 8 的用量分别为 60,60,24,48,32,60,45,3060, 60, 24, 48, 32, 60, 45, 30。

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

首页