CF1280E.Kirchhoff's Current Loss

省选/NOI-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Your friend Kirchhoff is shocked with the current state of electronics design.

"Ohmygosh! Watt is wrong with the field? All these circuits are inefficient! There's so much capacity for improvement. The electrical engineers must not conduct their classes very well. It's absolutely revolting" he said.

The negativity just keeps flowing out of him, but even after complaining so many times he still hasn't lepton the chance to directly change anything.

"These circuits have too much total resistance. Wire they designed this way? It's just causing a massive loss of resistors! Their entire field could conserve so much money if they just maximized the potential of their designs. Why can't they just try alternative ideas?"

The frequency of his protests about the electrical engineering department hertz your soul, so you have decided to take charge and help them yourself. You plan to create a program that will optimize the circuits while keeping the same circuit layout and maintaining the same effective resistance.

A circuit has two endpoints, and is associated with a certain constant, RR, called its effective resistance.

The circuits we'll consider will be formed from individual resistors joined together in series or in parallel, forming more complex circuits. The following image illustrates combining circuits in series or parallel.

According to your friend Kirchhoff, the effective resistance can be calculated quite easily when joining circuits this way:

  • When joining kk circuits in series with effective resistances R1,R2,…,RkR_1, R_2, \ldots, R_k, the effective resistance RR of the resulting circuit is the sum $$R = R_1 + R_2 + \ldots + R_k.$$

  • When joining kk circuits in parallel with effective resistances R1,R2,…,RkR_1, R_2, \ldots, R_k, the effective resistance RR of the resulting circuit is found by solving for RR in $$\frac{1}{R} = \frac{1}{R_1} + \frac{1}{R_2} + \ldots + \frac{1}{R_k},$$ assuming all Ri>0R_i \gt 0; if at least one Ri=0R_i = 0, then the effective resistance of the whole circuit is simply R=0R = 0.

Circuits will be represented by strings. Individual resistors are represented by an asterisk, "*". For more complex circuits, suppose s1,s2,…,sks_1, s_2, \ldots, s_k represent k≥2k \ge 2 circuits. Then:

  • "(s1s_1 S s2s_2 S …\ldots S sks_k)" represents their series circuit;
  • "(s1s_1 P s2s_2 P …\ldots P sks_k)" represents their parallel circuit.

For example, "(* P (* S *) P *)" represents the following circuit:

Given a circuit, your task is to assign the resistances of the individual resistors such that they satisfy the following requirements:

  • Each individual resistor has a nonnegative integer resistance value;
  • The effective resistance of the whole circuit is rr;
  • The sum of the resistances of the individual resistors is minimized.

If there are nn individual resistors, then you need to output the list r1,r2,…,rnr_1, r_2, \ldots, r_n (0≤ri0 \le r_i, and rir_i is an integer), where rir_i is the resistance assigned to the ii-th individual resistor that appears in the input (from left to right). If it is impossible to accomplish the task, you must say so as well.

If it is possible, then it is guaranteed that the minimum sum of resistances is at most 101810^{18}.

你的朋友基尔霍夫对当前电子电路设计的现状感到震惊。

“天哪!这行当到底怎么了?所有这些电路都效率低下!还有巨大的改进空间。电气工程师们一定没把课教好。简直令人作呕!”他说道。

负面情绪持续从他身上倾泻而出,然而即便抱怨了无数次,他仍未能获得直接改变现状的机会。

“这些电路的总电阻太大了!他们为何要这样布线?这只会造成大量电阻器的浪费!只要充分挖掘设计潜力,整个领域本可以节省巨额资金。他们为何不尝试一些替代方案呢?”

他对电气工程系的抗议频率之高,令你灵魂震颤(hertz your soul),因此你决定挺身而出,亲自出手相助。你计划编写一个程序,在保持原有电路拓扑结构不变的前提下,优化电路参数,同时维持其等效电阻恒定。

一个电路具有两个端点,并关联一个称为其等效电阻的常数 RR。

我们所考虑的电路由单个电阻器通过串联或并联方式连接而成,从而构成更复杂的电路。下图展示了电路的串联与并联组合方式:

据你朋友基尔霍夫所述,以这种方式组合电路时,其等效电阻可非常简便地计算:

  • 当将 kk 个等效电阻分别为 R1,R2,…,RkR_1, R_2, \ldots, R_k 的电路串联时,所得电路的等效电阻 RR 为各电阻之和:

    R=R_1+R_2+ldots+R_k.R = R\_1 + R\_2 + \\ldots + R\_k.

  • 当将 kk 个等效电阻分别为 R1,R2,…,RkR_1, R_2, \ldots, R_k 的电路并联时,所得电路的等效电阻 RR 满足:

    frac1R=frac1R_1+frac1R_2+ldots+frac1R_k,\\frac{1}{R} = \\frac{1}{R\_1} + \\frac{1}{R\_2} + \\ldots + \\frac{1}{R\_k},

    其中假设所有 Ri>0R_i > 0;若至少有一个 Ri=0R_i = 0,则整个电路的等效电阻即为 R=0R = 0。

电路将用字符串表示。单个电阻器用星号 “*” 表示。对于更复杂的电路,设 s1,s2,…,sks_1, s_2, \ldots, s_k 表示 k≥2k \ge 2 个电路,则:

  • “(s1s_1 S s2s_2 S …\ldots S sks_k)” 表示它们的串联电路;
  • “(s1s_1 P s2s_2 P …\ldots P sks_k)” 表示它们的并联电路。

例如,“(* P (* S *) P *)” 表示如下电路:

给定一个电路,你的任务是为每个单个电阻器分配阻值,使其满足以下要求:

  • 每个单个电阻器的阻值为非负整数;
  • 整个电路的等效电阻恰好为 rr;
  • 所有单个电阻器阻值之和最小。

若电路中共有 nn 个单个电阻器,则你需要输出列表 r1,r2,…,rnr_1, r_2, \ldots, r_n(其中 0≤ri0 \le r_i,且 rir_i 为整数),其中 rir_i 表示输入字符串中从左至右第 ii 个出现的单个电阻器所分配的阻值。若该任务无法实现,你也必须明确指出。

若任务可行,则保证最小总阻值不超过 101810^{18}。

输入格式

The first line of input contains a single integer tt (1≤t≤320001 \le t \le 32000), denoting the number of test cases. The next lines contain descriptions of the test cases.

Each test case consists of a single line containing the integer rr (1≤r≤1061 \le r \le 10^6), space, and then the string representing the circuit. It is guaranteed that the string is valid and follows the description above. The number of individual resistors (symbols "*") is at least 11 and at most 8000080000.

It is guaranteed that the total number of individual resistors across all test cases is at most 320000320000.

输入的第一行包含一个整数 tt(1≤t≤320001 \le t \le 32000),表示测试用例的数量。接下来的若干行包含各测试用例的描述。

每个测试用例由一行组成,该行包含一个整数 rr(1≤r≤1061 \le r \le 10^6),后跟一个空格,再跟一个表示电路的字符串。保证该字符串是合法的,并符合上述描述。单个电阻器(符号“*”)的数量至少为 11,至多为 8000080000。

保证所有测试用例中单个电阻器的总数不超过 320000320000。

输出格式

For each test case, print a single line:

  • If it's possible to achieve an effective resistance of rr, print "REVOLTING" (without quotes) and then nn integers r1,r2,…,rnr_1, r_2, \ldots, r_n — the resistances assigned to the individual resistors. Here, nn denotes the number of the individual resistors in the circuit.

    There may be multiple possible such assignments with a minimal sum of resistances of the individual resistors, you can output any of them;

  • If it's impossible, print the string: "LOSS" (without quotes).

对于每个测试用例,输出一行:

  • 如果可以实现等效电阻 rr,则输出 "REVOLTING"(不带引号),随后输出 nn 个整数 r1,r2,…,rnr_1, r_2, \ldots, r_n —— 即分配给各个电阻器的阻值。其中,nn 表示电路中单个电阻器的数量。

    可能存在多种满足条件的分配方案,且使得各电阻器阻值之和最小;您可输出其中任意一种;

  • 如果不可能实现,则输出字符串 "LOSS"(不带引号)。

输入输出样例

  • 输入#1

    3
    5 *
    1 (* S *)
    1 (* P (* S *))

    输出#1

    REVOLTING 5
    REVOLTING 1 0
    REVOLTING 2 1 1

说明/提示

The following illustrates the third sample case:

Here, the sum of the resistances of the individual resistors is 2+1+1=42 + 1 + 1 = 4, which can be shown to be the minimum. Note that there may be other assignments that achieve this minimum.

下图展示了第三个样例:

此处,各电阻器的阻值之和为 2+1+1=42 + 1 + 1 = 4,可证明该值为最小值。注意,可能存在其他分配方案也能达到这一最小值。

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

首页